Skip to content

Commit

Permalink
working solution
Browse files Browse the repository at this point in the history
  • Loading branch information
Krykunov committed Dec 3, 2024
1 parent 611a1dc commit 9fe09d5
Show file tree
Hide file tree
Showing 14 changed files with 628 additions and 239 deletions.
110 changes: 73 additions & 37 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,53 +8,89 @@ import { PostsList } from './components/PostsList';
import { PostDetails } from './components/PostDetails';
import { UserSelector } from './components/UserSelector';
import { Loader } from './components/Loader';
import { useUsers } from './hooks/useUsers';
import { usePosts } from './hooks/usePosts';
import { useState } from 'react';

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 [isShowForm, setIsShowForm] = useState(false);

<div className="block" data-cy="MainContent">
<p data-cy="NoSelectedUser">No user selected</p>
const { users, currentUser, setCurrentUser } = useUsers();

<Loader />
const { posts, currentPost, setCurrentPost, isPostsLoading, isPostsError } =
usePosts(currentUser?.id || null);

<div
className="notification is-danger"
data-cy="PostsLoadingError"
>
Something went wrong!
</div>
const isShowPostsBlock = currentUser && !isPostsError && !isPostsLoading;

<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}
currentUser={currentUser}
setCurrentUser={setCurrentUser}
setCurrentPost={setCurrentPost}
/>
</div>

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

{isPostsLoading && <Loader />}

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

{isShowPostsBlock &&
(posts.length === 0 ? (
<div
className="notification is-warning"
data-cy="NoPostsYet"
>
No posts yet
</div>
) : (
<PostsList
posts={posts}
currentPost={currentPost}
setCurrentPost={setCurrentPost}
setIsShowForm={setIsShowForm}
/>
))}
</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': !!currentPost,
})}
>
{currentPost && (
<div className="tile is-child box is-success ">
<PostDetails
currentPost={currentPost}
isShowForm={isShowForm}
setIsShowForm={setIsShowForm}
/>
</div>
)}
</div>
</div>
</div>
</div>
</main>
);
</main>
);
};
19 changes: 19 additions & 0 deletions src/api/comments.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Comment } from '../types/Comment';
import { client } from '../utils/fetchClient';

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

export const createComment = ({
postId,
name,
email,
body,
}: Omit<Comment, 'id'>) => {
return client.post<Comment>(`/comments`, { postId, name, email, body });
};

export const deleteComment = (commentId: number) => {
return client.delete(`/comments/${commentId}`);
};
6 changes: 6 additions & 0 deletions src/api/posts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { Post } from '../types/Post';
import { client } from '../utils/fetchClient';

export const getPosts = (userId: number) => {
return client.get<Post[]>(`/posts?userId=${userId}`);
};
8 changes: 8 additions & 0 deletions src/api/users.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { User } from '../types/User';
import { client } from '../utils/fetchClient';

export const USER_ID = 1905;

export const getUsers = () => {
return client.get<User[]>(`/users`);
};
38 changes: 38 additions & 0 deletions src/components/Comment.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import React from 'react';
import type { Comment } from '../types/Comment';

type Props = {
comment: Comment;
deleteComment: (commentId: number) => void;
};

const CommentItem: React.FC<Props> = ({ comment, deleteComment }) => {
const handleDelete = () => {
deleteComment(comment.id);
};

return (
<article className="message is-small" data-cy="Comment">
<div className="message-header">
<a href={comment.email} data-cy="CommentAuthor">
{comment.name}
</a>
<button
data-cy="CommentDelete"
type="button"
className="delete is-small"
aria-label="delete"
onClick={handleDelete}
>
delete button
</button>
</div>

<div className="message-body" data-cy="CommentBody">
{comment.body}
</div>
</article>
);
};

export default CommentItem;
Loading

0 comments on commit 9fe09d5

Please sign in to comment.