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

add posts and comments #871

Open
wants to merge 3 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
81 changes: 62 additions & 19 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
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';
Expand All @@ -9,36 +10,78 @@ import { PostDetails } from './components/PostDetails';
import { UserSelector } from './components/UserSelector';
import { Loader } from './components/Loader';

import { getPosts } from './api/posts';

import { User } from './types/User';
import { Post } from './types/Post';

export const App: React.FC = () => {
const [selectedUser, setSelectedUser] = useState<User | null>(null);
const [isLoading, setIsLoading] = useState(false);
const [isError, setIsError] = useState(false);
const [usersPosts, setUsersPosts] = useState<Post[] | null>(null);
const [selectedPost, setSelectedPost] = useState<Post | null>(null);

useEffect(() => {
setIsError(false);

if (selectedUser) {
setIsLoading(true);

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

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

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

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

<div
className="notification is-danger"
data-cy="PostsLoadingError"
>
Something went wrong!
</div>
{!isLoading && !selectedUser && (
<p data-cy="NoSelectedUser">
No user selected
</p>
)}

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

<PostsList />
{!isLoading && !isError && usersPosts && (!usersPosts.length ? (
<div
className="notification is-warning"
data-cy="NoPostsYet"
>
No posts yet
</div>
) : (
<PostsList
posts={usersPosts}
onChoose={setSelectedPost}
selectedPost={selectedPost}
/>
))}
</div>
</div>
</div>
Expand All @@ -50,11 +93,11 @@ 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 post={selectedPost} />)}
</div>
</div>
</div>
Expand Down
24 changes: 24 additions & 0 deletions src/api/comments.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 { client } from '../utils/fetchClient';

type Parameters = {
name: string;
email: string;
body: string;
postId: number;
};

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

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

export const addComment = (
comment: Parameters,
) => {
return client.post<Comment>('/comments', comment);
};
7 changes: 7 additions & 0 deletions src/api/posts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Post } from '../types/Post';
import { User } from '../types/User';
import { client } from '../utils/fetchClient';

export const getPosts = ({ id }: User) => {
return client.get<Post[]>(`/posts?userId=${id}`);
};
6 changes: 6 additions & 0 deletions src/api/users.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { User } from '../types/User';
import { client } from '../utils/fetchClient';

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