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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 4 additions & 0 deletions src/App.scss
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
iframe {
display: none;
}

.Sidebar {
overflow: hidden;
opacity: 0;
Expand Down
167 changes: 148 additions & 19 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, { useEffect, useState } from 'react';
import 'bulma/bulma.sass';
import '@fortawesome/fontawesome-free/css/all.css';
import './App.scss';
Expand All @@ -8,53 +8,182 @@ import { PostsList } from './components/PostsList';
import { PostDetails } from './components/PostDetails';
import { UserSelector } from './components/UserSelector';
import { Loader } from './components/Loader';
import { getUsers } from './services/user';
import { getUserPosts } from './services/posts';
import * as services from './services/comments';
import { User } from './types/User';
import { Post } from './types/Post';
import { Comment, CommentData } from './types/Comment';
import { ErrorType } from './types/ErrorType';

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

const [posts, setPosts] = useState<Post[]>([]);
const [selectedPost, setSelectedPost] = useState<Post | null>(null);

const [comments, setComments] = useState<Comment[]>([]);

const [errorMessage, setErrorMessage] = useState('');

const [isLoading, setIsLoading] = useState(false);

const loadUsers = async () => {
setErrorMessage('');
setPosts([]);
setSelectedPost(null);
setComments([]);
setIsLoading(true);
try {
const loadedUser = await getUsers();

setUsers(loadedUser);
} catch {
setErrorMessage(ErrorType.USERS);
} finally {
setIsLoading(false);
}
};

useEffect(() => {
loadUsers();
}, [selectedUser]);

const loadPosts = async (id: number) => {
setErrorMessage('');
setIsLoading(true);
try {
const loadedPosts = await getUserPosts(id);

setPosts(loadedPosts);
} catch {
setErrorMessage(ErrorType.POSTS);
} finally {
setIsLoading(false);
}
};

useEffect(() => {
if (selectedUser) {
loadPosts(selectedUser?.id);
}
}, [selectedUser]);

const loadComments = async (id: number) => {
setErrorMessage('');
setIsLoading(true);
try {
const loadedComments = await services.getPostComments(id);

setComments(loadedComments);
} catch {
setErrorMessage(ErrorType.COMMENTS);
} finally {
setIsLoading(false);
}
};

useEffect(() => {
if (selectedPost) {
loadComments(selectedPost?.id);
}
}, [selectedPost]);

const deleteComment = async (commentId: number) => {
setErrorMessage('');
setComments(currentComments => currentComments
.filter(({ id }) => id !== commentId));
try {
await services.deleteComment(commentId);
} catch (error) {
setComments(comments);
setErrorMessage(ErrorType.COMM_DEL);
throw error;
}
};

const addComment = async (comment: CommentData) => {
setErrorMessage('');
try {
const addedComment = await services.postComment(comment);

setComments(currComments => [...currComments, addedComment]);
} catch {
setErrorMessage(ErrorType.COMM_ADD);
}
};

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 && !isLoading && !errorMessage && (
<p data-cy="NoSelectedUser">
No user selected
</p>
)}

<Loader />
{isLoading && posts.length === 0 && <Loader />}

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

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

<PostsList />
{posts.length > 0 && (
<PostsList
posts={posts}
selectedPost={selectedPost}
setSelectedPost={setSelectedPost}
/>
)}
</div>
</div>
</div>

<div
data-cy="Sidebar"
className={classNames(
'tile',
'is-parent',
'is-8-desktop',
'Sidebar',
'Sidebar--open',
{ 'Sidebar--open': selectedPost },
)}
>
<div className="tile is-child box is-success ">
<PostDetails />
{selectedPost && (
<PostDetails
comments={comments}
isLoading={isLoading}
errorMessage={errorMessage}
selectedPost={selectedPost}
onCommentDelete={deleteComment}
onCommentAdd={addComment}
/>
)}
</div>
</div>
</div>
Expand Down
Loading
Loading