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

add task solution #1193

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
12 changes: 7 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
},
"devDependencies": {
"@cypress/react18": "^2.0.1",
"@mate-academy/scripts": "^1.8.5",
"@mate-academy/scripts": "^1.9.12",
"@mate-academy/students-ts-config": "*",
"@mate-academy/stylelint-config": "*",
"@types/node": "^20.14.10",
Expand Down
132 changes: 93 additions & 39 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import classNames from 'classnames';
import { useEffect, useState } from 'react';
import { getUsers, getUserPosts } from './utils/fetchClient';
import { User } from './types/User';
import { Post } from './types/Post';
import cn from 'classnames';

import 'bulma/css/bulma.css';
import '@fortawesome/fontawesome-free/css/all.css';
Expand All @@ -9,52 +13,102 @@ import { PostDetails } from './components/PostDetails';
import { UserSelector } from './components/UserSelector';
import { Loader } from './components/Loader';

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: React.FC = () => {
const [users, setUsers] = useState<User[]>([]);
const [posts, setPosts] = useState<Post[]>([]);
const [currUser, setCurrUser] = useState<User | null>(null);
const [currPost, setCurrPost] = useState<Post | null>(null);
const [openModal, setOpenModal] = useState(false);
const [loading, setLoading] = useState(false);
const [error, setError] = useState(false);

<div className="block" data-cy="MainContent">
<p data-cy="NoSelectedUser">No user selected</p>
const searchUserPosts = (userId: number) => {
getUserPosts(userId)
.then(setPosts)
.catch(() => setError(true))
.finally(() => setLoading(false));

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 quite generic. Consider providing more detailed feedback or logging the error to help with debugging.

};
Comment on lines +26 to +29

Choose a reason for hiding this comment

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

The error handling in the searchUserPosts function is quite generic. Consider providing more informative feedback or logging for debugging purposes. This will help in understanding what went wrong during the API call.


<Loader />
useEffect(() => {
getUsers().then(setUsers);
}, []);

Choose a reason for hiding this comment

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

The getUsers function call does not handle errors. Consider adding a .catch() block to handle potential errors 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.

The getUsers function lacks error handling. It's important to handle errors here to provide feedback to users in case of failures. Consider adding a .catch() block to handle potential errors.


<div
className="notification is-danger"
data-cy="PostsLoadingError"
>
Something went wrong!
</div>
useEffect(() => {
if (currUser) {
setLoading(true);
searchUserPosts(currUser.id);
setCurrPost(null);
setOpenModal(false);
}
}, [currUser]);

<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}
currUser={currUser}
setCurrUser={setCurrUser}
/>
</div>

<PostsList />
<div className="block" data-cy="MainContent">
{!currUser && <p data-cy="NoSelectedUser">No user selected</p>}

{!loading &&
!error &&
currUser &&
(posts.length > 0 ? (
<PostsList
posts={posts}
currPost={currPost}
setOpenModal={setOpenModal}
setCurrPost={setCurrPost}
/>
) : (
<div
className="notification is-warning"
data-cy="NoPostsYet"
>
No posts yet
</div>
))}

{loading && <Loader />}

{error && (
<div
className="notification is-danger"
data-cy="PostsLoadingError"
>
Something went wrong!
</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={cn('tile', 'is-parent', 'is-8-desktop', 'Sidebar', {
'Sidebar--open': currPost,
})}
>
<div className="tile is-child box is-success ">
{currPost && (
<PostDetails
currPost={currPost}
openModal={openModal}
setOpenModal={setOpenModal}
/>
)}
</div>
</div>
</div>
</div>
</div>
</main>
);
</main>
);
};
Loading
Loading