Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Develop #900

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions src/Api/Api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { Post } from '../types/Post';
import { Comment } from '../types/Comment';
import { client } from '../utils/fetchClient';

export const getUsers = () => {
return client.get('/users');
};

export const getPosts = (id?: number) => {
return client.get(`/posts?userId=${id}`);
};

export const getPost = (id?: number) => {
return client.get(`/posts/${id}`);
};

export const getComments = (id?: number) => {
return client.get(`/comments?postId=${id}`);
};

export const postData = (url: string, data: Post) => {
return client.post(url, data);
};

export const updateComments = (data: Comment[]) => {
return client.patch('/comments', data);
};

export const deleteData = (url: string) => {
return client.delete(url);
};

export const deleteComment = (id: number) => {
return client.delete(`/comments/${id}`);
};

export const addComment = (postId: number, data: Comment) => {
return client.post(`/comments?postId=${postId}`, data);
};
131 changes: 111 additions & 20 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, { useEffect, useState } from 'react';
import 'bulma/bulma.sass';
import '@fortawesome/fontawesome-free/css/all.css';
import './App.scss';
Expand All @@ -8,38 +8,123 @@ import { PostsList } from './components/PostsList';
import { PostDetails } from './components/PostDetails';
import { UserSelector } from './components/UserSelector';
import { Loader } from './components/Loader';
import { Post } from './types/Post';
import { User } from './types/User';
import { getPosts, getUsers } from './Api/Api';

export const App: React.FC = () => {
const [posts, setPosts] = useState<Post[]>([]);
const [isShowPostDetails, setIsShowPostDetails] = useState<boolean>(false);
const [isError, setIsError] = useState<boolean>(false);

const [users, setUsers] = useState<User[]>([]);

const [selectedUserId, setSelectedUserId] = useState<number>();
const [selectPostId, setSelectPostId] = useState<number>();

const [isLoading, setIsLoading] = useState<boolean>(false);

const getUsersFromServer = () => {
getUsers()
.then(data => {
const user = data as User[];

setUsers(user);
})
.catch(() => {
setIsError(true);
});
};

const selectUser = users.find(person => person.id === selectedUserId);

const getPostsFromSelectedUser = () => {
setIsLoading(true);
if (selectUser) {
getPosts(selectedUserId)
.then(data => {
const selectedPosts = data as Post[];

setPosts(selectedPosts);
setIsLoading(false);
})
.catch(error => {
// eslint-disable-next-line no-console
console.error('Error:', error);
setIsError(true);
setIsLoading(false);
});
}
};

useEffect(() => {
getUsersFromServer();
}, []);

useEffect(() => {
if (selectedUserId) {
getPostsFromSelectedUser();
}
}, [selectUser]);

return (
<main className="section">
<div className="container">
<div className="tile is-ancestor">
<div className="tile is-parent">
<div className="tile is-child box is-success">
<div className="block">
<UserSelector />
<UserSelector
selectUser={selectUser}
selectedUserId={selectedUserId}
setSelectedUserId={setSelectedUserId}
users={users}
/>
</div>

<div className="block" data-cy="MainContent">
<p data-cy="NoSelectedUser">
No user selected
</p>

<Loader />
{isError && (
<>
<div
className="notification is-danger"
data-cy="PostsLoadingError"
>
Something went wrong!
</div>
</>
)}

<div
className="notification is-danger"
data-cy="PostsLoadingError"
>
Something went wrong!
</div>
{isLoading && <Loader />}

<div className="notification is-warning" data-cy="NoPostsYet">
No posts yet
</div>
{!selectedUserId && !isLoading
? (
<p data-cy="NoSelectedUser">
No user selected
</p>
)
: (posts.length > 0
&& (
<PostsList
posts={posts}
isShowPostDetails={isShowPostDetails}
setIsShowPostDetails={setIsShowPostDetails}
selectedPostId={selectPostId}
setSelectPostId={setSelectPostId}
/>
)
)}
{(selectedUserId && posts.length === 0 && !isLoading)
&& (
<div
className="notification is-warning"
data-cy="NoPostsYet"
>
No posts yet
</div>
)}

<PostsList />
</div>

</div>
</div>

Expand All @@ -53,9 +138,15 @@ export const App: React.FC = () => {
'Sidebar--open',
)}
>
<div className="tile is-child box is-success ">
<PostDetails />
</div>
{isShowPostDetails && (
<div className="tile is-child box is-success ">
<PostDetails
isError={isError}
setIsError={setIsError}
selectedPostId={selectPostId}
/>
</div>
)}
</div>
</div>
</div>
Expand Down
Loading
Loading