From b4d934ba5ea3c500f26773e8a7a457ddf055f4c3 Mon Sep 17 00:00:00 2001 From: Mariia Date: Tue, 22 Oct 2024 18:24:42 +0300 Subject: [PATCH] solution --- src/App.scss | 4 - src/App.tsx | 120 ++++++---- src/components/CommentsList/CommentsList.tsx | 37 +++ src/components/CommentsList/index.ts | 1 + src/components/NewCommentForm.tsx | 103 --------- .../NewCommentForm/NewCommentForm.tsx | 215 ++++++++++++++++++ src/components/NewCommentForm/index.ts | 1 + src/components/PostDetails.tsx | 107 --------- src/components/PostDetails/PostDetails.scss | 3 + src/components/PostDetails/PostDetails.tsx | 100 ++++++++ src/components/PostDetails/index.ts | 1 + src/components/PostsList.tsx | 86 ------- src/components/PostsList/PostsList.tsx | 63 +++++ src/components/PostsList/index.ts | 1 + src/components/UserSelector.tsx | 42 ---- src/components/UserSelector/UserSelector.tsx | 79 +++++++ src/components/UserSelector/index.ts | 1 + src/index.tsx | 4 + src/services/comments.service.ts | 15 ++ src/services/index.ts | 3 + src/services/posts.service.ts | 7 + src/services/users.service.ts | 6 + src/types/index.ts | 3 + src/utils/fetchClient.ts | 14 +- 24 files changed, 627 insertions(+), 389 deletions(-) create mode 100644 src/components/CommentsList/CommentsList.tsx create mode 100644 src/components/CommentsList/index.ts delete mode 100644 src/components/NewCommentForm.tsx create mode 100644 src/components/NewCommentForm/NewCommentForm.tsx create mode 100644 src/components/NewCommentForm/index.ts delete mode 100644 src/components/PostDetails.tsx create mode 100644 src/components/PostDetails/PostDetails.scss create mode 100644 src/components/PostDetails/PostDetails.tsx create mode 100644 src/components/PostDetails/index.ts delete mode 100644 src/components/PostsList.tsx create mode 100644 src/components/PostsList/PostsList.tsx create mode 100644 src/components/PostsList/index.ts delete mode 100644 src/components/UserSelector.tsx create mode 100644 src/components/UserSelector/UserSelector.tsx create mode 100644 src/components/UserSelector/index.ts create mode 100644 src/services/comments.service.ts create mode 100644 src/services/index.ts create mode 100644 src/services/posts.service.ts create mode 100644 src/services/users.service.ts create mode 100644 src/types/index.ts diff --git a/src/App.scss b/src/App.scss index 695435da4..1bb66237b 100644 --- a/src/App.scss +++ b/src/App.scss @@ -17,7 +17,3 @@ } } } - -.message-body { - white-space: pre-line; -} diff --git a/src/App.tsx b/src/App.tsx index 017957182..853d65a76 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,60 +1,98 @@ -import classNames from 'classnames'; - -import 'bulma/css/bulma.css'; -import '@fortawesome/fontawesome-free/css/all.css'; +import { useState } from 'react'; +import cn from 'classnames'; import './App.scss'; +import { User, Post } from './types'; +import { getPosts } from './services'; + import { PostsList } from './components/PostsList'; import { PostDetails } from './components/PostDetails'; import { UserSelector } from './components/UserSelector'; import { Loader } from './components/Loader'; -export const App = () => ( -
-
-
-
-
-
- -
+export const App = () => { + const [selectedUser, setSelectedUser] = useState(null); + const [posts, setPosts] = useState([]); + const [hasPostsLoadingError, setHasPostsLoadingError] = useState(false); + const [isLoading, setIsLoading] = useState(false); + const [selectedPost, setSelectedPost] = useState(null); -
-

No user selected

+ const showNoPostsYet = + !!selectedUser && !isLoading && !hasPostsLoadingError && !posts.length; - + const showPostList = + !!selectedUser && !isLoading && !hasPostsLoadingError && !!posts.length; -
- Something went wrong! -
+ const handleLoadPosts = (userId: User['id']) => { + setIsLoading(true); + setHasPostsLoadingError(false); + setSelectedPost(null); + + getPosts(userId) + .then(setPosts) + .catch(() => setHasPostsLoadingError(true)) + .finally(() => setIsLoading(false)); + }; -
- No posts yet + return ( +
+
+
+
+
+
+
- +
+ {!selectedUser && ( +

No user selected

+ )} + + {isLoading && } + + {hasPostsLoadingError && ( +
+ Something went wrong! +
+ )} + + {showNoPostsYet && ( +
+ No posts yet +
+ )} + + {showPostList && ( + + )} +
-
-
-
- +
+
+ {!!selectedPost && } +
-
-
-); +
+ ); +}; diff --git a/src/components/CommentsList/CommentsList.tsx b/src/components/CommentsList/CommentsList.tsx new file mode 100644 index 000000000..8d5371834 --- /dev/null +++ b/src/components/CommentsList/CommentsList.tsx @@ -0,0 +1,37 @@ +import { FC } from 'react'; +import { Comment } from '../../types'; + +interface Props { + comments: Comment[]; + onDeleteComment: (commentId: Comment['id']) => void; +} + +export const CommentsList: FC = ({ comments, onDeleteComment }) => { + return ( + <> +

Comments:

+ + {comments.map(({ id, email, name, body }) => ( +
+
+ + {name} + + +
+ +
+ {body} +
+
+ ))} + + ); +}; diff --git a/src/components/CommentsList/index.ts b/src/components/CommentsList/index.ts new file mode 100644 index 000000000..9e1d3ca5f --- /dev/null +++ b/src/components/CommentsList/index.ts @@ -0,0 +1 @@ +export * from './CommentsList'; diff --git a/src/components/NewCommentForm.tsx b/src/components/NewCommentForm.tsx deleted file mode 100644 index 73a8a0b45..000000000 --- a/src/components/NewCommentForm.tsx +++ /dev/null @@ -1,103 +0,0 @@ -import React from 'react'; - -export const NewCommentForm: React.FC = () => { - return ( -
-
- - -
- - - - - - - - - -
- -

- Name is required -

-
- -
- - -
- - - - - - - - - -
- -

- Email is required -

-
- -
- - -
-