Skip to content

Commit

Permalink
solution
Browse files Browse the repository at this point in the history
  • Loading branch information
syavaYki committed Sep 26, 2024
1 parent 8b7ce12 commit 10c268d
Show file tree
Hide file tree
Showing 11 changed files with 491 additions and 257 deletions.
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 { userClient } from './utils/userClient';
import { postClient } from './utils/postClient';
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 = () => {
//region States
const [users, setUsers] = useState<User[]>([]);
const [userSelected, setUserSelected] = useState<User | null>(null);
const [userSelPosts, setUserSelPosts] = useState<Post[]>([]);
const [openedPost, setOpendPost] = useState<Post | null>(null);
const [error, setError] = useState(false);
const [loading, setLoading] = useState(false);
//endregion

<div className="block" data-cy="MainContent">
<p data-cy="NoSelectedUser">No user selected</p>
//Init users
useEffect(() => {
setLoading(true);
userClient
.getAll()
.then(res => {
setUsers(res);
setError(false);
})
.catch(() => setError(true))
.finally(() => setLoading(false));
}, []);

<Loader />
// Update User Posts
useEffect(() => {
setOpendPost(null);

<div
className="notification is-danger"
data-cy="PostsLoadingError"
>
Something went wrong!
</div>
if (userSelected) {
setLoading(true);
postClient
.get(userSelected.id)
.then(res => {
setUserSelPosts(() => res);
setError(false);
})
.catch(() => setError(true))
.finally(() => {
setLoading(false);
});
}
}, [userSelected]);

const handleUserSelect = (user: User) => {
setUserSelected(user);
};

<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} onSelect={handleUserSelect} />
</div>

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

{loading && <Loader />}

{error && (
<div
className="notification is-danger"
data-cy="PostsLoadingError"
>
Something went wrong!
</div>
)}

{userSelected && !loading ? (
userSelPosts.length > 0 ? (
<PostsList posts={userSelPosts} onOpen={setOpendPost} />
) : (
<div
className="notification is-warning"
data-cy="NoPostsYet"
>
No posts yet
</div>
)
) : undefined}
</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--close': !openedPost, 'Sidebar--open': openedPost },
)}
>
<div className="tile is-child box is-success ">
<PostDetails post={openedPost} />
</div>
</div>
</div>
</div>
</div>
</main>
);
</main>
);
};
31 changes: 31 additions & 0 deletions src/components/Comment.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { Comment } from '../types/Comment';

type Props = {
comment: Comment;
onDelete: (id: number) => void;
};

export const CommentObj: React.FC<Props> = ({ comment, onDelete }) => {
return (
<article key={comment.id} className="message is-small" data-cy="Comment">
<div className="message-header">
<a href={`mailto:${comment.email}`} data-cy="CommentAuthor">
{comment.name}
</a>
<button
data-cy="CommentDelete"
type="button"
className="delete is-small"
aria-label="delete"
onClick={() => onDelete(comment.id)}
>
delete button
</button>
</div>

<div className="message-body" data-cy="CommentBody">
{comment.body}
</div>
</article>
);
};
29 changes: 29 additions & 0 deletions src/components/CommentList.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { Comment } from '../types/Comment';
import { CommentObj } from './Comment';

type Props = {
comments: Comment[];
onDelete: (id: number) => void;
};

export const CommentList: React.FC<Props> = ({ comments, onDelete }) => {
return (
<>
{comments && comments.length > 0 ? (
comments.map(comment => {
return (
<CommentObj
key={comment.id}
comment={comment}
onDelete={onDelete}
/>
);
})
) : (
<p className="title is-4" data-cy="NoCommentsMessage">
No comments yet
</p>
)}
</>
);
};
Loading

0 comments on commit 10c268d

Please sign in to comment.