Skip to content
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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
125 changes: 105 additions & 20 deletions src/App.tsx
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';
Expand All @@ -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('');

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

Suggested change
const [errorMessage, setErrorMessage] = useState('');
const [errorMessage, setErrorMessage] = useState<string>('');

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) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this check is redundant here
If the request is successful we will get an array (at least empty)

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));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would be nice to add .catch too here

}
}, [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
) && (

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd move it into a variable above. Do you need isDropdownActive here?

<div
className="notification is-warning"
data-cy="NoPostsYet"
>
No posts yet
</div>
)}

{(posts.length > 0

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
{(posts.length > 0
{(!!posts.length

&& selectedUser
&& !isLoading
) && (
<PostsList
posts={posts}
selectedPost={selectedPost}
setSelectedPost={setSelectedPost}
setIsCommentButtonClicked={setIsCommentButtonClicked}
/>
)}

<PostsList />
</div>
</div>
</div>
Expand All @@ -50,12 +128,19 @@ export const App: React.FC = () => {
'is-parent',
'is-8-desktop',
'Sidebar',
'Sidebar--open',
{ 'Sidebar--open': selectedPost !== null },

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
{ 'Sidebar--open': selectedPost !== null },
{ 'Sidebar--open': selectedPost },

)}
>
<div className="tile is-child box is-success ">
<PostDetails />
</div>

{selectedPost !== null && (

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
{selectedPost !== null && (
{selectedPost && (

<div className="tile is-child box is-success ">
<PostDetails
selectedPost={selectedPost}
isCommentButtonClicked={isCommentButtonClicked}
setIsCommentButtonClicked={setIsCommentButtonClicked}
/>
</div>
)}
</div>
</div>
</div>
Expand Down
Loading