-
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
solution #1184
base: master
Are you sure you want to change the base?
solution #1184
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,3 +1,4 @@ | ||
import { useState, useEffect } from 'react'; | ||
import classNames from 'classnames'; | ||
|
||
import 'bulma/css/bulma.css'; | ||
|
@@ -8,53 +9,102 @@ import { PostsList } from './components/PostsList'; | |
import { PostDetails } from './components/PostDetails'; | ||
import { UserSelector } from './components/UserSelector'; | ||
import { Loader } from './components/Loader'; | ||
import { User } from './types/User'; | ||
import { Post } from './types/Post'; | ||
import { getUserPosts } from './api/posts'; | ||
|
||
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 [selectedUser, setSelectedUser] = useState<User | null>(null); | ||
const [posts, setPosts] = useState<Post[]>([]); | ||
const [postsIsLoading, setPostsIsLoading] = useState(false); | ||
const [hasPostsError, setHasPostsError] = useState(''); | ||
|
||
<div className="block" data-cy="MainContent"> | ||
<p data-cy="NoSelectedUser">No user selected</p> | ||
const [chosenPost, setChosenPost] = useState<Post | null>(null); | ||
|
||
<Loader /> | ||
const hasNoPosts = | ||
selectedUser && !postsIsLoading && !hasPostsError && !posts.length; | ||
Comment on lines
+24
to
+25
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
Comment on lines
+24
to
+25
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 (!selectedUser) { | ||
return; | ||
} | ||
|
||
setPostsIsLoading(true); | ||
setHasPostsError(''); | ||
setPosts([]); | ||
setChosenPost(null); | ||
|
||
<div className="notification is-warning" data-cy="NoPostsYet"> | ||
No posts yet | ||
getUserPosts(selectedUser.id) | ||
.then(setPosts) | ||
.catch(() => setHasPostsError('Something went wrong!')) | ||
.finally(() => setPostsIsLoading(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 | ||
selectedUser={selectedUser} | ||
onSelect={setSelectedUser} | ||
onPost={setChosenPost} | ||
/> | ||
</div> | ||
|
||
<PostsList /> | ||
<div className="block" data-cy="MainContent"> | ||
{!selectedUser && ( | ||
<p data-cy="NoSelectedUser">No user selected</p> | ||
)} | ||
|
||
{postsIsLoading && <Loader />} | ||
|
||
{!postsIsLoading && hasPostsError && ( | ||
<div | ||
className="notification is-danger" | ||
data-cy="PostsLoadingError" | ||
> | ||
{hasPostsError} | ||
</div> | ||
)} | ||
|
||
{hasNoPosts && ( | ||
<div className="notification is-warning" data-cy="NoPostsYet"> | ||
No posts yet | ||
</div> | ||
)} | ||
|
||
{!postsIsLoading && posts.length > 0 && ( | ||
<PostsList | ||
posts={posts} | ||
chosenPost={chosenPost} | ||
onSelect={setChosenPost} | ||
/> | ||
)} | ||
</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': chosenPost, | ||
}, | ||
)} | ||
> | ||
<div className="tile is-child box is-success "> | ||
{chosenPost && <PostDetails post={chosenPost} />} | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
</main> | ||
); | ||
</main> | ||
); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { Comment } from '../types/Comment'; | ||
import { client } from '../utils/fetchClient'; | ||
|
||
export const getPostComments = (postId: number) => { | ||
return client.get<Comment[]>(`/comments?postId=${postId}`); | ||
}; | ||
|
||
export const postComment = ({ | ||
name, | ||
email, | ||
body, | ||
postId, | ||
}: Omit<Comment, 'id'>) => { | ||
return client.post<Comment>('/comments', { name, email, body, postId }); | ||
}; | ||
|
||
export const deleteComment = (commentId: number) => { | ||
return client.delete(`/comments/${commentId}`); | ||
}; |
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 getUserPosts = (userId: number) => { | ||
return client.get<Post[]>(`/posts?userId=${userId}`); | ||
}; |
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 comment
The reason will be displayed to describe this comment to others. Learn more.
The
hasPostsError
state is initialized as a string. Consider initializing it as a boolean or an object to allow for more flexible error handling in the future.