-
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
commit for chat #1183
base: master
Are you sure you want to change the base?
commit for chat #1183
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,4 +1,5 @@ | ||
import classNames from 'classnames'; | ||
import { useEffect, useMemo, useState } from 'react'; | ||
|
||
import 'bulma/css/bulma.css'; | ||
import '@fortawesome/fontawesome-free/css/all.css'; | ||
|
@@ -8,53 +9,139 @@ 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 { getUsersFromServer } from './api/users'; | ||
import { Post } from './types/Post'; | ||
import { getPostsFromServer } 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 [isDropDownOpen, setIsDropDownOpen] = useState(false); | ||
const [users, setUsers] = useState<User[]>([]); | ||
const [selectedUserId, setSelectedUserId] = useState<number | null>(null); | ||
const [isLoading, setIsLoading] = useState(false); | ||
const [posts, setPosts] = useState<Post[]>([]); | ||
const [errorMessage, setErrorMessage] = useState(''); | ||
const [notificationMessage, setNotificationMessage] = useState(''); | ||
const [activePostId, setActivePostId] = useState<number | null>(null); | ||
|
||
<div className="block" data-cy="MainContent"> | ||
<p data-cy="NoSelectedUser">No user selected</p> | ||
useEffect(() => { | ||
getUsersFromServer() | ||
.then(setUsers) | ||
.catch(() => {}); | ||
}, []); | ||
|
||
<Loader /> | ||
useEffect(() => { | ||
if (selectedUserId) { | ||
setIsLoading(true); | ||
setPosts([]); | ||
setErrorMessage(''); | ||
setNotificationMessage(''); | ||
setActivePostId(null); | ||
|
||
getPostsFromServer(selectedUserId) | ||
.then(fetchPosts => { | ||
setPosts(fetchPosts); | ||
if (fetchPosts.length === 0) { | ||
setNotificationMessage('No posts yet'); | ||
} | ||
}) | ||
.catch(() => { | ||
setErrorMessage('Something went wrong!'); | ||
Comment on lines
+48
to
+49
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 the catch block is incomplete. It seems like the string is not properly closed. Make sure to complete the error message string. |
||
}) | ||
.finally(() => { | ||
setIsLoading(false); | ||
}); | ||
} | ||
}, [selectedUserId]); | ||
|
||
const activePost = useMemo(() => { | ||
if (activePostId) { | ||
return posts.find(post => post.id === activePostId); | ||
} | ||
|
||
return null; | ||
}, [activePostId, posts]); | ||
|
||
useEffect(() => { | ||
const handleClickOutside = () => { | ||
setIsDropDownOpen(false); | ||
}; | ||
|
||
document.addEventListener('click', handleClickOutside); | ||
|
||
return () => { | ||
document.removeEventListener('click', handleClickOutside); | ||
}; | ||
}, []); | ||
|
||
<div | ||
className="notification is-danger" | ||
data-cy="PostsLoadingError" | ||
> | ||
Something went wrong! | ||
</div> | ||
|
||
<div className="notification is-warning" data-cy="NoPostsYet"> | ||
No posts yet | ||
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 | ||
isDropDownOpen={isDropDownOpen} | ||
users={users} | ||
setSelectedUserId={setSelectedUserId} | ||
selectedUserId={selectedUserId} | ||
setIsDropDownOpen={setIsDropDownOpen} | ||
/> | ||
</div> | ||
|
||
<PostsList /> | ||
<div className="block" data-cy="MainContent"> | ||
{!selectedUserId && ( | ||
<p data-cy="NoSelectedUser">No user selected</p> | ||
)} | ||
|
||
{isLoading && <Loader />} | ||
|
||
{errorMessage && !isLoading && ( | ||
<div | ||
className="notification is-danger" | ||
data-cy="PostsLoadingError" | ||
> | ||
{errorMessage} | ||
</div> | ||
)} | ||
|
||
{notificationMessage && !isLoading && !errorMessage && ( | ||
<div className="notification is-warning" data-cy="NoPostsYet"> | ||
{notificationMessage} | ||
</div> | ||
)} | ||
|
||
{posts.length > 0 && ( | ||
<PostsList | ||
posts={posts} | ||
activePostId={activePostId} | ||
setActivePostId={setActivePostId} | ||
/> | ||
)} | ||
</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': activePostId }, | ||
)} | ||
> | ||
{activePost && ( | ||
<div className="tile is-child box is-success "> | ||
<PostDetails activePost={activePost} /> | ||
</div> | ||
)} | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
</main> | ||
); | ||
</main> | ||
); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { Comment } from '../types/Comment'; | ||
import { client } from '../utils/fetchClient'; | ||
|
||
export const USER_ID = 1549; | ||
|
||
export const getCommentsFromServer = (postId: number) => { | ||
return client.get<Comment[]>(`/comments?postId=${postId}`); | ||
}; | ||
|
||
export const deleteComment = (commentId: number) => { | ||
return client.delete(`/comments/${commentId}`); | ||
}; | ||
|
||
export const createComment = ({ | ||
postId, | ||
name, | ||
email, | ||
body, | ||
}: Omit<Comment, 'id'>) => { | ||
return client.post<Comment>('/comments', { postId, name, email, body }); | ||
}; |
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 getPostsFromServer = (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 getUsersFromServer = () => { | ||
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. The endpoint |
||
}; |
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.
Consider adding error handling logic in the catch block of this useEffect hook. According to the checklist, you should use
try {..} catch
for error handling .