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

v1 #1199

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

v1 #1199

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
13 changes: 13 additions & 0 deletions src/App.scss
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
.tile {
&.is-ancestor {
display: flex;
}

&.is-parent {
display: flex;
flex-direction: column;
flex-grow: 1;
padding: 15px;
}
}

.Sidebar {
overflow: hidden;
opacity: 0;
Expand Down
137 changes: 100 additions & 37 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,53 +8,116 @@ import { PostsList } from './components/PostsList';
import { PostDetails } from './components/PostDetails';
import { UserSelector } from './components/UserSelector';
import { Loader } from './components/Loader';
import { useEffect, useState } from 'react';
import { getUsers } from './api/users';
import { User } from './types/User';
import { Post } from './types/Post';
import { getUserPosts } from './api/posts';

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 = () => {
const [loading, setLoading] = useState(false);
const [users, setUsers] = useState<User[]>([]);
const [selectedUser, setSelectedUser] = useState<User | null>(null);
const [posts, setPosts] = useState<Post[]>([]);
const [error, setError] = useState('');
const [postsFetched, setPostsFetched] = useState(false);
const [selectedPost, setSelectedPost] = useState<Post | null>(null);

<div className="block" data-cy="MainContent">
<p data-cy="NoSelectedUser">No user selected</p>
useEffect(() => {
getUsers().then(fetchedUsers => setUsers(fetchedUsers));

Choose a reason for hiding this comment

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

Suggested change
getUsers().then(fetchedUsers => setUsers(fetchedUsers));
getUsers()
.then(setUsers);

}, []);

<Loader />
const handleOpenPosts = (userId: number) => {
setLoading(true);
setError('');
setPostsFetched(false);

<div
className="notification is-danger"
data-cy="PostsLoadingError"
>
Something went wrong!
</div>
getUserPosts(userId)
.then(fetchedPosts => {
setPosts(fetchedPosts);
setPostsFetched(true);
})
.catch(() => {
setError('Something went wrong!');
})
.finally(() => {
setLoading(false);
});
};

useEffect(() => {
if (selectedUser) {
handleOpenPosts(selectedUser.id);
setSelectedPost(null);
}
}, [selectedUser]);

<div className="notification is-warning" data-cy="NoPostsYet">
No posts yet
const isPostEmpty =
selectedUser && !loading && posts.length === 0 && !error && postsFetched;

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}
selectedUser={selectedUser}
onSelectUser={setSelectedUser}
/>
</div>

<PostsList />
<div className="block" data-cy="MainContent">
{!selectedUser && (
<p data-cy="NoSelectedUser">No user selected</p>
)}

{loading && <Loader />}

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

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

{!error && selectedUser && posts.length > 0 && !loading && (

Choose a reason for hiding this comment

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

Suggested change
{!error && selectedUser && posts.length > 0 && !loading && (
{!error && selectedUser && !!posts.length && !loading && (

<PostsList
posts={posts}
selectedPost={selectedPost}
setSelectedPost={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} />}
</div>
</div>
</div>
</div>
</div>
</main>
);
</main>
);
};
24 changes: 24 additions & 0 deletions src/api/comments.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Comment } from '../types/Comment';
import { client } from '../utils/fetchClient';

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

export function deleteComment(commentId: number) {
return client.delete(`/comments/${commentId}`);
}

export function addComment({
postId,
name,
email,
body,
}: Omit<Comment, 'id'>): Promise<Comment> {
return client.post('/comments', {
postId,
name,
email,
body,
});
}
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 getUserPosts = (userId: number) => {
return client.get<Post[]>(`/posts?userId=${userId}`);
};
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