diff --git a/src/App.tsx b/src/App.tsx index 017957182..738820f8c 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,46 +1,87 @@ -import classNames from 'classnames'; +//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 { 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'; -export const App = () => ( -
-
-
-
-
-
- -
+export const App = () => { + const [users, setUsers] = useState([]); + const [posts, setPosts] = useState([]); + const [selectedUser, setSelectedUser] = useState(null); + const [notification, setNotification] = useState(''); + const [isLoading, setIsLoading] = useState(false); -
-

No user selected

+ // #region load + useEffect(() => { + getUsers().then(setUsers); + }, []); - + useEffect(() => { + if (selectedUser) { + setIsLoading(true); + setPosts([]); + setNotification(''); -
- Something went wrong! -
+ getPosts(selectedUser.id) + .then(res => { + if (res.length === 0) { + setNotification('is-warning'); + } -
- No posts yet + setPosts(res); + }) + .catch(() => setNotification('is-danger')) + .finally(() => setIsLoading(false)); + } + }, [selectedUser]); + // #endregion + + 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 +
+ )} +
-
-
(
+
*/}
-
-
-); + + ); +}; 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/PostsList.tsx b/src/components/PostsList.tsx deleted file mode 100644 index cf90f04b0..000000000 --- a/src/components/PostsList.tsx +++ /dev/null @@ -1,86 +0,0 @@ -import React from 'react'; - -export const PostsList: React.FC = () => ( -
-

Posts:

- - - - - - - {/* eslint-disable-next-line jsx-a11y/control-has-associated-label */} - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#Title
17 - fugit voluptas sed molestias voluptatem provident - - -
18 - voluptate et itaque vero tempora molestiae - - -
19adipisci placeat illum aut reiciendis qui - -
20doloribus ad provident suscipit at - -
-
-); diff --git a/src/components/PostsList/PostsList.tsx b/src/components/PostsList/PostsList.tsx new file mode 100644 index 000000000..a75e94002 --- /dev/null +++ b/src/components/PostsList/PostsList.tsx @@ -0,0 +1,59 @@ +import classNames from 'classnames'; +import React, { useState } from 'react'; +import { Post } from '../../types/Post'; + +interface Props { + posts: Post[]; +} + +export const PostsList: React.FC = ({ posts }) => { + const [selectedPost, setSelectedPost] = useState(null); + + const handleOnClick = (post: Post) => { + setSelectedPost(prevPost => + prevPost && prevPost.id === post.id ? null : post, + ); + }; + + return ( +
+

Posts:

+ + + + + + + {/* eslint-disable-next-line jsx-a11y/control-has-associated-label */} + + + + + + {posts.map(post => ( + + + + + + + + ))} + +
#Title
{post.id}{post.title} + +
+
+ ); +}; diff --git a/src/components/PostsList/index.js b/src/components/PostsList/index.js new file mode 100644 index 000000000..41c5eee64 --- /dev/null +++ b/src/components/PostsList/index.js @@ -0,0 +1 @@ +export * from './PostsList'; diff --git a/src/components/UserSelector.tsx b/src/components/UserSelector.tsx deleted file mode 100644 index c89442841..000000000 --- a/src/components/UserSelector.tsx +++ /dev/null @@ -1,42 +0,0 @@ -import React from 'react'; - -export const UserSelector: React.FC = () => { - return ( -
-
-