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

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

Develop #1163

Show file tree
Hide file tree
Changes from 5 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
143 changes: 106 additions & 37 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,53 +8,122 @@ 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 { User } from './types/User';
import { Post } from './types/Post';
import * as userService from './api/users';
import * as postService 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 [users, setUsers] = useState<User[]>([]);
const [selectedUser, setSelectedUser] = useState<User | null>(null);

<div className="block" data-cy="MainContent">
<p data-cy="NoSelectedUser">No user selected</p>
const [userPosts, setUserPosts] = useState<Post[]>([]);
const [selectedPost, setSelectedPost] = useState<Post | null>(null);

<Loader />
const [isLoadingPosts, setIsLoadingPosts] = useState(false);
const [hasError, setHasError] = useState(false);
const [hasWarning, setHasWarning] = useState(false);

<div
className="notification is-danger"
data-cy="PostsLoadingError"
>
Something went wrong!
</div>
useEffect(() => {
setHasError(false);

userService
.getUsers()
.then(setUsers)
.catch(() => setHasError(true));
}, []);

<div className="notification is-warning" data-cy="NoPostsYet">
No posts yet
useEffect(() => {
if (selectedUser) {
setIsLoadingPosts(true);
setHasError(false);
setHasWarning(false);
setUserPosts([]);
setSelectedPost(null);

postService
.getPosts(selectedUser.id)
.then(posts => {
if (posts.length > 0) {
setUserPosts(posts);
} else {
setHasWarning(true);
}
})
.catch(() => setHasError(true))
.finally(() => setIsLoadingPosts(false));
}
}, [selectedUser]);

return (
<main className="section">
<div className="container">
<div className="tile is-ancestor columns">
<div className="tile is-parent column">
<div className="tile is-child box is-success">
<div className="block">
{users && (
<UserSelector
users={users}
selectedUser={selectedUser}
onSelectUser={setSelectedUser}
/>
)}
</div>

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

{isLoadingPosts && <Loader />}

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

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

{!hasError && !!userPosts.length && (
<PostsList
posts={userPosts}
selectedPost={selectedPost}
onSelect={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',
'column',
'Sidebar',
{
'Sidebar--open': selectedPost !== null,
},
)}
>
<div className="tile is-child box is-success">
{selectedPost && <PostDetails post={selectedPost} />}
</div>
</div>
</div>
</div>
</div>
</main>
);
</main>
);
};
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 function getComments(postId: number) {
return client.get<Comment[]>(`/comments?postId=${postId}`);
}

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

export function postComment(data: Omit<Comment, 'id'>) {
return client.post<Comment>(`/comments`, data);
}
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 function getPosts(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 function getUsers() {
return client.get<User[]>('/users');
}
44 changes: 44 additions & 0 deletions src/components/CommentInfo.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import React, { useState } from 'react';
import { Comment } from '../types/Comment';
import classNames from 'classnames';

type Props = {
comment: Comment;
onDelete: (commentId: number) => Promise<void>;
};

export const CommentInfo: React.FC<Props> = ({ comment, onDelete }) => {
const [deletingCommentId, setDeletingCommentId] = useState<number | null>(
null,
);
const { id, name, email, body } = comment;

const handleDelete = (commentId: number) => {
setDeletingCommentId(commentId);

onDelete(commentId).finally(() => setDeletingCommentId(null));
};

return (
<article className="message is-small" data-cy="Comment">
<div className="message-header">
<a href={`mailto:${email}`} data-cy="CommentAuthor">
{name}
</a>
<button
data-cy="CommentDelete"
type="button"
className={classNames('button delete is-small', {
'is-loading': deletingCommentId === id,
})}
aria-label="delete"
onClick={() => handleDelete(id)}
></button>
</div>

<div className="message-body" data-cy="CommentBody">
{body}
</div>
</article>
);
};
Loading
Loading