blob: dd17acebc4fd52a1e83ffa470f7dfab3dd10d50f (
plain)
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
|
import React, { useEffect, useState } from 'react';
import axios from './axios';
const UserList = () => {
const [ data, setData ] = useState({});
useEffect(() => {
(async () => {
try {
const resp = await axios.get('/users');
setData({
users: resp.data.users,
});
} catch (err) {
if (err.response.status === 401) {
setData({
error: err.response.data.error,
});
} else {
throw err;
}
}
})();
}, []);
const users = data.users;
const userRows = !!users
? users.map(user => (
<tr key={user.id}>
<td>{user.id}</td>
<td>{user.username}</td>
</tr>
))
: null;
return (
<div>
<h2 className="subtitle">User list</h2>
<div>
<div className="error" key="error">
{data.error}
</div>
<table className="table">
<thead>
<tr>
<th>ID</th>
<th>Username</th>
</tr>
</thead>
<tbody>{userRows}</tbody>
</table>
</div>
</div>
);
};
export default UserList;
|