-
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
solution 0.01` #1208
base: master
Are you sure you want to change the base?
solution 0.01` #1208
Changes from all commits
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 |
---|---|---|
|
@@ -8,53 +8,137 @@ import { PostsList } from './components/PostsList'; | |
import { PostDetails } from './components/PostDetails'; | ||
import { UserSelector } from './components/UserSelector'; | ||
import { Loader } from './components/Loader'; | ||
import { useEffect, useState } from 'react'; | ||
import { User } from './types/User'; | ||
import { Post } from './types/Post'; | ||
import { getData } from './api/fetch'; | ||
|
||
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 [userId, setUserId] = useState<number | null>(null); | ||
|
||
<div className="block" data-cy="MainContent"> | ||
<p data-cy="NoSelectedUser">No user selected</p> | ||
const [isLoading, setIsLoading] = useState(false); | ||
const [catchError, setCatchError] = useState(false); | ||
|
||
<Loader /> | ||
const [posts, setPosts] = useState<Post[]>([]); | ||
const [selectedPost, setSelectedPost] = useState<Post | null>(null); | ||
const [emptyPosts, setEmptyPosts] = useState(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. The 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. The |
||
|
||
<div | ||
className="notification is-danger" | ||
data-cy="PostsLoadingError" | ||
> | ||
Something went wrong! | ||
</div> | ||
const fetchUsers = async () => { | ||
try { | ||
setIsLoading(true); | ||
const url = '/users'; | ||
const currentUsers = await getData<User>(url); | ||
|
||
setUsers(currentUsers); | ||
} catch { | ||
setCatchError(true); | ||
} finally { | ||
setIsLoading(false); | ||
setTimeout(() => { | ||
setCatchError(false); | ||
}, 3000); | ||
} | ||
}; | ||
|
||
useEffect(() => { | ||
fetchUsers(); | ||
}, []); | ||
|
||
useEffect(() => { | ||
setPosts([]); | ||
setEmptyPosts(false); | ||
|
||
if (users && userId) { | ||
const fetchPosts = async () => { | ||
try { | ||
setIsLoading(true); | ||
const url = `/posts?userId=${userId}`; | ||
|
||
if (userId) { | ||
const currentPosts = await getData<Post>(url); | ||
|
||
if (currentPosts.length === 0) { | ||
setEmptyPosts(true); | ||
} | ||
|
||
setPosts(currentPosts); | ||
} | ||
} catch { | ||
setCatchError(true); | ||
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. Setting |
||
} finally { | ||
setIsLoading(false); | ||
} | ||
}; | ||
|
||
<div className="notification is-warning" data-cy="NoPostsYet"> | ||
No posts yet | ||
fetchPosts(); | ||
} | ||
}, [users, userId]); | ||
|
||
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} | ||
userId={userId} | ||
setUserId={setUserId} | ||
setSelectedPost={setSelectedPost} | ||
/> | ||
</div> | ||
|
||
<PostsList /> | ||
<div className="block" data-cy="MainContent"> | ||
{!userId && <p data-cy="NoSelectedUser">No user selected</p>} | ||
|
||
{isLoading && <Loader />} | ||
|
||
{catchError && ( | ||
<div | ||
className="notification is-danger" | ||
data-cy="PostsLoadingError" | ||
> | ||
Something went wrong! | ||
</div> | ||
)} | ||
|
||
{emptyPosts && posts.length === 0 && userId && ( | ||
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. The condition |
||
<div className="notification is-warning" data-cy="NoPostsYet"> | ||
No posts yet | ||
</div> | ||
)} | ||
|
||
{userId && posts.length > 0 && ( | ||
<PostsList | ||
posts={posts} | ||
selectedPost={selectedPost} | ||
setSelectedPost={setSelectedPost} | ||
/> | ||
)} | ||
</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 posts={posts} selectedPost={selectedPost} /> | ||
)} | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
</main> | ||
); | ||
</main> | ||
); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { client } from '../utils/fetchClient'; | ||
|
||
export const getData = async <T>(url: string) => { | ||
try { | ||
const data = await client.get<T[]>(url); | ||
|
||
return data; | ||
} catch (error) { | ||
throw new Error('Unable to load data'); | ||
} | ||
}; |
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.
Ensure that the function name
setSelectedPost
is correctly spelled and matches its usage throughout the code.