Skip to content

Commit

Permalink
solution 0.02
Browse files Browse the repository at this point in the history
  • Loading branch information
StanislavKapytsia committed Dec 18, 2024
1 parent 823d0dd commit a798946
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 29 deletions.
7 changes: 4 additions & 3 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export const App = () => {
const [catchError, setCatchError] = useState(false);

const [posts, setPosts] = useState<Post[]>([]);
const [selectedPost, setSelectedpost] = useState<Post | null>(null);
const [selectedPost, setSelectedPost] = useState<Post | null>(null);
const [emptyPosts, setEmptyPosts] = useState(false);

const fetchUsers = async () => {
Expand All @@ -47,6 +47,7 @@ export const App = () => {

useEffect(() => {
setPosts([]);
setEmptyPosts(false);

if (users && userId) {
const fetchPosts = async () => {
Expand Down Expand Up @@ -85,7 +86,7 @@ export const App = () => {
users={users}
userId={userId}
setUserId={setUserId}
setSelectedpost={setSelectedpost}
setSelectedPost={setSelectedPost}
/>
</div>

Expand Down Expand Up @@ -113,7 +114,7 @@ export const App = () => {
<PostsList
posts={posts}
selectedPost={selectedPost}
setSelectedpost={setSelectedpost}
setSelectedPost={setSelectedPost}
/>
)}
</div>
Expand Down
15 changes: 5 additions & 10 deletions src/components/NewCommentForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export const NewCommentForm: React.FC<Props> = ({
return doesEmpty;
};

const addCommetns = async () => {
const addComments = async () => {
const newData = {
postId: selectedPost.id,
name: userName,
Expand Down Expand Up @@ -112,15 +112,10 @@ export const NewCommentForm: React.FC<Props> = ({
{ value: commentText, setError: setCommentTextError },
];

const hasEmptyForm =
!validateInput(userName) &&
!validateInput(userMail) &&
!validateInput(commentText);
const allEmpty = inputs.every(input => !validateInput(input.value));

if (hasEmptyForm) {
setUserNameError(true);
setUserMailError(true);
setCommentTextError(true);
if (allEmpty) {
inputs.forEach(input => input.setError(true));

return;
}
Expand All @@ -139,7 +134,7 @@ export const NewCommentForm: React.FC<Props> = ({
return;
}

addCommetns();
addComments();
};

return (
Expand Down
12 changes: 3 additions & 9 deletions src/components/PostDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ export const PostDetails: React.FC<Props> = ({ posts, selectedPost }) => {
};

const handleDelete = async (id: number) => {
const updatedCommnets = comments.filter(comment => comment.id !== id);
const updatedComments = comments.filter(comment => comment.id !== id);

setComments(updatedCommnets);
setComments(updatedComments);

try {
await client.delete(`/comments/${id}`);
Expand All @@ -64,7 +64,7 @@ export const PostDetails: React.FC<Props> = ({ posts, selectedPost }) => {

if (deletedComment) {
setTimeout(() => {
setComments([...updatedCommnets, deletedComment]);
setComments([...updatedComments, deletedComment]);
setLoadingError(true);

setTimeout(() => {
Expand Down Expand Up @@ -93,12 +93,6 @@ export const PostDetails: React.FC<Props> = ({ posts, selectedPost }) => {
</div>
)}

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

{comments.length === 0 && !loading && !loadingError && (
<p className="title is-4" data-cy="NoCommentsMessage">
No comments yet
Expand Down
8 changes: 4 additions & 4 deletions src/components/PostsList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@ import classNames from 'classnames';
interface Props {
posts: Post[];
selectedPost: Post | null;
setSelectedpost: React.Dispatch<React.SetStateAction<Post | null>>;
setSelectedPost: React.Dispatch<React.SetStateAction<Post | null>>;
}

export const PostsList: React.FC<Props> = ({
posts,
selectedPost,
setSelectedpost,
setSelectedPost,
}) => {
const handleButtonSwitch = (currentPost: Post) => {
if (selectedPost && selectedPost.id === currentPost.id) {
setSelectedpost(null);
setSelectedPost(null);
} else {
setSelectedpost(currentPost);
setSelectedPost(currentPost);
}
};

Expand Down
6 changes: 3 additions & 3 deletions src/components/UserSelector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ interface Props {
users: User[];
userId: number | null;
setUserId: React.Dispatch<React.SetStateAction<number | null>>;
setSelectedpost: React.Dispatch<React.SetStateAction<Post | null>>;
setSelectedPost: React.Dispatch<React.SetStateAction<Post | null>>;
}

export const UserSelector: React.FC<Props> = ({
users,
userId,
setUserId,
setSelectedpost,
setSelectedPost,
}) => {
const [showList, setShowList] = useState(false);
const [userName, setUserName] = useState('Choose a user');
Expand Down Expand Up @@ -71,7 +71,7 @@ export const UserSelector: React.FC<Props> = ({
setUserId(user.id);
setUserName(user.name);
setShowList(false);
setSelectedpost(null);
setSelectedPost(null);
}}
>
{user.name}
Expand Down

0 comments on commit a798946

Please sign in to comment.