-
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
dynamic list of posts completed #1188
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
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,110 @@ import 'bulma/css/bulma.css'; | |
import '@fortawesome/fontawesome-free/css/all.css'; | ||
import './App.scss'; | ||
|
||
import * as userService from './api/users'; | ||
import * as postService from './api/posts'; | ||
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 { Post } from './types/Post'; | ||
|
||
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 [selectedUser, setSelectedUser] = useState<User | null>(null); | ||
const [isPostsLoading, setIsPostsLoading] = useState(false); | ||
const [userPosts, setUserPosts] = useState<Post[]>([]); | ||
const [selectedPost, setSelectedPost] = useState<Post | null>(null); | ||
const [hasError, setHasError] = useState(false); | ||
|
||
<div className="block" data-cy="MainContent"> | ||
<p data-cy="NoSelectedUser">No user selected</p> | ||
useEffect(() => { | ||
userService | ||
.getUsers() | ||
.then(setUsers) | ||
.catch(() => setHasError(true)); | ||
}, []); | ||
|
||
<Loader /> | ||
useEffect(() => { | ||
setIsPostsLoading(true); | ||
postService | ||
.getUserPosts(selectedUser?.id || 0) | ||
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 |
||
.then(setUserPosts) | ||
.catch(() => setHasError(true)) | ||
.finally(() => setIsPostsLoading(false)); | ||
}, [selectedUser]); | ||
|
||
<div | ||
className="notification is-danger" | ||
data-cy="PostsLoadingError" | ||
> | ||
Something went wrong! | ||
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} | ||
selectedUser={selectedUser} | ||
setSelectedUser={setSelectedUser} | ||
/> | ||
</div> | ||
|
||
<div className="notification is-warning" data-cy="NoPostsYet"> | ||
No posts yet | ||
</div> | ||
{isPostsLoading ? ( | ||
<Loader /> | ||
) : ( | ||
<div className="block" data-cy="MainContent"> | ||
{!selectedUser && ( | ||
<p data-cy="NoSelectedUser">No user selected</p> | ||
)} | ||
|
||
{hasError && !isPostsLoading && ( | ||
<div | ||
className="notification is-danger" | ||
data-cy="PostsLoadingError" | ||
> | ||
Something went wrong! | ||
</div> | ||
)} | ||
|
||
<PostsList /> | ||
{selectedUser && | ||
!hasError && | ||
(!userPosts.length ? ( | ||
<div | ||
className="notification is-warning" | ||
data-cy="NoPostsYet" | ||
> | ||
No posts yet | ||
</div> | ||
) : ( | ||
<PostsList | ||
userPosts={userPosts} | ||
selectedPost={selectedPost} | ||
setSelectedPost={setSelectedPost} | ||
/> | ||
))} | ||
</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': selectedPost !== null }, | ||
)} | ||
> | ||
{selectedPost && ( | ||
<div className="tile is-child box is-success "> | ||
<PostDetails selestedPost={selectedPost} /> | ||
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. There is a typo in the |
||
</div> | ||
)} | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
</main> | ||
); | ||
</main> | ||
); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { Comment } from '../types/Comment'; | ||
import { client } from '../utils/fetchClient'; | ||
|
||
export const getComments = (postId: Comment['postId']) => { | ||
return client.get<Comment[]>(` | ||
/comments?postId=${postId}`); | ||
}; | ||
|
||
export const createComment = ({ | ||
postId, | ||
name, | ||
email, | ||
body, | ||
}: Omit<Comment, 'id'>) => { | ||
return client.post<Comment>(`/comments`, { postId, name, email, body }); | ||
}; | ||
|
||
export const deleteComment = (commId: Comment['id']) => { | ||
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 parameter name |
||
return client.delete(`/comments/${commId}`); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { Post } from '../types/Post'; | ||
import { client } from '../utils/fetchClient'; | ||
|
||
export const getUserPosts = (userId: Post['userId']) => { | ||
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'); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import React from 'react'; | ||
import { Comment } from '../types/Comment'; | ||
|
||
type Props = { | ||
comments: Comment[]; | ||
onDelete: (id: number) => void; | ||
}; | ||
|
||
export const CommentList: React.FC<Props> = ({ comments, onDelete }) => { | ||
return ( | ||
<> | ||
<p className="title is-4">Comments:</p> | ||
|
||
{comments.map(comm => ( | ||
<article className="message is-small" data-cy="Comment" key={comm.id}> | ||
<div className="message-header"> | ||
<a href={`mailto:${comm.email}`} data-cy="CommentAuthor"> | ||
{comm.name} | ||
</a> | ||
<button | ||
data-cy="CommentDelete" | ||
type="button" | ||
className="delete is-small" | ||
aria-label="delete" | ||
onClick={() => onDelete(comm.id)} | ||
/> | ||
</div> | ||
|
||
<div className="message-body" data-cy="CommentBody"> | ||
{comm.body} | ||
</div> | ||
</article> | ||
))} | ||
</> | ||
); | ||
}; |
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 media query combines
print
andscreen and (min-width: 769px)
. Ensure this is intentional, as it affects both print and screen media types.