Skip to content

Commit

Permalink
cleanupp comments
Browse files Browse the repository at this point in the history
  • Loading branch information
syavaYki committed Sep 27, 2024
1 parent e1a5d0a commit bfcc001
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 35 deletions.
4 changes: 0 additions & 4 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,13 @@ import { postClient } from './utils/postClient';
import { Post } from './types/Post';

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

//Init users
useEffect(() => {
setLoading(true);
userClient
Expand All @@ -37,7 +34,6 @@ export const App = () => {
.finally(() => setLoading(false));
}, []);

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

Expand Down
2 changes: 1 addition & 1 deletion src/components/Comment.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ type Props = {
};

export const CommentObj: React.FC<Props> = ({ comment, onDelete }) => {
const { id, email, name, body } = { ...comment };
const { id, email, name, body } = comment;

return (
<article key={id} className="message is-small" data-cy="Comment">
Expand Down
41 changes: 41 additions & 0 deletions src/components/PostItem.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import React from 'react';
import { Post } from '../types/Post';
import classNames from 'classnames';

type Props = {
posts: Post[];
openPostId: number | undefined;
onClick: (post: Post) => void;
};

export const PostItem: React.FC<Props> = ({ posts, openPostId, onClick }) => {
return (
<>
{posts.map(post => {
const { id, title } = post;
const isOppened = openPostId === id;

return (
<tr key={id} data-cy="Post">
<td data-cy="PostId">{id}</td>

<td data-cy="PostTitle">{title}</td>

<td className="has-text-right is-vcentered">
<button
type="button"
data-cy="PostButton"
className={classNames('button is-link', {
'is-light': !isOppened,
})}
onClick={() => onClick(post)}
>
{isOppened ? 'Close' : 'Open'}
</button>
</td>
</tr>
);
})}
</>
);
};
33 changes: 7 additions & 26 deletions src/components/PostsList.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { useState } from 'react';
import { Post } from '../types/Post';
import classNames from 'classnames';
import { PostItem } from './PostItem';

type Props = {
posts: Post[];
Expand Down Expand Up @@ -29,36 +29,17 @@ export const PostsList: React.FC<Props> = ({ posts, onOpen }) => {
<tr className="has-background-link-light">
<th>#</th>
<th>Title</th>
{/* eslint-disable-next-line jsx-a11y/control-has-associated-label */}
{}
<th> </th>
</tr>
</thead>

<tbody>
{posts.map(post => {
const isOppened = openedPost?.id === post.id;

return (
<tr key={post.id} data-cy="Post">
<td data-cy="PostId">{post.id}</td>

<td data-cy="PostTitle">{post.title}</td>

<td className="has-text-right is-vcentered">
<button
type="button"
data-cy="PostButton"
className={classNames('button is-link', {
'is-light': !isOppened,
})}
onClick={() => handleClick(post)}
>
{isOppened ? 'Close' : 'Open'}
</button>
</td>
</tr>
);
})}
<PostItem
posts={posts}
openPostId={openedPost?.id}
onClick={handleClick}
/>
</tbody>
</table>
</div>
Expand Down
4 changes: 0 additions & 4 deletions src/components/UserSelector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,14 @@ type Props = {
};

export const UserSelector: React.FC<Props> = ({ users, onSelect }) => {
// region States
const [userSelect, setUserSelect] = useState<User | null>(null);
const [visible, setVisible] = useState(false);
//endregion

// region Event handlers
const handleUserSelect = (user: User): void => {
setUserSelect(user);
setVisible(false);
onSelect(user);
};
//endregion

return (
<div data-cy="UserSelector" className="dropdown is-active">
Expand Down

0 comments on commit bfcc001

Please sign in to comment.