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

task done #877

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
131 changes: 112 additions & 19 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,44 +1,127 @@
import React from 'react';
import React, { useEffect, useState } from 'react';
import 'bulma/bulma.sass';
import '@fortawesome/fontawesome-free/css/all.css';
import './App.scss';

import classNames from 'classnames';

import { PostsList } from './components/PostsList';
import { PostDetails } from './components/PostDetails';
import { UserSelector } from './components/UserSelector';
import { Loader } from './components/Loader';
import * as postService from './api/api';
import { User } from './types/User';
import { Post } from './types/Post';
import { Comment } from './types/Comment';

export const App: React.FC = () => {
const [users, setUsers] = useState<User[]>([]);
const [posts, setPosts] = useState<Post[]>([]);
const [comments, setComments] = useState<Comment[]>([]);
const [selectedUser, setSelectedUser] = useState<User | null>(null);
const [selectedPost, setSelectedPost] = useState<Post | null>(null);
const [isLoading, setIsLoading] = useState(false);
const [isError, setIsError] = useState(false);
const [isWriting, setIsWriting] = useState(false);

useEffect(() => {
postService.getUsers()
.then(setUsers);
}, []);

useEffect(() => {
setSelectedPost(null);

if (selectedUser) {
setIsLoading(true);

postService.getPosts(selectedUser.id)
.then(setPosts)
.catch(() => setIsError(true))
.finally(() => setIsLoading(false));
}
}, [selectedUser]);

const getComments = (postId: number) => {
return postService.getComments(postId)
.then(setComments);
};

const deleteComment = (id: number) => {
setComments(prevComments => prevComments
.filter(comment => comment.id !== id));

return postService.deleteComment(id);
};

const addComment = ({
postId,
name,
email,
body,
}: Omit<Comment, 'id'>) => {
const maxId = Math.max(0, ...comments.map(comment => comment.id));

const id = maxId + 1;

return postService.createComment({
id,
postId,
name,
email,
body,
})
.then(newComment => setComments(prevCmnts => [...prevCmnts, newComment]));
};

const validatePosts = selectedUser && !isLoading && !isError;

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
users={users}
selectedUser={selectedUser}
setSelectedUser={setSelectedUser}
/>
</div>

<div className="block" data-cy="MainContent">
<p data-cy="NoSelectedUser">
No user selected
</p>
{!selectedUser && (
<p data-cy="NoSelectedUser">
No user selected
</p>
)}

<Loader />
{isLoading && <Loader />}

<div
className="notification is-danger"
data-cy="PostsLoadingError"
>
Something went wrong!
</div>
{validatePosts && posts.length === 0 && (
<div className="notification is-warning" data-cy="NoPostsYet">
No posts yet
</div>
)}

<div className="notification is-warning" data-cy="NoPostsYet">
No posts yet
</div>
{validatePosts && posts.length > 0 && (
<PostsList
posts={posts}
setSelectedPost={setSelectedPost}
selectedPost={selectedPost}
isWriting={isWriting}
setIsWriting={setIsWriting}
/>
)}

<PostsList />
{isError && !isLoading && (
<div
className="notification is-danger"
data-cy="PostsLoadingError"
>
Something went wrong!
</div>
)}
</div>
</div>
</div>
Expand All @@ -50,11 +133,21 @@ export const App: React.FC = () => {
'is-parent',
'is-8-desktop',
'Sidebar',
'Sidebar--open',
{ 'Sidebar--open': selectedPost },
)}
>
<div className="tile is-child box is-success ">
<PostDetails />
{selectedPost && (
<PostDetails
selectedPost={selectedPost}
comments={comments}
deleteComment={deleteComment}
isWriting={isWriting}
setIsWriting={setIsWriting}
addComment={addComment}
getComments={getComments}
/>
)}
</div>
</div>
</div>
Expand Down
24 changes: 24 additions & 0 deletions src/api/api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Comment } from '../types/Comment';
import { Post } from '../types/Post';
import { User } from '../types/User';
import { client } from '../utils/fetchClient';

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

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

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

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

export const createComment = (comment: Comment) => {
return client.post<Comment>('/comments', comment);
};
Loading
Loading