-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8b7ce12
commit be9298c
Showing
7 changed files
with
479 additions
and
250 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,134 @@ | ||
import classNames from 'classnames'; | ||
|
||
import 'bulma/css/bulma.css'; | ||
import '@fortawesome/fontawesome-free/css/all.css'; | ||
import './App.scss'; | ||
|
||
import { useEffect, useState } from 'react'; | ||
import { User } from './types/User'; | ||
import { PostsList } from './components/PostsList'; | ||
import { PostDetails } from './components/PostDetails'; | ||
import { UserSelector } from './components/UserSelector'; | ||
import { Loader } from './components/Loader'; | ||
import { client } from './utils/fetchClient'; | ||
import { Post } from './types/Post'; | ||
import classNames from 'classnames'; | ||
import { PostDetails } from './components/PostDetails'; | ||
|
||
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 = () => { | ||
const [users, setUsers] = useState<User[]>([]); | ||
const [selectedUser, setSelectedUser] = useState<User | null>(null); | ||
const [posts, setPosts] = useState<Post[]>([]); | ||
const [isLoading, setIsLoading] = useState(false); | ||
const [error, setError] = useState(''); | ||
const [selectedPost, setSelectedPost] = useState<Post | null>(null); | ||
|
||
<div className="block" data-cy="MainContent"> | ||
<p data-cy="NoSelectedUser">No user selected</p> | ||
const loadUsers = async () => { | ||
setIsLoading(true); | ||
setError(''); | ||
try { | ||
const fetchedUsers = await client.get<User[]>('/users'); | ||
|
||
<Loader /> | ||
if (fetchedUsers.length === 0) { | ||
throw new Error('No users found'); | ||
} | ||
|
||
<div | ||
className="notification is-danger" | ||
data-cy="PostsLoadingError" | ||
> | ||
Something went wrong! | ||
</div> | ||
setUsers(fetchedUsers); | ||
} catch (err) { | ||
setError('Failed to load users'); | ||
} finally { | ||
setIsLoading(false); | ||
} | ||
}; | ||
|
||
const loadUserPosts = async (userId: number) => { | ||
setIsLoading(true); | ||
setError(''); | ||
try { | ||
const fetchedPosts = await client.get<Post[]>(`/posts?userId=${userId}`); | ||
|
||
<div className="notification is-warning" data-cy="NoPostsYet"> | ||
No posts yet | ||
setPosts(fetchedPosts); | ||
} catch (err) { | ||
setError('Failed to load posts'); | ||
} finally { | ||
setIsLoading(false); | ||
} | ||
}; | ||
|
||
useEffect(() => { | ||
if (selectedUser) { | ||
loadUserPosts(selectedUser.id); | ||
setSelectedPost(null); | ||
} | ||
}, [selectedUser]); | ||
|
||
useEffect(() => { | ||
loadUsers(); | ||
}, []); | ||
|
||
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 | ||
users={users} | ||
selectedUser={selectedUser} | ||
onSelectUser={setSelectedUser} | ||
/> | ||
</div> | ||
|
||
<PostsList /> | ||
<div className="block" data-cy="MainContent"> | ||
{!selectedUser && posts.length === 0 && !isLoading && ( | ||
<p data-cy="NoSelectedUser">No user selected</p> | ||
)} | ||
{isLoading && <Loader />} | ||
{error && ( | ||
<div | ||
data-cy="PostsLoadingError" | ||
className="notification is-danger" | ||
> | ||
{error} | ||
</div> | ||
)} | ||
|
||
{!isLoading && !!posts.length ? ( | ||
<PostsList | ||
posts={posts} | ||
openedPost={selectedPost} | ||
onOpen={setSelectedPost} | ||
/> | ||
) : ( | ||
!isLoading && | ||
!error && | ||
posts.length === 0 && | ||
selectedUser && ( | ||
<div | ||
className="notification is-warning" | ||
data-cy="NoPostsYet" | ||
> | ||
No posts yet | ||
</div> | ||
) | ||
)} | ||
</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': selectedPost }, | ||
)} | ||
> | ||
<div className="tile is-child box is-success"> | ||
{selectedPost && <PostDetails post={selectedPost} />} | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
</main> | ||
); | ||
</main> | ||
); | ||
}; |
Oops, something went wrong.