-
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
pushing #946
base: master
Are you sure you want to change the base?
pushing #946
Changes from 2 commits
981356e
bb1478e
7bc24b3
297879d
245faee
098f03d
5bec440
8dd8836
487818e
14eef94
36144a3
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,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); | ||||||
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 | ||||||
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
|
||||||
&& !loader && ( | ||||||
<div | ||||||
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. also pls consider moving long ternaries to separate consts with consistent names 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. |
||||||
className="notification is-warning" | ||||||
data-cy="NoPostsYet" | ||||||
> | ||||||
No posts yet | ||||||
</div> | ||||||
)} | ||||||
|
||||||
<PostsList /> | ||||||
{selectedUser && userPosts.length > 0 && ( | ||||||
<PostsList | ||||||
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
|
||||||
userPosts={userPosts} | ||||||
selectedPost={selectedPost} | ||||||
setSelectedPost={setSelectedPost} | ||||||
setCreateNewComment={setCreateNewComment} | ||||||
/> | ||||||
)} | ||||||
</div> | ||||||
</div> | ||||||
</div> | ||||||
|
@@ -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> | ||||||
|
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}`); | ||
}; |
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.
Rename all boolean variables using tips