-
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 solution #1224
base: master
Are you sure you want to change the base?
add solution #1224
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 |
---|---|---|
@@ -1,60 +1,155 @@ | ||
import classNames from 'classnames'; | ||
|
||
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 { Loader } from './components/Loader'; | ||
import { useEffect, useState } from 'react'; | ||
import { User } from './types/User'; | ||
import { client } from './utils/fetchClient'; | ||
import { Post } from './types/Post'; | ||
import { Comment } from './types/Comment'; | ||
import classNames from 'classnames'; | ||
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 [users, setUsers] = useState<User[]>([]); | ||
const [activeUser, setActiveUser] = useState<User | null>(null); | ||
const [posts, setPosts] = useState<Post[]>([]); | ||
const [isLoading, setIsLoading] = useState(false); | ||
const [isError, setIsError] = useState(false); | ||
const [activePost, setActivePost] = useState<Post | null>(null); | ||
const [comments, setComments] = useState<Comment[]>([]); | ||
const [isCommentsLoading, setIsCommentsLoading] = useState(false); | ||
const [isCommentsError, setIsCommentsError] = useState(false); | ||
|
||
<div className="block" data-cy="MainContent"> | ||
<p data-cy="NoSelectedUser">No user selected</p> | ||
useEffect(() => { | ||
client.get<User[]>('/users').then(setUsers); | ||
}, []); | ||
|
||
<Loader /> | ||
const handleActiveUser = (user: User) => { | ||
setActiveUser(null); | ||
setActivePost(null); | ||
setIsLoading(true); | ||
setIsError(false); | ||
|
||
<div | ||
className="notification is-danger" | ||
data-cy="PostsLoadingError" | ||
> | ||
Something went wrong! | ||
</div> | ||
setActiveUser(user); | ||
client | ||
.get<Post[]>(`/posts?userId=${user?.id}`) | ||
.then(setPosts) | ||
.catch(() => setIsError(true)) | ||
.finally(() => setIsLoading(false)); | ||
}; | ||
|
||
const handleActivePost = (post: Post | null) => { | ||
setIsCommentsLoading(true); | ||
setIsCommentsError(false); | ||
|
||
setActivePost(post); | ||
if (post !== null) { | ||
client | ||
.get<Comment[]>(`/comments?postId=${post?.id}`) | ||
.then(setComments) | ||
.catch(() => setIsCommentsError(true)) | ||
.finally(() => setIsCommentsLoading(false)); | ||
} | ||
}; | ||
|
||
const handleAddComment = (newComment: Comment, postId: number) => { | ||
const comment = { ...newComment, postId: postId }; | ||
|
||
<div className="notification is-warning" data-cy="NoPostsYet"> | ||
No posts yet | ||
return client | ||
.post<Comment>('/comments', comment) | ||
.then(commentFromServer => | ||
setComments(prev => [...(prev || []), commentFromServer]), | ||
) | ||
.catch(() => setIsCommentsError(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. Consider providing user feedback when an error occurs in |
||
|
||
const handleDeleteComment = (commentId: number) => { | ||
client | ||
.delete(`/comments/${commentId}`) | ||
.then(() => | ||
setComments(prevComments => | ||
prevComments.filter(prevComment => prevComment.id !== commentId), | ||
), | ||
) | ||
.catch(() => setIsCommentsError(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. Consider providing user feedback when an error occurs in |
||
|
||
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} | ||
activeUser={activeUser} | ||
handleActiveUser={handleActiveUser} | ||
/> | ||
</div> | ||
|
||
<PostsList /> | ||
<div className="block" data-cy="MainContent"> | ||
{!activeUser && ( | ||
<p data-cy="NoSelectedUser">No user selected</p> | ||
)} | ||
|
||
{isLoading && <Loader />} | ||
|
||
{isError && ( | ||
<div | ||
className="notification is-danger" | ||
data-cy="PostsLoadingError" | ||
> | ||
Something went wrong! | ||
</div> | ||
)} | ||
|
||
{!isLoading && !isError && posts.length === 0 && activeUser && ( | ||
<div className="notification is-warning" data-cy="NoPostsYet"> | ||
No posts yet | ||
</div> | ||
)} | ||
|
||
{posts.length > 0 && !isLoading && ( | ||
<PostsList | ||
posts={posts} | ||
activePost={activePost} | ||
handleActivePost={handleActivePost} | ||
/> | ||
)} | ||
</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 }, | ||
)} | ||
> | ||
<div className="tile is-child box is-success "> | ||
{activePost && ( | ||
<PostDetails | ||
activePost={activePost} | ||
comments={comments} | ||
isCommentsError={isCommentsError} | ||
isCommentsLoading={isCommentsLoading} | ||
handleDeleteComment={handleDeleteComment} | ||
handleAddComment={handleAddComment} | ||
/> | ||
)} | ||
</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.
Ensure that
activePost
is not null before accessing its properties to prevent potential runtime errors.