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

solution #868

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
90 changes: 57 additions & 33 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
import React from 'react';
import React, { useContext } 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 {
ErrorContext,
PostDataContext,
PostsContext,
UserContext,
} from './components/UserContext/UserContext';
import { Loader } from './components/Loader';
import { PostsList } from './components/PostsList';
import { PostDetails } from './components/PostDetails';

export const App: React.FC = () => {
const { isError, isLoadingPosts } = useContext(ErrorContext);
const { user } = useContext(UserContext);
const posts = useContext(PostsContext);
const postDetails = useContext(PostDataContext);

return (
<main className="section">
<div className="container">
Expand All @@ -21,42 +31,56 @@ export const App: React.FC = () => {
</div>

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

<Loader />

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

<div className="notification is-warning" data-cy="NoPostsYet">
No posts yet
</div>
{isLoadingPosts ? <Loader /> : (
<>
{(posts?.length > 0 && user && !isError) && (
<PostsList />
)}

<PostsList />
{(posts?.length === 0 && user && !isError) && (
<div
className="notification is-warning"
data-cy="NoPostsYet"
>
No posts yet
</div>
)}
</>
)}
</div>
</div>
</div>

<div
data-cy="Sidebar"
className={classNames(
'tile',
'is-parent',
'is-8-desktop',
'Sidebar',
'Sidebar--open',
)}
>
<div className="tile is-child box is-success ">
<PostDetails />
{postDetails.postData !== null && (
<div

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also need to destracture postDetails

data-cy="Sidebar"
className={classNames(
'tile',
'is-parent',
'is-8-desktop',
'Sidebar',
'Sidebar--open',
)}
>
<div className="tile is-child box is-success ">
<PostDetails />
</div>
</div>
</div>
)}
</div>
</div>
</main>
Expand Down
15 changes: 15 additions & 0 deletions src/api/comments.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Comment } from '../types/Comment';
import { Post } from '../types/Post';
import { client } from '../utils/fetchClient';

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

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

export const deleteComment = (commentId: number) => {
return client.delete(`/comments/${commentId}`);
};
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 = (user: User | null) => {
return client.get<Post[]>(`/posts?userId=${user?.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