-
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
Develop #1219
base: master
Are you sure you want to change the base?
Develop #1219
Changes from 3 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 |
---|---|---|
|
@@ -4,57 +4,118 @@ import 'bulma/css/bulma.css'; | |
import '@fortawesome/fontawesome-free/css/all.css'; | ||
import './App.scss'; | ||
|
||
import { PostsList } from './components/PostsList'; | ||
import { PostDetails } from './components/PostDetails'; | ||
import { UserSelector } from './components/UserSelector'; | ||
import React, { useEffect } from 'react'; | ||
import { User } from './types/User'; | ||
import { getPostsOfUser, getUsers } from './utils/api'; | ||
import { Post } from './types/Post'; | ||
import { PostsList } from './components/PostsList'; | ||
import { Loader } from './components/Loader'; | ||
import { PostDetails } from './components/PostDetails'; | ||
|
||
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 [usersFromServer, setUsersFromServer] = React.useState<User[]>(); | ||
const [postsFromServer, setPostsFromServer] = React.useState<Post[]>(); | ||
|
||
<div className="block" data-cy="MainContent"> | ||
<p data-cy="NoSelectedUser">No user selected</p> | ||
const [chosenUser, setChosenUser] = React.useState<User | null>(null); | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
const [postError, setPostError] = React.useState(false); | ||
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 |
||
const [isPostsLoading, setIsPostsLoading] = React.useState(false); | ||
const [activePost, setActivePost] = React.useState<Post | null>(null); | ||
|
||
<Loader /> | ||
useEffect(() => { | ||
const fetchUsers = async () => { | ||
try { | ||
const currentUsers = await getUsers(); | ||
|
||
<div | ||
className="notification is-danger" | ||
data-cy="PostsLoadingError" | ||
> | ||
Something went wrong! | ||
</div> | ||
setUsersFromServer(currentUsers); | ||
} catch (err) { | ||
} finally { | ||
} | ||
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 block in the |
||
}; | ||
|
||
fetchUsers(); | ||
}, []); | ||
|
||
<div className="notification is-warning" data-cy="NoPostsYet"> | ||
No posts yet | ||
useEffect(() => { | ||
const fetchPosts = async () => { | ||
setIsPostsLoading(true); | ||
try { | ||
const currentPosts = await getPostsOfUser(chosenUser?.id ?? 0); | ||
|
||
setPostsFromServer(currentPosts); | ||
} catch (err) { | ||
setPostError(true); | ||
} finally { | ||
setIsPostsLoading(false); | ||
} | ||
}; | ||
|
||
if (chosenUser) { | ||
setActivePost(null); | ||
fetchPosts(); | ||
} | ||
}, [chosenUser]); | ||
|
||
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={usersFromServer ?? []} | ||
chosenUser={chosenUser} | ||
chooseUser={setChosenUser} | ||
/> | ||
</div> | ||
|
||
<PostsList /> | ||
<div className="block" data-cy="MainContent"> | ||
{!chosenUser ? ( | ||
<p data-cy="NoSelectedUser">No user selected</p> | ||
) : isPostsLoading ? ( | ||
<Loader /> | ||
) : postError ? ( | ||
<div | ||
className="notification is-danger" | ||
data-cy="PostsLoadingError" | ||
> | ||
Something went wrong! | ||
</div> | ||
) : postsFromServer?.length === 0 ? ( | ||
<div className="notification is-warning" data-cy="NoPostsYet"> | ||
No posts yet | ||
</div> | ||
) : ( | ||
<PostsList | ||
posts={postsFromServer} | ||
activePost={activePost} | ||
choosePost={setActivePost} | ||
/> | ||
)} | ||
</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': activePost }, | ||
)} | ||
> | ||
{activePost && ( | ||
<div className="tile is-child box is-success"> | ||
<PostDetails post={activePost} /> | ||
</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
postError
state is initialized but not used in the component logic. Consider using it to handle errors related to fetching posts or remove it if it's unnecessary.