-
Notifications
You must be signed in to change notification settings - Fork 0
/
Users.js
82 lines (75 loc) · 1.83 KB
/
Users.js
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
import React, {
useContext,
useEffect,
useState
} from 'react';
import DOMPurify from 'dompurify';
import PageTitle from '../components/common/PageTitle';
import { FetchContext } from '../context/FetchContext';
import Card from '../components/common/Card';
import defaultAvatar from './../images/defaultAvatar.png';
const UserDetailLabel = ({ text }) => (
<p className="mt-2 uppercase font-bold text-gray-500 text-xs">
{text}
</p>
);
const UserDetail = ({ user }) => (
<Card>
<div className="flex">
<div className="w-24">
<img
src={user.avatar || defaultAvatar}
alt="avatar"
/>
</div>
<div>
<p className="font-bold text-lg">
{user.firstName} {user.lastName}
</p>
<div className="mt-2">
<UserDetailLabel text="Bio" />
{user.bio ? (
<div
dangerouslySetInnerHTML={{ __html: DOMPurify.sanitize(user.bio) }}
/>
) : (
<p className="text-gray-500 italic">
No bio set
</p>
)}
</div>
</div>
</div>
</Card>
);
const Users = () => {
const fetchContext = useContext(FetchContext);
const [users, setUsers] = useState([]);
useEffect(() => {
const getUsers = async () => {
try {
const { data } = await fetchContext.authAxios.get(
'users'
);
setUsers(data.users);
} catch (err) {
console.log(err);
}
};
getUsers();
}, [fetchContext]);
return (
<>
<PageTitle title="Users" />
<div className="flex flex-col">
{!!users.length &&
users.map(user => (
<div key={user._id} className="m-2">
<UserDetail user={user} />
</div>
))}
</div>
</>
);
};
export default Users;