-
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
results #1211
base: master
Are you sure you want to change the base?
results #1211
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,116 @@ 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 { getPosts, getUsers } from './api/api'; | ||
import { Post } from './types/Post'; | ||
|
||
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 [posts, setPosts] = useState<Post[]>([]); | ||
const [isError, setIsError] = useState(false); | ||
const [loading, setLoading] = useState(false); | ||
const [activeUser, setActiveUser] = useState<User | null>(null); | ||
const [activePost, setActivePost] = useState<Post | null>(null); | ||
const [showSideBar, setShowSideBar] = useState(false); | ||
|
||
<div className="block" data-cy="MainContent"> | ||
<p data-cy="NoSelectedUser">No user selected</p> | ||
useEffect(() => { | ||
getUsers() | ||
.then(setUsers) | ||
.catch(() => setIsError(true)); | ||
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. The error handling here is minimal. Consider providing more detailed error messages or logging to help with debugging and user feedback. |
||
}, []); | ||
|
||
<Loader /> | ||
useEffect(() => { | ||
if (activeUser) { | ||
setLoading(true); | ||
|
||
<div | ||
className="notification is-danger" | ||
data-cy="PostsLoadingError" | ||
> | ||
Something went wrong! | ||
</div> | ||
getPosts(activeUser) | ||
.then(setPosts) | ||
.catch(() => setIsError(true)) | ||
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, the error handling for the |
||
.finally(() => setLoading(false)); | ||
Comment on lines
+35
to
+38
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 enhancing error handling in this catch block for better debugging and user feedback. |
||
} | ||
}, [activeUser]); | ||
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 necessary dependencies are included in the |
||
|
||
const isSideBar = | ||
activePost && showSideBar && activeUser?.id === activePost.userId; | ||
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. The logic for determining |
||
|
||
<div className="notification is-warning" data-cy="NoPostsYet"> | ||
No posts yet | ||
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} | ||
onActiveUser={setActiveUser} | ||
activeUser={activeUser} | ||
onShowSideBar={setShowSideBar} | ||
onActivePost={setActivePost} | ||
/> | ||
</div> | ||
|
||
<PostsList /> | ||
<div className="block" data-cy="MainContent"> | ||
{loading ? ( | ||
<Loader /> | ||
) : ( | ||
<> | ||
{isError && ( | ||
<div | ||
className="notification is-danger" | ||
data-cy="PostsLoadingError" | ||
> | ||
Something went wrong! | ||
</div> | ||
)} | ||
|
||
{!isError && !activeUser && ( | ||
<p data-cy="NoSelectedUser">No user selected</p> | ||
)} | ||
|
||
{!isError && activeUser && posts.length === 0 && ( | ||
<div | ||
className="notification is-warning" | ||
data-cy="NoPostsYet" | ||
> | ||
No posts yet | ||
</div> | ||
)} | ||
|
||
{!isError && activeUser && posts.length > 0 && ( | ||
<PostsList | ||
posts={posts} | ||
onActivePost={setActivePost} | ||
onShowSideBar={setShowSideBar} | ||
/> | ||
)} | ||
</> | ||
)} | ||
</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': isSideBar, | ||
}, | ||
)} | ||
> | ||
<div className="tile is-child box is-success "> | ||
{activePost && ( | ||
<PostDetails post={activePost} key={activePost.id} /> | ||
)} | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
</main> | ||
); | ||
</main> | ||
); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { Comment } from '../types/Comment'; | ||
import { Post } from '../types/Post'; | ||
import { User } from '../types/User'; | ||
import { client } from '../utils/fetchClient'; | ||
|
||
export const getUsers = () => { | ||
return client.get<User[]>('/users'); | ||
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 API request to manage potential failures and provide feedback to the user. 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 getPosts = ({ id }: Pick<User, 'id'>) => { | ||
return client.get<Post[]>(`/posts?userId=${id}`); | ||
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 implementing error handling for this API request. 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 |
||
}; | ||
|
||
export const getComments = ({ id }: Pick<Post, 'id'>) => { | ||
return client.get<Comment[]>(`/comments?postId=${id}`); | ||
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 this API request to manage potential errors. 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. Add error handling for the |
||
}; | ||
|
||
export const addComment = ({ | ||
postId, | ||
name, | ||
email, | ||
body, | ||
}: Omit<Comment, 'id'>) => { | ||
const data = { | ||
postId, | ||
name, | ||
email, | ||
body, | ||
}; | ||
|
||
return client.post<Comment>('/comments', data); | ||
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 implementing error handling for the POST request to manage potential failures. 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. The |
||
}; | ||
|
||
export const deleteComment = ({ id }: Pick<Comment, 'id'>) => { | ||
return client.delete(`/comments/${id}`); | ||
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 DELETE request to manage potential errors. 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. Include error handling for 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.
Consider providing more detailed error handling or logging in the catch block to help with debugging and understanding what went wrong.