-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
base: master
Are you sure you want to change the base?
add task solution #1200
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
|
||
<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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider adding more detailed error messages or logging within the |
||
.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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
.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> | ||
); | ||
}; |
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider adding error handling for the |
||
|
||
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider adding error handling for the |
||
|
||
export const deleteComment = (commentId: number) => { | ||
return client.delete(`/comments/${commentId}`); | ||
}; | ||
Comment on lines
+17
to
+19
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider adding error handling for the
Comment on lines
+4
to
+19
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider adding error handling for the
Comment on lines
+4
to
+6
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ensure that the |
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider adding error handling for the
Comment on lines
+6
to
+8
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ensure that the |
There was a problem hiding this comment.
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.