-
Notifications
You must be signed in to change notification settings - Fork 313
/
UserListScreen.jsx
95 lines (90 loc) · 2.67 KB
/
UserListScreen.jsx
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import React from 'react';
import { Table, Button } from 'react-bootstrap';
import { FaTrash, FaEdit, FaCheck, FaTimes } from 'react-icons/fa';
import Message from '../../components/Message';
import Loader from '../../components/Loader';
import {
useDeleteUserMutation,
useGetUsersQuery,
} from '../../slices/usersApiSlice';
import { toast } from 'react-toastify';
import { Link } from 'react-router-dom';
const UserListScreen = () => {
const { data: users, refetch, isLoading, error } = useGetUsersQuery();
const [deleteUser] = useDeleteUserMutation();
const deleteHandler = async (id) => {
if (window.confirm('Are you sure')) {
try {
await deleteUser(id);
refetch();
} catch (err) {
toast.error(err?.data?.message || err.error);
}
}
};
return (
<>
<h1>Users</h1>
{isLoading ? (
<Loader />
) : error ? (
<Message variant='danger'>
{error?.data?.message || error.error}
</Message>
) : (
<Table striped bordered hover responsive className='table-sm'>
<thead>
<tr>
<th>ID</th>
<th>NAME</th>
<th>EMAIL</th>
<th>ADMIN</th>
<th></th>
</tr>
</thead>
<tbody>
{users.map((user) => (
<tr key={user._id}>
<td>{user._id}</td>
<td>{user.name}</td>
<td>
<a href={`mailto:${user.email}`}>{user.email}</a>
</td>
<td>
{user.isAdmin ? (
<FaCheck style={{ color: 'green' }} />
) : (
<FaTimes style={{ color: 'red' }} />
)}
</td>
<td>
{!user.isAdmin && (
<>
<Button
as={Link}
to={`/admin/user/${user._id}/edit`}
style={{ marginRight: '10px' }}
variant='light'
className='btn-sm'
>
<FaEdit />
</Button>
<Button
variant='danger'
className='btn-sm'
onClick={() => deleteHandler(user._id)}
>
<FaTrash style={{ color: 'white' }} />
</Button>
</>
)}
</td>
</tr>
))}
</tbody>
</Table>
)}
</>
);
};
export default UserListScreen;