Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add task solution #1200

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
159 changes: 122 additions & 37 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,53 +8,138 @@ import { PostsList } from './components/PostsList';
import { PostDetails } from './components/PostDetails';
import { UserSelector } from './components/UserSelector';
import { Loader } from './components/Loader';
import { useEffect, useState } from 'react';
import { User } from './types/User';
import { getUsers } from './api/users';
import { getPosts } from './api/posts';
import { Post } from './types/Post';
import { getComments } from './api/comments';
import { Comment } from './types/Comment';

export const App = () => (
<main className="section">
<div className="container">
<div className="tile is-ancestor">
<div className="tile is-parent">
<div className="tile is-child box is-success">
<div className="block">
<UserSelector />
</div>
export const App = () => {
const [users, setUsers] = useState<User[]>([]);
const [userSelected, setUserSelected] = useState<User | null>(null);
const [posts, setPosts] = useState<Post[]>([]);
const [post, setPost] = useState<Post | null>(null);
const [loadingPosts, setLoadingPosts] = useState(false);
const [errorLoadPosts, setErrorLoadPosts] = useState<boolean>(false);
const [openPostDetails, setOpenPostDetails] = useState<number>(0);
const [comments, setComments] = useState<Comment[]>([]);
const [loadingComments, setLoadingComments] = useState(false);
const [errorLoadComments, setErrorLoadComments] = useState<boolean>(false);
const [writeComment, setWriteComment] = useState<boolean>(false);
Comment on lines +20 to +30

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure that all state setters have explicit type definitions to enhance type safety and prevent potential runtime errors.


<div className="block" data-cy="MainContent">
<p data-cy="NoSelectedUser">No user selected</p>
useEffect(() => {
getUsers().then(setUsers);
}, []);

<Loader />
function getPostsUser(user: User) {
setLoadingPosts(true);
getPosts(user.id)
.then(setPosts)
.catch(() => {
setErrorLoadPosts(true);
})
Comment on lines +40 to +42

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding more detailed error messages or logging within the .catch() block to aid in debugging and provide more context about the error.

.finally(() => setLoadingPosts(false));
}

<div
className="notification is-danger"
data-cy="PostsLoadingError"
>
Something went wrong!
</div>
function getCommentsPost(gettingPost: Post) {
setLoadingComments(true);
getComments(gettingPost.id)
.then(setComments)
.catch(() => {
setErrorLoadComments(true);
})
Comment on lines +50 to +52

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to the previous comment, consider adding more detailed error messages or logging within the .catch() block to aid in debugging and provide more context about the error.

.finally(() => setLoadingComments(false));
}

const showPostsList: boolean | null =
!!posts.length && !errorLoadPosts && userSelected && !loadingPosts;

<div className="notification is-warning" data-cy="NoPostsYet">
No posts yet
const showNoPostsYet: boolean | null =
posts.length === 0 && !errorLoadPosts && userSelected && !loadingPosts;

return (
<main className="section">
<div className="container">
<div className="tile is-ancestor">
<div className="tile is-parent">
<div className="tile is-child box is-success">
<div className="block">
<UserSelector
users={users}
userSelected={userSelected}
setUserSelected={setUserSelected}
getPostUser={getPostsUser}
setOpenPostDetails={setOpenPostDetails}
setWriteComment={setWriteComment}
setPosts={setPosts}
setPost={setPost}
/>
</div>

<PostsList />
<div className="block" data-cy="MainContent">
{users.length > 0 && !userSelected && (
<p data-cy="NoSelectedUser">No user selected</p>
)}

{loadingPosts && <Loader />}

{errorLoadPosts && (
<div
className="notification is-danger"
data-cy="PostsLoadingError"
>
Something went wrong!
</div>
)}

{showNoPostsYet && (
<div className="notification is-warning" data-cy="NoPostsYet">
No posts yet
</div>
)}

{showPostsList && (
<PostsList
posts={posts}
setPost={setPost}
openPostDetails={openPostDetails}
setOpenPostDetails={setOpenPostDetails}
getCommentsPost={getCommentsPost}
setWriteComment={setWriteComment}
/>
)}
</div>
</div>
</div>
</div>

<div
data-cy="Sidebar"
className={classNames(
'tile',
'is-parent',
'is-8-desktop',
'Sidebar',
'Sidebar--open',
)}
>
<div className="tile is-child box is-success ">
<PostDetails />
<div
data-cy="Sidebar"
className={classNames(
'tile',
'is-parent',
'is-8-desktop',
'Sidebar',
{ 'Sidebar--open': openPostDetails },
)}
>
<div className="tile is-child box is-success ">
{post && (
<PostDetails
post={post}
comments={comments}
setComments={setComments}
loadingComments={loadingComments}
errorLoadComments={errorLoadComments}
writeComment={writeComment}
setWriteComment={setWriteComment}
/>
)}
</div>
</div>
</div>
</div>
</div>
</main>
);
</main>
);
};
19 changes: 19 additions & 0 deletions src/api/comments.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Comment } from '../types/Comment';
import { client } from '../utils/fetchClient';

export const getComments = (postId: number) => {
return client.get<Comment[]>(`/comments?postId=${postId}`);
};
Comment on lines +4 to +6

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding error handling for the getComments function to manage potential errors gracefully.


export const createComment = ({
postId,
name,
email,
body,
}: Omit<Comment, 'id'>) => {
return client.post<Comment>(`/comments`, { postId, name, email, body });
};
Comment on lines +13 to +15

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding error handling for the createComment function to manage potential errors gracefully.


export const deleteComment = (commentId: number) => {
return client.delete(`/comments/${commentId}`);
};
Comment on lines +17 to +19

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding error handling for the deleteComment function to manage potential errors gracefully.

Comment on lines +4 to +19

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure that all functions have explicit return type definitions to enhance type safety and prevent potential runtime errors.

6 changes: 6 additions & 0 deletions src/api/posts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { Post } from '../types/Post';
import { client } from '../utils/fetchClient';

export const getPosts = (userId: number) => {
return client.get<Post[]>(`/posts?userId=${userId}`);
};
Comment on lines +4 to +6

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding error handling for the getPosts function to manage potential errors gracefully.

Comment on lines +4 to +6

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure that the getPosts function has an explicit return type definition to enhance type safety and prevent potential runtime errors.

8 changes: 8 additions & 0 deletions src/api/users.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { User } from '../types/User';
import { client } from '../utils/fetchClient';

export const USER_ID = 1844;

export const getUsers = () => {
return client.get<User[]>(`/users`);
};
Comment on lines +6 to +8

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding error handling for the getUsers function to manage potential errors gracefully.

Comment on lines +6 to +8

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure that the getUsers function has an explicit return type definition to enhance type safety and prevent potential runtime errors.

Loading
Loading