-
Notifications
You must be signed in to change notification settings - Fork 24
/
users-list.tsx
36 lines (33 loc) · 931 Bytes
/
users-list.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import { FC } from 'react';
import { useQuery } from 'react-query';
import { Link } from 'react-router-dom';
import { GetUsersCollectionApiResponse } from '../types/users';
export type UsersListProps = unknown;
export const UsersList: FC<UsersListProps> = () => {
const { data: users, isError } = useQuery<GetUsersCollectionApiResponse>(
'users',
async () => {
const response = await fetch('/api/users');
return await response.json();
},
{ retry: false },
);
return (
<div data-testid="users-list">
<h1>Users</h1>
{isError ? (
<span>Failed to load users</span>
) : users ? (
<ul>
{users.map(({ id, firstName, lastName }) => (
<li key={id}>
<Link to={`/users/${id}`}>{`${firstName} ${lastName}`}</Link>
</li>
))}
</ul>
) : (
<span>Loading...</span>
)}
</div>
);
};