-
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 #925
base: master
Are you sure you want to change the base?
add task solution #925
Changes from 1 commit
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,4 @@ | ||||||
import React from 'react'; | ||||||
import React, { useEffect, useState } from 'react'; | ||||||
import 'bulma/bulma.sass'; | ||||||
import '@fortawesome/fontawesome-free/css/all.css'; | ||||||
import './App.scss'; | ||||||
|
@@ -8,37 +8,115 @@ 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 { client } from './utils/fetchClient'; | ||||||
|
||||||
export const App: React.FC = () => { | ||||||
const [users, setUsers] = useState<User[]>([]); | ||||||
const [posts, setPosts] = useState<Post[]>([]); | ||||||
const [errorMessage, setErrorMessage] = useState(''); | ||||||
const [selectedUser, setSelectedUser] = useState<User | null>(null); | ||||||
const [selectedPost, setSelectedPost] = useState<Post | null>(null); | ||||||
const [isLoading, setIsLoading] = useState(false); | ||||||
const [isDropdownActive, setIsDropdownActive] = useState(false); | ||||||
const [isCommentButtonClicked, setIsCommentButtonClicked] = useState(false); | ||||||
|
||||||
const getAllUsers = () => { | ||||||
return client.get<User[]>('/users') | ||||||
.catch(() => setErrorMessage('Something went wrong!')); | ||||||
}; | ||||||
|
||||||
useEffect(() => { | ||||||
getAllUsers() | ||||||
.then(usersFromServer => { | ||||||
if (usersFromServer) { | ||||||
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. I guess this check is redundant here but not critical |
||||||
setUsers(usersFromServer); | ||||||
} | ||||||
}); | ||||||
}, []); | ||||||
|
||||||
const getUserPosts = () => { | ||||||
return client.get<Post[]>(`/posts?userId=${selectedUser?.id}`) | ||||||
.catch(() => setErrorMessage('Something went wrong!')); | ||||||
}; | ||||||
|
||||||
useEffect(() => { | ||||||
if (selectedUser) { | ||||||
setIsLoading(true); | ||||||
getUserPosts() | ||||||
.then(postsFromServer => { | ||||||
if (postsFromServer) { | ||||||
setPosts(postsFromServer); | ||||||
} | ||||||
}) | ||||||
.finally(() => setIsLoading(false)); | ||||||
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. would be nice to add |
||||||
} | ||||||
}, [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 /> | ||||||
<UserSelector | ||||||
users={users} | ||||||
isDropdownActive={isDropdownActive} | ||||||
setIsDropdownActive={setIsDropdownActive} | ||||||
selectedUser={selectedUser} | ||||||
setSelectedUser={setSelectedUser} | ||||||
setSelectedPost={setSelectedPost} | ||||||
/> | ||||||
</div> | ||||||
|
||||||
<div className="block" data-cy="MainContent"> | ||||||
<p data-cy="NoSelectedUser"> | ||||||
No user selected | ||||||
</p> | ||||||
{!selectedUser && ( | ||||||
<p data-cy="NoSelectedUser"> | ||||||
No user selected | ||||||
</p> | ||||||
)} | ||||||
|
||||||
<Loader /> | ||||||
{isLoading && ( | ||||||
<Loader /> | ||||||
)} | ||||||
|
||||||
<div | ||||||
className="notification is-danger" | ||||||
data-cy="PostsLoadingError" | ||||||
> | ||||||
Something went wrong! | ||||||
</div> | ||||||
{errorMessage && ( | ||||||
<div | ||||||
className="notification is-danger" | ||||||
data-cy="PostsLoadingError" | ||||||
> | ||||||
{errorMessage} | ||||||
</div> | ||||||
)} | ||||||
|
||||||
<div className="notification is-warning" data-cy="NoPostsYet"> | ||||||
No posts yet | ||||||
</div> | ||||||
{(!posts.length | ||||||
&& selectedUser | ||||||
&& !errorMessage | ||||||
&& !isLoading | ||||||
&& !isDropdownActive | ||||||
) && ( | ||||||
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. I'd move it into a variable above. Do you need |
||||||
<div | ||||||
className="notification is-warning" | ||||||
data-cy="NoPostsYet" | ||||||
> | ||||||
No posts yet | ||||||
</div> | ||||||
)} | ||||||
|
||||||
{(posts.length > 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.
Suggested change
|
||||||
&& selectedUser | ||||||
&& !isLoading | ||||||
) && ( | ||||||
<PostsList | ||||||
posts={posts} | ||||||
selectedPost={selectedPost} | ||||||
setSelectedPost={setSelectedPost} | ||||||
setIsCommentButtonClicked={setIsCommentButtonClicked} | ||||||
/> | ||||||
)} | ||||||
|
||||||
<PostsList /> | ||||||
</div> | ||||||
</div> | ||||||
</div> | ||||||
|
@@ -50,12 +128,19 @@ export const App: React.FC = () => { | |||||
'is-parent', | ||||||
'is-8-desktop', | ||||||
'Sidebar', | ||||||
'Sidebar--open', | ||||||
{ 'Sidebar--open': selectedPost !== null }, | ||||||
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.
Suggested change
|
||||||
)} | ||||||
> | ||||||
<div className="tile is-child box is-success "> | ||||||
<PostDetails /> | ||||||
</div> | ||||||
|
||||||
{selectedPost !== null && ( | ||||||
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.
Suggested change
|
||||||
<div className="tile is-child box is-success "> | ||||||
<PostDetails | ||||||
selectedPost={selectedPost} | ||||||
isCommentButtonClicked={isCommentButtonClicked} | ||||||
setIsCommentButtonClicked={setIsCommentButtonClicked} | ||||||
/> | ||||||
</div> | ||||||
)} | ||||||
</div> | ||||||
</div> | ||||||
</div> | ||||||
|
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.
Add a generic types everywher