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 #1172

Open
wants to merge 3 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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,6 @@ Install Prettier Extention and use this [VSCode settings](https://mate-academy.g
1. Implement comment deletion
- Delete the commnet immediately not waiting for the server response to improve the UX.
1. (*) Handle `Add` and `Delete` errors so the user can retry
data.map((user) => {
id, name, email, phone;
})
9 changes: 5 additions & 4 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
12 changes: 12 additions & 0 deletions src/App.scss
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,15 @@
.message-body {
white-space: pre-line;
}

.tile.is-ancestor {
display: flex;
flex-wrap: wrap;
}

.tile.is-parent {
flex-grow: 1;
display: flex;
flex-direction: column;
padding: .75rem;
}
87 changes: 48 additions & 39 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,54 +7,63 @@ import './App.scss';
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';

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>

<div className="block" data-cy="MainContent">
<p data-cy="NoSelectedUser">No user selected</p>
export const App = () => {
const [selectedUser, setSelectedUser] = useState<User | null>(null);
const [openedPost, setOpenedPost] = useState<Post | null>(null);

<Loader />
useEffect(() => {
setOpenedPost(null);
}, [selectedUser]);

<div
className="notification is-danger"
data-cy="PostsLoadingError"
>
Something went wrong!
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
selectUser={setSelectedUser}
selectedUser={selectedUser}
/>
</div>

<div className="notification is-warning" data-cy="NoPostsYet">
No posts yet
<div className="block" data-cy="MainContent">
{selectedUser ? (
<PostsList
userID={selectedUser.id}
setOpenedPost={setOpenedPost}
openedPost={openedPost}
/>
) : (
<p data-cy="NoSelectedUser">No user selected</p>
)}
</div>

<PostsList />
</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': !!openedPost },
)}
>
{!!openedPost && (
<div className="tile is-child box is-success">
<PostDetails post={openedPost} />
</div>
)}
</div>
</div>
</div>
</div>
</main>
);
</main>
);
};
53 changes: 53 additions & 0 deletions src/components/CommentItem.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import React, { useState } from 'react';
import { Comment } from '../types/Comment';
import { client } from '../utils/fetchClient';

interface Props {
comment: Comment;
setComments: (
updateFn: (prevComments: Comment[] | null) => Comment[] | null,
) => void;
}

export const CommentItem: React.FC<Props> = ({
comment: { name, email, body, id },
setComments,
}) => {
const [isDeleted, setIsDeleted] = useState(false);
const deleteComment = () => {
setIsDeleted(true);
client.delete(`/comments/${id}`);
setComments(prevComments => {
if (!prevComments) {
return prevComments;
}

return prevComments.filter(comment => comment.id !== id);
});
};

if (isDeleted) {
return;
}

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

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