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

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
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
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 { User } from './types/User';
import { userClient } from './utils/userClient';
import { postClient } from './utils/postClient';
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 = () => {
//region States
const [users, setUsers] = useState<User[]>([]);
const [userSelected, setUserSelected] = useState<User | null>(null);
const [userSelPosts, setUserSelPosts] = useState<Post[]>([]);
const [openedPost, setOpendPost] = useState<Post | null>(null);
const [error, setError] = useState(false);
const [loading, setLoading] = useState(false);
//endregion

<div className="block" data-cy="MainContent">
<p data-cy="NoSelectedUser">No user selected</p>
//Init users
useEffect(() => {
setLoading(true);
userClient
.getAll()
.then(res => {
setUsers(res);
setError(false);
})
.catch(() => setError(true))
.finally(() => setLoading(false));
}, []);

<Loader />
// Update User Posts
useEffect(() => {
setOpendPost(null);

<div
className="notification is-danger"
data-cy="PostsLoadingError"
>
Something went wrong!
</div>
if (userSelected) {
setLoading(true);
postClient
.get(userSelected.id)
.then(res => {
setUserSelPosts(() => res);
setError(false);
})
.catch(() => setError(true))
.finally(() => {
setLoading(false);
});
}
}, [userSelected]);

const handleUserSelect = (user: User) => {
setUserSelected(user);
};

<div className="notification is-warning" data-cy="NoPostsYet">
No posts yet
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} onSelect={handleUserSelect} />
</div>

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

{loading && <Loader />}

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

{userSelected && !loading ? (

Choose a reason for hiding this comment

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

Avoid nested ternary

userSelPosts.length > 0 ? (
<PostsList posts={userSelPosts} onOpen={setOpendPost} />
) : (
<div
className="notification is-warning"
data-cy="NoPostsYet"
>
No posts yet
</div>
)
) : undefined}
</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--close': !openedPost, 'Sidebar--open': openedPost },
)}
>
<div className="tile is-child box is-success ">
<PostDetails post={openedPost} />
</div>
</div>
</div>
</div>
</div>
</main>
);
</main>
);
};
31 changes: 31 additions & 0 deletions src/components/Comment.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { Comment } from '../types/Comment';

type Props = {
comment: Comment;
onDelete: (id: number) => void;
};

export const CommentObj: React.FC<Props> = ({ comment, onDelete }) => {
return (
<article key={comment.id} className="message is-small" data-cy="Comment">
<div className="message-header">
<a href={`mailto:${comment.email}`} data-cy="CommentAuthor">
{comment.name}

Choose a reason for hiding this comment

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

Use destructuring for comment

</a>
<button
data-cy="CommentDelete"
type="button"
className="delete is-small"
aria-label="delete"
onClick={() => onDelete(comment.id)}
>
delete button
</button>
</div>

<div className="message-body" data-cy="CommentBody">
{comment.body}
</div>
</article>
);
};
29 changes: 29 additions & 0 deletions src/components/CommentList.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { Comment } from '../types/Comment';
import { CommentObj } from './Comment';

type Props = {
comments: Comment[];
onDelete: (id: number) => void;
};

export const CommentList: React.FC<Props> = ({ comments, onDelete }) => {
return (
<>
{comments.length > 0 ? (
comments.map(comment => {
return (
<CommentObj
key={comment.id}
comment={comment}
onDelete={onDelete}
/>
);
})
) : (
<p className="title is-4" data-cy="NoCommentsMessage">
No comments yet
</p>
)}
</>
);
};
Loading
Loading