-
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
Dev #1194
base: master
Are you sure you want to change the base?
Dev #1194
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 |
---|---|---|
|
@@ -4,57 +4,131 @@ import 'bulma/css/bulma.css'; | |
import '@fortawesome/fontawesome-free/css/all.css'; | ||
import './App.scss'; | ||
|
||
import { useEffect, useState } from 'react'; | ||
|
||
import { PostsList } from './components/PostsList'; | ||
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> | ||
import { User } from './types/User'; | ||
import { Post } from './types/Post'; | ||
import { Errors } from './types/Errors'; | ||
|
||
<div className="block" data-cy="MainContent"> | ||
<p data-cy="NoSelectedUser">No user selected</p> | ||
import { getUsers } from './api/users'; | ||
import { getPosts } from './api/posts'; | ||
|
||
<Loader /> | ||
export const App = () => { | ||
const [usersList, setUsersList] = useState<User[]>([]); | ||
const [selectedUser, setSelectedUser] = useState<User | null>(null); | ||
const [isPostsLoading, setIsPostsLoading] = useState(false); | ||
const [currentError, setCurrentError] = useState<Errors | null>(null); | ||
const [userPosts, setUserPosts] = useState<Post[] | null>(null); | ||
const [openedPost, setOpenedPost] = useState<Post | null>(null); | ||
const [newCommentCreating, setNewCommentCreating] = useState<boolean>(false); | ||
|
||
<div | ||
className="notification is-danger" | ||
data-cy="PostsLoadingError" | ||
> | ||
Something went wrong! | ||
</div> | ||
useEffect(() => { | ||
getUsers() | ||
.then(setUsersList) | ||
.catch(() => setCurrentError(Errors.UsersLoading)); | ||
}, []); | ||
|
||
useEffect(() => { | ||
if (!selectedUser) { | ||
return; | ||
} | ||
|
||
<div className="notification is-warning" data-cy="NoPostsYet"> | ||
No posts yet | ||
setCurrentError(null); | ||
setUserPosts(null); | ||
setIsPostsLoading(true); | ||
|
||
getPosts(selectedUser.id) | ||
.then(response => { | ||
if (Array.isArray(response)) { | ||
setUserPosts(response); | ||
if (response.length === 0) { | ||
setCurrentError(Errors.NoPosts); | ||
} | ||
} else { | ||
setCurrentError(Errors.PostsLoading); | ||
} | ||
}) | ||
.catch(() => setCurrentError(Errors.PostsLoading)) | ||
.finally(() => { | ||
setIsPostsLoading(false); | ||
}); | ||
}, [selectedUser]); | ||
|
||
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={usersList} | ||
selectedUser={selectedUser} | ||
setSelectedUser={setSelectedUser} | ||
/> | ||
</div> | ||
|
||
<PostsList /> | ||
<div className="block" data-cy="MainContent"> | ||
{!selectedUser && ( | ||
<p data-cy="NoSelectedUser">No user selected</p> | ||
)} | ||
|
||
{isPostsLoading && !currentError && <Loader />} | ||
|
||
{currentError === Errors.PostsLoading && ( | ||
<div | ||
className="notification is-danger" | ||
data-cy="PostsLoadingError" | ||
> | ||
Something went wrong! | ||
</div> | ||
)} | ||
Comment on lines
+83
to
+90
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 message for post loading is displayed when |
||
|
||
{currentError === Errors.NoPosts && ( | ||
<div className="notification is-warning" data-cy="NoPostsYet"> | ||
No posts yet | ||
</div> | ||
)} | ||
|
||
{userPosts && userPosts.length > 0 && ( | ||
<PostsList | ||
posts={userPosts} | ||
openedPost={openedPost} | ||
setOpenedPost={setOpenedPost} | ||
setNewCommentCreating={setNewCommentCreating} | ||
/> | ||
)} | ||
</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': openedPost }, | ||
)} | ||
> | ||
<div className="tile is-child box is-success"> | ||
{openedPost && ( | ||
<PostDetails | ||
post={openedPost} | ||
newCommentCreating={newCommentCreating} | ||
setNewCommentCreating={setNewCommentCreating} | ||
/> | ||
)} | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
</main> | ||
); | ||
</main> | ||
); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { Comment } from '../types/Comment'; | ||
import { client } from '../utils/fetchClient'; | ||
|
||
export const getComments = (postId: number) => { | ||
return client.get<Comment[]>(`/comments?postId=${postId}`); | ||
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}`); | ||
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 |
||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { Comment } from '../types/Comment'; | ||
import { client } from '../utils/fetchClient'; | ||
|
||
export const newComment = (comment: Partial<Comment>) => { | ||
return client.post('/comments', comment); | ||
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 |
||
}; |
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}`); | ||
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 |
||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
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 |
||
}; |
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 loader is displayed when
isPostsLoading
is true, but it doesn't account forcurrentError
. This could lead to both the loader and an error message being displayed simultaneously. Consider updating the condition to ensure these states are mutually exclusive.