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

solution 0.01` #1208

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
158 changes: 121 additions & 37 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);

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.

const [emptyPosts, setEmptyPosts] = useState(false);

Choose a reason for hiding this comment

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

The emptyPosts state is not reset when posts are successfully fetched. Consider setting setEmptyPosts(false) after setting posts to ensure the UI reflects the correct state.

Choose a reason for hiding this comment

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

The emptyPosts state is not reset when posts are successfully fetched. Ensure to reset this state appropriately to avoid incorrect UI behavior.


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

Choose a reason for hiding this comment

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

Setting setCatchError(true) in the catch block will trigger an error notification, but consider revising this logic to preserve existing comments or provide more context-specific error handling.

} 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 && (

Choose a reason for hiding this comment

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

The condition emptyPosts && posts.length === 0 && userId might not be necessary if emptyPosts is managed correctly. Ensure that the logic for displaying this message aligns with the application's requirements.

<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>
);
};
11 changes: 11 additions & 0 deletions src/api/fetch.ts
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');
}
};
Loading
Loading