-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
95 additions
and
141 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,94 +1,95 @@ | ||
import React from 'react'; | ||
import { useEffect, useState } from 'react'; | ||
import { Post } from '../types/Post'; | ||
import { getPosts } from '../api/posts'; | ||
import { Loader } from './Loader'; | ||
|
||
interface Props { | ||
filteredPostsList: Post[]; | ||
selectedUserId: number | undefined; | ||
} | ||
export const PostsList: React.FC<Props> = ({ filteredPostsList }) => { | ||
return ( | ||
<div data-cy="PostsList"> | ||
<p className="title">Posts:</p> | ||
export const PostsList: React.FC<Props> = ({ selectedUserId }) => { | ||
const [postsList, setPostsList] = useState<Post[]>([]); | ||
const [isLoading, setIsLoading] = useState(false); | ||
const [isError, setIsError] = useState(false); | ||
|
||
const filteredPostsList = () => { | ||
return postsList.filter(post => post.userId === selectedUserId); | ||
}; | ||
|
||
<table className="table is-fullwidth is-striped is-hoverable is-narrow"> | ||
<thead> | ||
<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> | ||
const isHasPostsList = filteredPostsList().length; | ||
|
||
<tbody> | ||
{filteredPostsList.map(post => ( | ||
<tr key={post.id} data-cy="Post"> | ||
<td data-cy="PostId">{post.id}</td> | ||
useEffect(() => { | ||
if (selectedUserId !== undefined) { | ||
const fetchPosts = async () => { | ||
setIsLoading(true); | ||
try { | ||
const posts = await getPosts(selectedUserId); | ||
|
||
<td data-cy="PostTitle">{post.title}</td> | ||
setPostsList(posts); | ||
} catch (error) { | ||
setIsError(true); | ||
} finally { | ||
setIsLoading(false); | ||
} | ||
}; | ||
|
||
<td className="has-text-right is-vcentered"> | ||
<button | ||
type="button" | ||
data-cy="PostButton" | ||
className="button is-link is-light" | ||
> | ||
Open | ||
</button> | ||
</td> | ||
</tr> | ||
))} | ||
fetchPosts(); | ||
} | ||
}, [selectedUserId]); | ||
|
||
{/* <tr data-cy="Post"> | ||
<td data-cy="PostId">18</td> | ||
return ( | ||
<> | ||
{!isLoading && isHasPostsList > 0 && ( | ||
<div data-cy="PostsList"> | ||
<p className="title">Posts:</p> | ||
|
||
<td data-cy="PostTitle"> | ||
voluptate et itaque vero tempora molestiae | ||
</td> | ||
<table | ||
className="table | ||
is-fullwidth is-striped is-hoverable is-narrow" | ||
> | ||
<thead> | ||
<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> | ||
|
||
<td className="has-text-right is-vcentered"> | ||
<button | ||
type="button" | ||
data-cy="PostButton" | ||
className="button is-link" | ||
> | ||
Close | ||
</button> | ||
</td> | ||
</tr> */} | ||
<tbody> | ||
{filteredPostsList().map(post => ( | ||
<tr key={post.id} data-cy="Post"> | ||
<td data-cy="PostId">{post.id}</td> | ||
|
||
{/* <tr data-cy="Post"> | ||
<td data-cy="PostId">19</td> | ||
<td data-cy="PostTitle"> | ||
adipisci placeat illum aut reiciendis qui | ||
</td> | ||
<td data-cy="PostTitle">{post.title}</td> | ||
|
||
<td className="has-text-right is-vcentered"> | ||
<button | ||
type="button" | ||
data-cy="PostButton" | ||
className="button is-link is-light" | ||
> | ||
Open | ||
</button> | ||
</td> | ||
</tr> | ||
<td className="has-text-right is-vcentered"> | ||
<button | ||
type="button" | ||
data-cy="PostButton" | ||
className="button is-link is-light" | ||
> | ||
Open | ||
</button> | ||
</td> | ||
</tr> | ||
))} | ||
</tbody> | ||
</table> | ||
</div> | ||
)} | ||
{isLoading && <Loader />} | ||
|
||
<tr data-cy="Post"> | ||
<td data-cy="PostId">20</td> | ||
<td data-cy="PostTitle">doloribus ad provident suscipit at</td> | ||
{isError && ( | ||
<div className="notification is-danger" data-cy="PostsLoadingError"> | ||
Something went wrong! | ||
</div> | ||
)} | ||
|
||
<td className="has-text-right is-vcentered"> | ||
<button | ||
type="button" | ||
data-cy="PostButton" | ||
className="button is-link is-light" | ||
> | ||
Open | ||
</button> | ||
</td> | ||
</tr> */} | ||
</tbody> | ||
</table> | ||
</div> | ||
{!isHasPostsList && selectedUserId && !isError && !isLoading && ( | ||
<div className="notification is-warning" data-cy="NoPostsYet"> | ||
No posts yet | ||
</div> | ||
)} | ||
</> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters