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 #1013

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
2 changes: 1 addition & 1 deletion cypress/integration/page.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ Cypress.on('fail', (e) => {

describe('', () => {
beforeEach(() => {
if (failed) Cypress.runner.stop();
// if (failed) Cypress.runner.stop();
});

describe('Page by default', () => {
Expand Down
168 changes: 146 additions & 22 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,61 +1,185 @@
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 cn from 'classnames';

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 fetchUsers from './api/users';
import * as fetchPosts from './api/posts';
import * as fetchComments from './api/comments';
import { User } from './types/User';
import { Post } from './types/Post';
import { Comment } from './types/Comment';
import { client } from './utils/fetchClient';

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 [isLoadingPosts, setIsLoadingPosts] = useState(false);

Choose a reason for hiding this comment

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

I'm curious why do we need selectedPost when we have openPostId? Isn't it about the same selected/opened post?

const [isLoadingComments, setIsLoadingComments] = useState(false);
const [isShowingComments, setIsShowingComments] = useState(false);
const [openPostId, setOpenPostId] = useState(0);
const [isShowingForm, setIsShowingForm] = useState(false);
const [errorPosts, setErrorPosts] = useState(false);
const [errorComments, setErrorComments] = useState(false);

useEffect(() => {
fetchUsers.getUsers('/users')
.then(res => setUsers(res));
}, []);

useEffect(() => {
if (selectedUser) {
setIsLoadingPosts(true);
setErrorPosts(false);

fetchPosts.getPosts(`/posts?userId=${selectedUser.id}`)
.then(res => setPosts(res))
.catch(() => setErrorPosts(true))
.finally(() => setIsLoadingPosts(false));
}
}, [selectedUser]);

const showPostDetails = (post: Post) => {
setIsLoadingComments(true);
if (post.id === openPostId) {
setOpenPostId(0);
setIsShowingComments(!isShowingComments);
}

if (!openPostId || openPostId !== post.id) {
setIsShowingComments(true);
setOpenPostId(post.id);
setErrorComments(false);

fetchComments.getComments(`/comments?postId=${post.id}`)
.then(res => {
setComments(res);
setIsLoadingComments(false);
})
.catch(() => setErrorComments(true));
}

setSelectedPost(post);
setIsShowingForm(false);
};

const updateSelectedUser = (user: User | null) => {
setSelectedUser(user);
setIsShowingComments(false);
setOpenPostId(0);
setSelectedPost(null);
};

const handleDeleteComment = (commentId: number) => {
const showingComments = comments
.filter(comment => comment.id !== commentId);

setComments(showingComments);

client.delete(`/comments/${commentId}`);
};

const createNewComment = (newComment: Comment) => {
setComments([...comments, newComment]);
};

const handleChangeErrorState = (el: boolean) => {
setErrorComments(el);
};

const isShowingLoader = !errorPosts && isLoadingPosts;
const isShowingNoUserSelect = !errorPosts && !selectedUser && !isLoadingPosts;
const isShowingPosts = !isLoadingPosts && selectedUser && !errorPosts;

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}
updateSelectedUser={updateSelectedUser}
/>
</div>

<div className="block" data-cy="MainContent">
<p data-cy="NoSelectedUser">
No user selected
</p>
{isShowingLoader
&& <Loader />}

<Loader />
{isShowingNoUserSelect && (
<p data-cy="NoSelectedUser">
No user selected
</p>
)}

<div
className="notification is-danger"
data-cy="PostsLoadingError"
>
Something went wrong!
</div>
{isShowingPosts && posts.length > 0
&& (
<PostsList
posts={posts}
showPostDetails={showPostDetails}
openPostId={openPostId}
/>
)}

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

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

<div
data-cy="Sidebar"
className={classNames(
className={cn(
'tile',
'is-parent',
'is-8-desktop',
'Sidebar',
'Sidebar--open',
{ 'Sidebar--open': isShowingComments },
)}
>
<div className="tile is-child box is-success ">
<PostDetails />
</div>
{selectedPost && (
<div className="tile is-child box is-success">
<PostDetails
comments={comments}
selectedPost={selectedPost}
isLoadingComments={isLoadingComments}
openPostId={openPostId}
addComment={createNewComment}
isShowingForm={isShowingForm}
changeIsShowingForm={setIsShowingForm}
onDelete={handleDeleteComment}
error={errorComments}
changeErrorState={handleChangeErrorState}
/>
</div>
)}
</div>
</div>
</div>
Expand Down
14 changes: 14 additions & 0 deletions src/api/comments.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Comment } from '../types/Comment';
import { client } from '../utils/fetchClient';

export const getComments = (url: string) => {
return client.get<Comment[]>(url);
};

export const addComment = (url: string, newComment: Comment) => {
return client.post<Comment>(url, newComment);
};

export const deleteComment = (url: string) => {
return client.delete(url);
};
6 changes: 6 additions & 0 deletions src/api/posts.ts
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 = (url: string) => {
return client.get<Post[]>(url);
};
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 = (url: string) => {
return client.get<User[]>(url);
};
Loading
Loading