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

results #1211

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

results #1211

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
137 changes: 100 additions & 37 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,53 +8,116 @@ 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 { getPosts, getUsers } from './api/api';
import { Post } from './types/Post';

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 [posts, setPosts] = useState<Post[]>([]);
const [isError, setIsError] = useState(false);
const [loading, setLoading] = useState(false);
const [activeUser, setActiveUser] = useState<User | null>(null);
const [activePost, setActivePost] = useState<Post | null>(null);
const [showSideBar, setShowSideBar] = useState(false);

<div className="block" data-cy="MainContent">
<p data-cy="NoSelectedUser">No user selected</p>
useEffect(() => {
getUsers()
.then(setUsers)
.catch(() => setIsError(true));
Comment on lines +26 to +28

Choose a reason for hiding this comment

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

Consider providing more detailed error handling or logging in the catch block to help with debugging and understanding what went wrong.

Choose a reason for hiding this comment

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

The error handling here is minimal. Consider providing more detailed error messages or logging to help with debugging and user feedback.

}, []);

<Loader />
useEffect(() => {
if (activeUser) {
setLoading(true);

<div
className="notification is-danger"
data-cy="PostsLoadingError"
>
Something went wrong!
</div>
getPosts(activeUser)
.then(setPosts)
.catch(() => setIsError(true))

Choose a reason for hiding this comment

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

Similar to the previous comment, the error handling for the getPosts call is minimal. It would be beneficial to provide more detailed error messages or logging.

.finally(() => setLoading(false));
Comment on lines +35 to +38

Choose a reason for hiding this comment

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

Similar to the previous comment, consider enhancing error handling in this catch block for better debugging and user feedback.

}
}, [activeUser]);

Choose a reason for hiding this comment

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

Ensure that all necessary dependencies are included in the useEffect hook. Currently, it only depends on activeUser, which is correct, but make sure this is intentional and no other dependencies are needed.


const isSideBar =
activePost && showSideBar && activeUser?.id === activePost.userId;

Choose a reason for hiding this comment

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

The logic for determining isSideBar could be simplified or clarified. Ensure that this logic is correct and that it aligns with the intended functionality.


<div className="notification is-warning" data-cy="NoPostsYet">
No posts yet
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}
onActiveUser={setActiveUser}
activeUser={activeUser}
onShowSideBar={setShowSideBar}
onActivePost={setActivePost}
/>
</div>

<PostsList />
<div className="block" data-cy="MainContent">
{loading ? (
<Loader />
) : (
<>
{isError && (
<div
className="notification is-danger"
data-cy="PostsLoadingError"
>
Something went wrong!
</div>
)}

{!isError && !activeUser && (
<p data-cy="NoSelectedUser">No user selected</p>
)}

{!isError && activeUser && posts.length === 0 && (
<div
className="notification is-warning"
data-cy="NoPostsYet"
>
No posts yet
</div>
)}

{!isError && activeUser && posts.length > 0 && (
<PostsList
posts={posts}
onActivePost={setActivePost}
onShowSideBar={setShowSideBar}
/>
)}
</>
)}
</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': isSideBar,
},
)}
>
<div className="tile is-child box is-success ">
{activePost && (
<PostDetails post={activePost} key={activePost.id} />
)}
</div>
</div>
</div>
</div>
</div>
</main>
);
</main>
);
};
36 changes: 36 additions & 0 deletions src/api/api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { Comment } from '../types/Comment';
import { Post } from '../types/Post';
import { User } from '../types/User';
import { client } from '../utils/fetchClient';

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

Choose a reason for hiding this comment

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

Consider adding error handling for the API request to manage potential failures and provide feedback to the user.

Choose a reason for hiding this comment

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

Consider adding error handling for the getUsers function to manage potential API call failures.

};

export const getPosts = ({ id }: Pick<User, 'id'>) => {
return client.get<Post[]>(`/posts?userId=${id}`);

Choose a reason for hiding this comment

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

Similar to the previous comment, consider implementing error handling for this API request.

Choose a reason for hiding this comment

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

Similar to getUsers, add error handling for the getPosts function to handle any issues that may arise during the API call.

};

export const getComments = ({ id }: Pick<Post, 'id'>) => {
return client.get<Comment[]>(`/comments?postId=${id}`);

Choose a reason for hiding this comment

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

Consider adding error handling for this API request to manage potential errors.

Choose a reason for hiding this comment

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

Add error handling for the getComments function to ensure that any errors during the API call are properly managed.

};

export const addComment = ({
postId,
name,
email,
body,
}: Omit<Comment, 'id'>) => {
const data = {
postId,
name,
email,
body,
};

return client.post<Comment>('/comments', data);

Choose a reason for hiding this comment

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

Consider implementing error handling for the POST request to manage potential failures.

Choose a reason for hiding this comment

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

The addComment function should include error handling to manage any issues that occur during the POST request.

};

export const deleteComment = ({ id }: Pick<Comment, 'id'>) => {
return client.delete(`/comments/${id}`);

Choose a reason for hiding this comment

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

Consider adding error handling for the DELETE request to manage potential errors.

Choose a reason for hiding this comment

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

Include error handling for the deleteComment function to handle potential errors during the DELETE request.

};
Loading
Loading