diff --git a/src/App.tsx b/src/App.tsx index 73c324c44..c1aae74ff 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,3 +1,5 @@ +/* eslint-disable prettier/prettier */ +/* eslint-disable @typescript-eslint/indent */ import classNames from 'classnames'; import 'bulma/css/bulma.css'; @@ -18,6 +20,7 @@ import { Comment } from './types/Comment'; export const App = () => { const [users, setUsers] = useState([]); const [isUser, setIsUser] = useState(false); + const [userId, setUserId] = useState(null); const [posts, setPosts] = useState([]); const [comments, setComments] = useState([]); const [currentPostId, setCurrentPostId] = useState(0); @@ -38,31 +41,31 @@ export const App = () => { .finally(() => setLoading('')); }, []); - const getPostsByUserId = (id: number) => { + useEffect(() => { + if (userId) { setLoading('posts'); - setIsUser(true); - return servicesPosts - .getPosts(id) + servicesPosts.getPosts(userId) .then(res => { setPosts(res); setIsError(false); }) .catch(() => setIsError(true)) .finally(() => setLoading('')); - }; + } +}, [userId]); const getCommentsByPostId = (postId: number) => { setLoading('comments'); setCurrentPostId(postId); - servicesComments + return servicesComments .getComments(postId) - .catch(() => setIsError(true)) .then(res => { setComments(res); setIsError(false); }) + .catch(() => setIsError(true)) .finally(() => setLoading('')); }; @@ -80,7 +83,7 @@ export const App = () => { const deleteComment = (commentId: number) => { servicesComments .deleteComment(commentId) - .then(res => + .then(() => setComments(currentComments => currentComments.filter(comment => comment.id !== commentId), ), @@ -91,8 +94,6 @@ export const App = () => { return posts.find((post: Post) => post.id === currentPostId); }, [currentPostId, posts]); - console.log(isError); - return (
@@ -103,9 +104,8 @@ export const App = () => {
{loading === 'users' && } @@ -133,7 +133,7 @@ export const App = () => { data-cy="NoPostsYet" > No posts yet - + )} {isUser && diff --git a/src/components/NewCommentForm.tsx b/src/components/NewCommentForm.tsx index 84a5eb32a..265058a79 100644 --- a/src/components/NewCommentForm.tsx +++ b/src/components/NewCommentForm.tsx @@ -26,7 +26,7 @@ export const NewCommentForm: React.FC = ({ setBody(''); } - function resetAllFeilds() { + function resetAllFields() { setName(''); setEmail(''); setBody(''); @@ -193,7 +193,7 @@ export const NewCommentForm: React.FC = ({ diff --git a/src/components/PostDetails.tsx b/src/components/PostDetails.tsx index 03490b3a0..70c0f1389 100644 --- a/src/components/PostDetails.tsx +++ b/src/components/PostDetails.tsx @@ -1,4 +1,4 @@ -import React, { useState } from 'react'; +import React from 'react'; import { Loader } from './Loader'; import { NewCommentForm } from './NewCommentForm'; import { Comment } from '../types/Comment'; @@ -28,12 +28,11 @@ export const PostDetails: React.FC = ({ isError, }) => { if (!currentPost) { - return; + return null; } const handleOpenWritePost = () => { setIsWritePostButton(true); - console.log('true'); }; const handleDeleteComment = (postId: number) => { @@ -107,7 +106,6 @@ export const PostDetails: React.FC = ({ ) : ( diff --git a/src/components/UserSelector.tsx b/src/components/UserSelector.tsx index 97a0dc902..cb50eb111 100644 --- a/src/components/UserSelector.tsx +++ b/src/components/UserSelector.tsx @@ -5,47 +5,35 @@ import { User } from '../types/User'; type Props = { users: User[]; setIsUser: (a: boolean) => void; - getPostsByUserId: (a: number) => void; + setUserId: (a: number | null) => void; setOpenPostId: (a: null | number) => void; - isUser: boolean; }; export const UserSelector: React.FC = ({ users, setIsUser, - getPostsByUserId, + setUserId, setOpenPostId, - isUser, }) => { const [isActive, setIsActive] = useState(false); const [isActiveUserId, setIsActiveUserId] = useState(0); const [userName, setUserName] = useState(''); const handleClickDropdown = () => { - if (!isActive) { - setIsActive(true); - } else { - setIsActive(false); - } + setIsActive(!isActive); }; const handleShowUsersPosts = ( event: React.MouseEvent, userId: number, ) => { - - if (!userId) { - return; - } - event.preventDefault(); setOpenPostId(null); setIsActiveUserId(userId); setIsUser(true); - getPostsByUserId(userId); + setUserId(userId); setUserName(event.currentTarget.innerHTML); - setIsActive(false); };