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

Develop #1165

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open

Develop #1165

Show file tree
Hide file tree
Changes from 3 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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,5 @@ Install Prettier Extention and use this [VSCode settings](https://mate-academy.g
1. Implement comment deletion
- Delete the commnet immediately not waiting for the server response to improve the UX.
1. (*) Handle `Add` and `Delete` errors so the user can retry

[DEMO] (https://oleksandrakoshyk.github.io/react_dynamic-list-of-posts/)

Choose a reason for hiding this comment

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

Update this link in the PR description

Suggested change
[DEMO] (https://oleksandrakoshyk.github.io/react_dynamic-list-of-posts/)
[DEMO](https://oleksandrakoshyk.github.io/react_dynamic-list-of-posts/)

29 changes: 29 additions & 0 deletions src/App.scss
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
.section {
padding: 3rem;
margin: 0;
}

.container {
max-width: 1344;
}

.Sidebar {
overflow: hidden;
opacity: 0;
Expand All @@ -21,3 +30,23 @@
.message-body {
white-space: pre-line;
}

.is-ancestor {
display: flex;
align-items: stretch;
flex: 1 1 0px;

}

.is-parent {
display: flex;
align-items: stretch;
flex: 1 1 0px;
min-height: min-content;
padding: 12px;
width: 100%;
}

.is-child {
width: 100%;
}
132 changes: 94 additions & 38 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,57 +4,113 @@ import 'bulma/css/bulma.css';
import '@fortawesome/fontawesome-free/css/all.css';
import './App.scss';

import React, { useEffect, useState } from 'react';

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 { client } from './utils/fetchClient';
import { Post } from './types/Post';

export const App = () => (
<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 />
</div>
export const App: React.FC = () => {
const [users, setUsers] = useState<User[]>([]);
const [posts, setPosts] = useState<Post[]>([]);
const [selectedUser, setSelectedUser] = useState<User | null>(null);
const [isLoading, setIsLoading] = useState(false);
const [selectedPost, setSelectedPost] = useState<Post | null>(null);
const [error, setError] = useState(false);

<div className="block" data-cy="MainContent">
<p data-cy="NoSelectedUser">No user selected</p>
useEffect(() => {
client.get<User[]>('/users').then(res => {
setUsers(res);
});
}, []);

<Loader />
useEffect(() => {
if (selectedUser) {
setIsLoading(true);
setPosts([]);
setSelectedPost(null);
client
.get<Post[]>(`/posts?userId=${selectedUser.id}`)
.then(res => {
setPosts(res);
})
.catch(() => setError(true))
.finally(() => setIsLoading(false));
}
}, [selectedUser]);

<div
className="notification is-danger"
data-cy="PostsLoadingError"
>
Something went wrong!
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
users={users}
onChange={setSelectedUser}
selectedUser={selectedUser}
/>
</div>

<div className="notification is-warning" data-cy="NoPostsYet">
No posts yet
</div>
<div className="block" data-cy="MainContent">
{!selectedUser && (
<p data-cy="NoSelectedUser">No user selected</p>
)}

{isLoading && <Loader />}

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

<PostsList />
{!error &&
!!selectedUser &&
!isLoading &&
(!posts.length ? (
<div
className="notification is-warning"
data-cy="NoPostsYet"
>
No posts yet
</div>
) : (
<PostsList
posts={posts}
selectedPostId={selectedPost?.id}
onPostSelected={setSelectedPost}
/>
))}
</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 />
<div
data-cy="Sidebar"
className={classNames(
'tile',
'is-parent',
'is-8-desktop',
'Sidebar',
{ 'Sidebar--open': !!selectedPost },
)}
>
<div className="tile is-child box is-success ">
{!!selectedPost && (
<PostDetails post={selectedPost} key={selectedPost.id} />
)}
</div>
</div>
</div>
</div>
</div>
</main>
);
</main>
);
};
Loading
Loading