-
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 task solution #1205
base: master
Are you sure you want to change the base?
add task solution #1205
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 @@ | ||
/* eslint-disable react-hooks/exhaustive-deps */ | ||
import classNames from 'classnames'; | ||
|
||
import 'bulma/css/bulma.css'; | ||
|
@@ -8,53 +9,151 @@ 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 { getUserPosts } from './api/ClientFunctions'; | ||
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 = () => { | ||
// #region states | ||
|
||
<div className="block" data-cy="MainContent"> | ||
<p data-cy="NoSelectedUser">No user selected</p> | ||
const [currUser, setCurrUser] = useState<User | null>(null); | ||
const [isDdActive, setIsDdActive] = useState(false); | ||
const [userPosts, setUserPosts] = useState<Post[]>([]); | ||
const [loadingPostsError, setLoadingPostsError] = useState(false); | ||
const [arePostsLoading, setArePostsLoading] = useState(false); | ||
const [isNoPosts, setIsNoPosts] = useState(false); | ||
const [openedPost, setOpenedPost] = useState<Post | null>(null); | ||
const [prevUser, setPrevUser] = useState<User | null>(null); | ||
const [doesFormExist, setDoesFormExist] = useState(false); | ||
|
||
<Loader /> | ||
// #endregion | ||
// #region handlers | ||
|
||
<div | ||
className="notification is-danger" | ||
data-cy="PostsLoadingError" | ||
> | ||
Something went wrong! | ||
</div> | ||
const sectionOnClickHandler = () => { | ||
if (isDdActive) { | ||
setIsDdActive(false); | ||
} | ||
}; | ||
|
||
// #endregion | ||
// #region useEffects | ||
|
||
useEffect(() => { | ||
if (currUser) { | ||
const { id } = currUser; | ||
|
||
setArePostsLoading(true); | ||
|
||
getUserPosts(id) | ||
.then(posts => { | ||
setUserPosts(posts); | ||
|
||
if (loadingPostsError) { | ||
setLoadingPostsError(false); | ||
} | ||
}) | ||
.catch(() => setLoadingPostsError(true)) | ||
.finally(() => setArePostsLoading(false)); | ||
} | ||
}, [currUser]); | ||
|
||
useEffect(() => { | ||
if (userPosts.length === 0 && currUser) { | ||
setIsNoPosts(true); | ||
} else if (userPosts.length > 0 && isNoPosts) { | ||
setIsNoPosts(false); | ||
} | ||
}, [currUser, isNoPosts, userPosts]); | ||
|
||
useEffect(() => { | ||
if (prevUser?.id !== currUser?.id) { | ||
setOpenedPost(null); | ||
} | ||
}, [prevUser, currUser]); | ||
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 |
||
|
||
useEffect(() => { | ||
if (doesFormExist) { | ||
setDoesFormExist(false); | ||
} | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [openedPost]); | ||
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-warning" data-cy="NoPostsYet"> | ||
No posts yet | ||
// #endregion | ||
// #region markups | ||
|
||
const loadingPostsErrorMarkup = ( | ||
<div className="notification is-danger" data-cy="PostsLoadingError"> | ||
Something went wrong! | ||
</div> | ||
); | ||
const noPostsMarkup = ( | ||
<div className="notification is-warning" data-cy="NoPostsYet"> | ||
No posts yet | ||
</div> | ||
); | ||
const postsList = ( | ||
<PostsList | ||
userPosts={userPosts} | ||
openedPost={openedPost} | ||
setOpenedPost={setOpenedPost} | ||
/> | ||
); | ||
|
||
// #endregion | ||
|
||
return ( | ||
<main className="section" onClick={sectionOnClickHandler}> | ||
<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 | ||
isDdActive={isDdActive} | ||
currUser={currUser} | ||
setPrevUser={setPrevUser} | ||
setCurrUser={setCurrUser} | ||
setIsDdActive={setIsDdActive} | ||
/> | ||
</div> | ||
|
||
<PostsList /> | ||
<div className="block" data-cy="MainContent"> | ||
{!currUser && <p data-cy="NoSelectedUser">No user selected</p>} | ||
|
||
{arePostsLoading ? ( | ||
<Loader /> | ||
) : ( | ||
(loadingPostsError && loadingPostsErrorMarkup) || | ||
(isNoPosts && noPostsMarkup) || | ||
(userPosts.length > 0 && postsList) | ||
)} | ||
</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 | ||
openedPost={openedPost} | ||
doesFormExist={doesFormExist} | ||
setDoesFormExist={setDoesFormExist} | ||
/> | ||
)} | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
</main> | ||
); | ||
</main> | ||
); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { client } from '../utils/fetchClient'; | ||
import { User } from '../types/User'; | ||
import { Post } from '../types/Post'; | ||
import { Comment } from '../types/Comment'; | ||
|
||
export const getUsers = () => { | ||
return client.get<User[]>('/users'); | ||
}; | ||
|
||
export const getUserPosts = (userId: number) => { | ||
return client.get<Post[]>(`/posts?userId=${userId}`); | ||
}; | ||
|
||
export const getPostComments = (postId: number) => { | ||
return client.get<Comment[]>(`/comments?postId=${postId}`); | ||
}; | ||
|
||
export const deleteComment = (id: number) => { | ||
return client.delete(`/comments/${id}`); | ||
}; | ||
|
||
export const postComment = ({ | ||
postId, | ||
name, | ||
email, | ||
body, | ||
}: Omit<Comment, 'id'>) => { | ||
return client.post<Comment>('/comments', { postId, name, email, body }); | ||
}; |
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
useEffect
dependency array includescurrUser
,isNoPosts
, anduserPosts
. IncludingcurrUser
andisNoPosts
might cause unnecessary re-renders. Consider revising the dependencies to only includeuserPosts
if the effect is solely dependent on the posts.