1 |
8c45ccb0
|
hrubyjar
|
import 'antd/dist/antd.css';
|
2 |
82ba2ee5
|
Jaroslav Hrubý
|
import React, { useContext, useEffect, useState } from 'react';
|
3 |
8c45ccb0
|
hrubyjar
|
|
4 |
|
|
import { useUnauthRedirect } from '../../hooks';
|
5 |
|
|
import { useRouter } from 'next/router';
|
6 |
82ba2ee5
|
Jaroslav Hrubý
|
import { Button, Space, Table, Typography } from 'antd';
|
7 |
8c45ccb0
|
hrubyjar
|
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
|
8 |
|
|
import { faUsers } from '@fortawesome/free-solid-svg-icons';
|
9 |
|
|
import { LoggedUserContext } from '../../contexts/LoggedUserContext';
|
10 |
51f70e00
|
Lukáš Vlček
|
import { MainLayout } from '../../layouts/MainLayout';
|
11 |
82ba2ee5
|
Jaroslav Hrubý
|
import { UserInfo } from '../../api';
|
12 |
|
|
import { userController } from '../../controllers';
|
13 |
|
|
import AddUserModal from '../../components/modals/AddUserModal';
|
14 |
43d49a98
|
Jaroslav Hrubý
|
import UserDocumentsModal from '../../components/modals/UserDocumentsModal';
|
15 |
|
|
import { ShowConfirmDelete } from '../../utils/alerts';
|
16 |
|
|
import EditUserModal from '../../components/modals/EditUserModal';
|
17 |
8c45ccb0
|
hrubyjar
|
|
18 |
|
|
function UsersPage() {
|
19 |
|
|
const redirecting = useUnauthRedirect('/login');
|
20 |
|
|
const { logout, role } = useContext(LoggedUserContext);
|
21 |
|
|
const router = useRouter();
|
22 |
|
|
|
23 |
43d49a98
|
Jaroslav Hrubý
|
const [addVisible, setAddVisible] = React.useState(false);
|
24 |
|
|
const [editVisible, setEditVisible] = React.useState(false);
|
25 |
|
|
const [docsVisible, setDocsVisible] = React.useState(false);
|
26 |
82ba2ee5
|
Jaroslav Hrubý
|
const [users, setUsers] = useState<UserInfo[]>([]);
|
27 |
|
|
|
28 |
43d49a98
|
Jaroslav Hrubý
|
const [user, setUser] = useState<UserInfo>({});
|
29 |
|
|
|
30 |
|
|
async function fetchData() {
|
31 |
|
|
let usrs = (await userController.usersGet()).data.users;
|
32 |
|
|
if (!usrs) {
|
33 |
|
|
setUsers([]);
|
34 |
|
|
} else {
|
35 |
|
|
setUsers(usrs);
|
36 |
82ba2ee5
|
Jaroslav Hrubý
|
}
|
37 |
43d49a98
|
Jaroslav Hrubý
|
}
|
38 |
82ba2ee5
|
Jaroslav Hrubý
|
|
39 |
43d49a98
|
Jaroslav Hrubý
|
useEffect(() => {
|
40 |
06d1aa21
|
Jaroslav Hrubý
|
if (!redirecting && role === 'ADMINISTRATOR') {
|
41 |
82ba2ee5
|
Jaroslav Hrubý
|
fetchData();
|
42 |
8c45ccb0
|
hrubyjar
|
}
|
43 |
|
|
}, [logout, redirecting, role, router]);
|
44 |
|
|
|
45 |
43d49a98
|
Jaroslav Hrubý
|
const setUserInfo = (userId: string) => {
|
46 |
|
|
const userInfo = users.find((user) => user.id === userId);
|
47 |
|
|
if (userInfo) {
|
48 |
|
|
setUser(userInfo);
|
49 |
|
|
}
|
50 |
|
|
};
|
51 |
|
|
|
52 |
|
|
const deleteUser = (userId: string) => {
|
53 |
|
|
ShowConfirmDelete(() => {
|
54 |
|
|
userController.userUserIdDelete(userId).then(() => fetchData());
|
55 |
|
|
}, 'uživatele');
|
56 |
|
|
};
|
57 |
|
|
|
58 |
|
|
const showAddModal = () => {
|
59 |
|
|
setAddVisible(true);
|
60 |
|
|
};
|
61 |
|
|
const showEditModal = (userId: string) => {
|
62 |
|
|
setUserInfo(userId);
|
63 |
|
|
setEditVisible(true);
|
64 |
|
|
};
|
65 |
|
|
const showDocsModal = (userId: string) => {
|
66 |
|
|
setUserInfo(userId);
|
67 |
|
|
setDocsVisible(true);
|
68 |
82ba2ee5
|
Jaroslav Hrubý
|
};
|
69 |
|
|
|
70 |
43d49a98
|
Jaroslav Hrubý
|
const hideModals = () => {
|
71 |
|
|
fetchData();
|
72 |
|
|
setAddVisible(false);
|
73 |
|
|
setDocsVisible(false);
|
74 |
|
|
setEditVisible(false);
|
75 |
82ba2ee5
|
Jaroslav Hrubý
|
};
|
76 |
|
|
|
77 |
|
|
const columns = [
|
78 |
|
|
{ title: 'Jméno', dataIndex: 'name', key: 'name' },
|
79 |
|
|
{ title: 'Příjmení', dataIndex: 'surname', key: 'surname' },
|
80 |
|
|
{ title: 'Uživatelské jméno', dataIndex: 'username', key: 'username' },
|
81 |
|
|
{
|
82 |
|
|
title: '',
|
83 |
|
|
key: 'operation',
|
84 |
43d49a98
|
Jaroslav Hrubý
|
dataIndex: 'id',
|
85 |
|
|
render: (id: string) => (
|
86 |
82ba2ee5
|
Jaroslav Hrubý
|
<div>
|
87 |
|
|
<Space size={'middle'}>
|
88 |
43d49a98
|
Jaroslav Hrubý
|
<Button onClick={() => showDocsModal(id)}>Dokumenty</Button>
|
89 |
|
|
<Button onClick={() => showEditModal(id)}>Upravit</Button>
|
90 |
|
|
<Button onClick={() => deleteUser(id)}>Smazat</Button>
|
91 |
82ba2ee5
|
Jaroslav Hrubý
|
</Space>
|
92 |
|
|
</div>
|
93 |
|
|
),
|
94 |
|
|
},
|
95 |
|
|
];
|
96 |
|
|
|
97 |
8c45ccb0
|
hrubyjar
|
return redirecting || role !== 'ADMINISTRATOR' ? null : (
|
98 |
|
|
<MainLayout>
|
99 |
|
|
<Typography.Title level={2}>
|
100 |
|
|
<FontAwesomeIcon icon={faUsers} /> Uživatelé
|
101 |
|
|
</Typography.Title>
|
102 |
43d49a98
|
Jaroslav Hrubý
|
<Button type={'primary'} onClick={showAddModal}>
|
103 |
82ba2ee5
|
Jaroslav Hrubý
|
Přidat uživatele
|
104 |
|
|
</Button>
|
105 |
43d49a98
|
Jaroslav Hrubý
|
{addVisible && <AddUserModal onCancel={hideModals} />}
|
106 |
|
|
{editVisible && <EditUserModal userInfo={user} onCancel={hideModals} />}
|
107 |
|
|
{docsVisible && <UserDocumentsModal userId={user.id} onCancel={hideModals} />}
|
108 |
82ba2ee5
|
Jaroslav Hrubý
|
|
109 |
|
|
<Table
|
110 |
|
|
columns={columns}
|
111 |
|
|
dataSource={users}
|
112 |
|
|
size="small"
|
113 |
|
|
scroll={{ y: 500 }}
|
114 |
|
|
/>
|
115 |
8c45ccb0
|
hrubyjar
|
</MainLayout>
|
116 |
|
|
);
|
117 |
|
|
}
|
118 |
|
|
|
119 |
|
|
export default UsersPage;
|