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

pushing #946

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
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
99 changes: 79 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,88 @@ 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 { getPostsData, getUsersData } from './api/posts';
import { Post } from './types/Post';

export const App: React.FC = () => {
const [users, setUsers] = useState<User[]>([]);
const [userPosts, setUserPosts] = useState<Post[]>([]);
const [selectedUser, setSelectedUser] = useState<User>();
const [selectedPost, setSelectedPost] = useState<Post>();
const [loader, setLoader] = useState<boolean>(false);
const [errorMessage, setErrorMessage] = useState<boolean>(false);

Choose a reason for hiding this comment

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

Rename all boolean variables using tips

Suggested change
const [loader, setLoader] = useState<boolean>(false);
const [isLoading, setIsLoading] = useState<boolean>(false);

const [createNewComment, setCreateNewComment] = useState<boolean>(false);

useEffect(() => {
getUsersData()
.then(setUsers)
.catch(() => setErrorMessage(true));
}, []);

useEffect(() => {
if (selectedUser) {
setLoader(true);
setErrorMessage(false);

getPostsData(selectedUser.id)
.then(setUserPosts)
.catch(() => setErrorMessage(true))
.finally(() => setLoader(false));
}
}, [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}
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 />
{loader && <Loader />}

<div
className="notification is-danger"
data-cy="PostsLoadingError"
>
Something went wrong!
</div>
{errorMessage && (
<div
className="notification is-danger"
data-cy="PostsLoadingError"
>
Something went wrong!
</div>
)}

<div className="notification is-warning" data-cy="NoPostsYet">
No posts yet
</div>
{selectedUser
&& userPosts.length === 0
&& !errorMessage

Choose a reason for hiding this comment

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

Suggested change
&& userPosts.length === 0
&& !userPosts.length

&& !loader && (
<div

Choose a reason for hiding this comment

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

also pls consider moving long ternaries to separate consts with consistent names

Copy link
Author

Choose a reason for hiding this comment

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

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

<PostsList />
{selectedUser && userPosts.length > 0 && (
<PostsList

Choose a reason for hiding this comment

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

Suggested change
{selectedUser && userPosts.length > 0 && (
{selectedUser && !!userPosts.length && (

userPosts={userPosts}
selectedPost={selectedPost}
setSelectedPost={setSelectedPost}
setCreateNewComment={setCreateNewComment}
/>
)}
</div>
</div>
</div>
Expand All @@ -50,12 +101,20 @@ export const App: React.FC = () => {
'is-parent',
'is-8-desktop',
'Sidebar',
'Sidebar--open',
{
'Sidebar--open': selectedPost,
},
)}
>
<div className="tile is-child box is-success ">
<PostDetails />
</div>
{selectedPost && (
<div className="tile is-child box is-success ">
<PostDetails
selectedPost={selectedPost}
createNewComment={createNewComment}
setCreateNewComment={setCreateNewComment}
/>
</div>
)}
</div>
</div>
</div>
Expand Down
24 changes: 24 additions & 0 deletions src/api/posts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Comment } from '../types/Comment';
import { Post } from '../types/Post';
import { User } from '../types/User';
import { client } from '../utils/fetchClient';

export const getPostsData = (userId: number) => {
return client.get<Post[]>(`/posts?userId=${userId}`);
};

export const getUsersData = () => {
return client.get<User[]>('/users');
};

export const getCommentsData = (postId: number) => {
return client.get<Comment[]>(`/comments?postId=${postId}`);
};

export const postCommentData = (data: Omit<Comment, 'id'>) => {
return client.post<Comment>('/comments', data);
};

export const deleteCommentData = (id: number) => {
return client.delete(`/comments/${id}`);
};
Loading
Loading