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 solution was added #926

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
69 changes: 47 additions & 22 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,61 +1,86 @@
import React from 'react';
import React, { useState, useContext } from 'react';
import 'bulma/bulma.sass';
import '@fortawesome/fontawesome-free/css/all.css';
import './App.scss';

import classNames from 'classnames';
import cn from 'classnames';
import { PostsList } from './components/PostsList';
import { PostDetails } from './components/PostDetails';
import { UserSelector } from './components/UserSelector';
import { Loader } from './components/Loader';
import { User } from './types/User';
import { PostContext } from './PostContext';

export const App: React.FC = () => {
const [selectedUser, setSelectedUser] = useState<User | null>(null);

const {
posts,
isPostLoading,
isPostLoadError,
selectedPost,
} = useContext(PostContext);

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}
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 />
{isPostLoading && <Loader />}

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

<div className="notification is-warning" data-cy="NoPostsYet">
No posts yet
</div>
{!isPostLoading
&& !isPostLoadError
&& posts.length === 0
&& selectedUser && (
<div className="notification is-warning" data-cy="NoPostsYet">
No posts yet
</div>
)}

<PostsList />
{selectedUser && !!posts.length && <PostsList />}
</div>
</div>
</div>

<div
data-cy="Sidebar"
className={classNames(
className={cn(
'tile',
'is-parent',
'is-8-desktop',
'Sidebar',
'Sidebar--open',
{ 'Sidebar--open': selectedPost },
)}
>
<div className="tile is-child box is-success ">
<PostDetails />
</div>
{!isPostLoadError && (
<div className="tile is-child box is-success">
<PostDetails />
</div>
)}
</div>
</div>
</div>
Expand Down
89 changes: 89 additions & 0 deletions src/CommentsContext.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import React, { useMemo, useState } from 'react';
import { Comment } from './types/Comment';
import { deleteComment } from './api/Comments';

type CommentsState = {
comments: Comment[],
setComments: (comments: Comment[]) => void,
isCommentLoading: boolean,
setIsCommentLoading: (value: boolean) => void,
isCommentLoadError: boolean,
setIsCommentLoadError: (value: boolean) => void,
isCommentDeleteError: boolean,
setIsCommentDeleteError: (value: boolean) => void,
isCommentUpdateError: boolean,
setIsCommentUpdateError: (value: boolean) => void,
isFormShown: boolean,
setIsFormShown: (value: boolean) => void,
hadnleCommentDelete: (id: number) => void;
};

export const CommentsContext = React.createContext<CommentsState>({
comments: [],
setComments: () => {},
isCommentLoading: false,
setIsCommentLoading: () => {},
isCommentLoadError: false,
setIsCommentLoadError: () => {},
isCommentDeleteError: false,
setIsCommentDeleteError: () => {},
isCommentUpdateError: false,
setIsCommentUpdateError: () => {},
isFormShown: false,
setIsFormShown: () => {},
hadnleCommentDelete: () => {},
});

interface Props {
children: React.ReactNode,
}

export const CommentsProvider: React.FC<Props> = ({ children }) => {
const [comments, setComments] = useState<Comment[]>([]);
const [isCommentLoading, setIsCommentLoading] = useState(false);
const [isCommentLoadError, setIsCommentLoadError] = useState(false);
const [isCommentDeleteError, setIsCommentDeleteError] = useState(false);
const [isCommentUpdateError, setIsCommentUpdateError] = useState(false);

const [isFormShown, setIsFormShown] = useState(false);

const hadnleCommentDelete = (id: number) => {
setIsCommentDeleteError(false);
deleteComment(id)
.then(() => setComments(
currentComments => currentComments.filter(comment => comment.id !== id),
))
.catch(() => {
setIsCommentDeleteError(true);
});
};

const value = useMemo(() => ({
comments,
setComments,
isCommentLoading,
setIsCommentLoading,
isCommentLoadError,
setIsCommentLoadError,
isCommentDeleteError,
setIsCommentDeleteError,
isCommentUpdateError,
setIsCommentUpdateError,
isFormShown,
setIsFormShown,
hadnleCommentDelete,
}), [
comments,
isCommentLoading,
isCommentLoadError,
isCommentDeleteError,
isCommentUpdateError,
isFormShown,
]);

return (
<CommentsContext.Provider value={value}>
{children}
</CommentsContext.Provider>
);
};
52 changes: 52 additions & 0 deletions src/PostContext.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import React, { useMemo, useState } from 'react';
import { Post } from './types/Post';

type PostState = {
posts: Post[],
setPosts: (comments: Post[]) => void,
selectedPost: Post | null,
setSelectedPost: (post: Post | null) => void,
isPostLoading: boolean,
setIsPostLoading: (value: boolean) => void,
isPostLoadError: boolean,
setIsPostLoadError: (value: boolean) => void,
};

export const PostContext = React.createContext<PostState>({
posts: [],
setPosts: () => {},
selectedPost: null,
setSelectedPost: () => {},
isPostLoading: false,
setIsPostLoading: () => {},
isPostLoadError: false,
setIsPostLoadError: () => {},
});

interface Props {
children: React.ReactNode,
}

export const PostProvider: React.FC<Props> = ({ children }) => {
const [posts, setPosts] = useState<Post[]>([]);
const [selectedPost, setSelectedPost] = useState<Post | null>(null);
const [isPostLoading, setIsPostLoading] = useState(false);
const [isPostLoadError, setIsPostLoadError] = useState(false);

const value = useMemo(() => ({
posts,
setPosts,
selectedPost,
setSelectedPost,
isPostLoading,
setIsPostLoading,
isPostLoadError,
setIsPostLoadError,
}), [posts, isPostLoading, isPostLoadError, selectedPost]);

return (
<PostContext.Provider value={value}>
{children}
</PostContext.Provider>
);
};
22 changes: 22 additions & 0 deletions src/api/Comments.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Comment, CommentData } from '../types/Comment';
import { client } from '../utils/fetchClient';

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

export const addComment = (
postId: number,
{ name, email, body }: CommentData,
) => {
return client.post<Comment>('/comments', {
postId,
name,
email,
body,
});
};

export const deleteComment = (commentId: number) => {
return client.delete(`/comments/${commentId}`);
};
6 changes: 6 additions & 0 deletions src/api/Posts.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { Post } from '../types/Post';
import { client } from '../utils/fetchClient';

export const getPosts = (userId: number) => {
return client.get<Post[]>(`/posts?userId=${userId}`);
};
6 changes: 6 additions & 0 deletions src/api/Users.tsx
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');
};
3 changes: 2 additions & 1 deletion src/components/Loader/Loader.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from 'react';
import './Loader.scss';

export const Loader = () => (
export const Loader: React.FC = () => (
<div className="Loader" data-cy="Loader">
<div className="Loader__content" />
</div>
Expand Down
Loading