diff --git a/src/App.tsx b/src/App.tsx index 017957182..6910d9f29 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,60 +1,107 @@ -import classNames from 'classnames'; - import 'bulma/css/bulma.css'; import '@fortawesome/fontawesome-free/css/all.css'; import './App.scss'; -import { PostsList } from './components/PostsList'; -import { PostDetails } from './components/PostDetails'; import { UserSelector } from './components/UserSelector'; +import { useEffect, useState } from 'react'; +import { User } from './types/User'; +import { getUsers } from './api/users'; import { Loader } from './components/Loader'; +import { PostsList } from './components/PostsList/PostsList'; +import { Post } from './types/Post'; +import { getPosts } from './api/posts'; +import { PostDetails } from './components/PostDetails'; +import classNames from 'classnames'; -export const App = () => ( -
-
-
-
-
-
- -
+export const App = () => { + const [users, setUsers] = useState([]); + const [posts, setPosts] = useState([]); + const [selectedPost, setSelectedPost] = useState(null); + const [notification, setNotification] = useState(''); + const [isLoading, setIsLoading] = useState(false); -
-

No user selected

+ // #region load + useEffect(() => { + getUsers().then(setUsers); + }, []); + // #endregion - + const handleLoadPosts = async (userId: number) => { + setIsLoading(true); + setPosts([]); + setNotification(''); + setSelectedPost(null); -
- Something went wrong! -
+ try { + const loadedPosts = await getPosts(userId); -
- No posts yet + if (loadedPosts.length === 0) { + setNotification('is-warning'); + } + + setPosts(loadedPosts); + } catch { + setNotification('is-danger'); + } finally { + setIsLoading(false); + } + }; + + return ( +
+
+
+
+
+
+
- +
+ {!isLoading && posts.length === 0 && !notification && ( +

No user selected

+ )} + {!isLoading && posts.length > 0 && ( + + )} + {isLoading && } + + {notification === 'is-danger' && ( +
+ Something went wrong! +
+ )} + + {notification === 'is-warning' && ( +
+ No posts yet +
+ )} +
-
-
-
- +
+ {selectedPost && ( +
+ +
+ )}
-
-
-); +
+ ); +}; diff --git a/src/api/comments.tsx b/src/api/comments.tsx new file mode 100644 index 000000000..f1de03a67 --- /dev/null +++ b/src/api/comments.tsx @@ -0,0 +1,14 @@ +import { Comment } from '../types/Comment'; +import { client } from '../utils/fetchClient'; + +export const getComments = (postId: number) => { + return client.get(`/comments?postId=${postId}`); +}; + +export const deleteComment = (commentId: number) => { + return client.delete(`/comments/${commentId}`); +}; + +export const addComment = ({ name, email, body, postId }: Comment) => { + return client.post(`/comments`, { name, email, body, postId }); +}; diff --git a/src/api/posts.tsx b/src/api/posts.tsx new file mode 100644 index 000000000..a6a1739ec --- /dev/null +++ b/src/api/posts.tsx @@ -0,0 +1,6 @@ +import { Post } from '../types/Post'; +import { client } from '../utils/fetchClient'; + +export const getPosts = (userId: number) => { + return client.get(`/posts?userId=${userId}`); +}; diff --git a/src/api/users.tsx b/src/api/users.tsx new file mode 100644 index 000000000..386bfacd5 --- /dev/null +++ b/src/api/users.tsx @@ -0,0 +1,6 @@ +import { User } from '../types/User'; +import { client } from '../utils/fetchClient'; + +export const getUsers = () => { + return client.get(`/users`); +}; 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 -

-
- -
- - -
-