-
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 #1193
base: master
Are you sure you want to change the base?
add task solution #1193
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,8 @@ | ||
import classNames from 'classnames'; | ||
import { useEffect, useState } from 'react'; | ||
import { getUsers, getUserPosts } from './utils/fetchClient'; | ||
import { User } from './types/User'; | ||
import { Post } from './types/Post'; | ||
import cn from 'classnames'; | ||
|
||
import 'bulma/css/bulma.css'; | ||
import '@fortawesome/fontawesome-free/css/all.css'; | ||
|
@@ -9,52 +13,102 @@ import { PostDetails } from './components/PostDetails'; | |
import { UserSelector } from './components/UserSelector'; | ||
import { Loader } from './components/Loader'; | ||
|
||
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: React.FC = () => { | ||
const [users, setUsers] = useState<User[]>([]); | ||
const [posts, setPosts] = useState<Post[]>([]); | ||
const [currUser, setCurrUser] = useState<User | null>(null); | ||
const [currPost, setCurrPost] = useState<Post | null>(null); | ||
const [openModal, setOpenModal] = useState(false); | ||
const [loading, setLoading] = useState(false); | ||
const [error, setError] = useState(false); | ||
|
||
<div className="block" data-cy="MainContent"> | ||
<p data-cy="NoSelectedUser">No user selected</p> | ||
const searchUserPosts = (userId: number) => { | ||
getUserPosts(userId) | ||
.then(setPosts) | ||
.catch(() => setError(true)) | ||
.finally(() => setLoading(false)); | ||
}; | ||
Comment on lines
+26
to
+29
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 in the |
||
|
||
<Loader /> | ||
useEffect(() => { | ||
getUsers().then(setUsers); | ||
}, []); | ||
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 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 |
||
|
||
<div | ||
className="notification is-danger" | ||
data-cy="PostsLoadingError" | ||
> | ||
Something went wrong! | ||
</div> | ||
useEffect(() => { | ||
if (currUser) { | ||
setLoading(true); | ||
searchUserPosts(currUser.id); | ||
setCurrPost(null); | ||
setOpenModal(false); | ||
} | ||
}, [currUser]); | ||
|
||
<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} | ||
currUser={currUser} | ||
setCurrUser={setCurrUser} | ||
/> | ||
</div> | ||
|
||
<PostsList /> | ||
<div className="block" data-cy="MainContent"> | ||
{!currUser && <p data-cy="NoSelectedUser">No user selected</p>} | ||
|
||
{!loading && | ||
!error && | ||
currUser && | ||
(posts.length > 0 ? ( | ||
<PostsList | ||
posts={posts} | ||
currPost={currPost} | ||
setOpenModal={setOpenModal} | ||
setCurrPost={setCurrPost} | ||
/> | ||
) : ( | ||
<div | ||
className="notification is-warning" | ||
data-cy="NoPostsYet" | ||
> | ||
No posts yet | ||
</div> | ||
))} | ||
|
||
{loading && <Loader />} | ||
|
||
{error && ( | ||
<div | ||
className="notification is-danger" | ||
data-cy="PostsLoadingError" | ||
> | ||
Something went wrong! | ||
</div> | ||
)} | ||
</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={cn('tile', 'is-parent', 'is-8-desktop', 'Sidebar', { | ||
'Sidebar--open': currPost, | ||
})} | ||
> | ||
<div className="tile is-child box is-success "> | ||
{currPost && ( | ||
<PostDetails | ||
currPost={currPost} | ||
openModal={openModal} | ||
setOpenModal={setOpenModal} | ||
/> | ||
)} | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
</main> | ||
); | ||
</main> | ||
); | ||
}; |
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.
The error handling here is quite generic. Consider providing more detailed feedback or logging the error to help with debugging.