1
|
import 'antd/dist/antd.css';
|
2
|
import React, { useContext, useEffect, useState } from 'react';
|
3
|
|
4
|
import { useUnauthRedirect } from '../../hooks';
|
5
|
import { useRouter } from 'next/router';
|
6
|
import { Button, Space, Table, Typography } from 'antd';
|
7
|
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
|
8
|
import { faUsers } from '@fortawesome/free-solid-svg-icons';
|
9
|
import { LoggedUserContext } from '../../contexts/LoggedUserContext';
|
10
|
import { MainLayout } from '../../layouts/MainLayout';
|
11
|
import { UserInfo } from '../../api';
|
12
|
import { userController } from '../../controllers';
|
13
|
import AddUserModal from '../../components/modals/AddUserModal';
|
14
|
import UserDocumentsModal from '../../components/modals/UserDocumentsModal';
|
15
|
import { ShowConfirmDelete } from '../../utils/alerts';
|
16
|
import EditUserModal from '../../components/modals/EditUserModal';
|
17
|
|
18
|
function UsersPage() {
|
19
|
const redirecting = useUnauthRedirect('/login');
|
20
|
const { logout, role } = useContext(LoggedUserContext);
|
21
|
const router = useRouter();
|
22
|
|
23
|
const [addVisible, setAddVisible] = React.useState(false);
|
24
|
const [editVisible, setEditVisible] = React.useState(false);
|
25
|
const [docsVisible, setDocsVisible] = React.useState(false);
|
26
|
const [users, setUsers] = useState<UserInfo[]>([]);
|
27
|
|
28
|
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
|
}
|
37
|
}
|
38
|
|
39
|
useEffect(() => {
|
40
|
if (!redirecting && role === 'ADMINISTRATOR') {
|
41
|
fetchData();
|
42
|
}
|
43
|
}, [logout, redirecting, role, router]);
|
44
|
|
45
|
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
|
};
|
69
|
|
70
|
const hideModals = () => {
|
71
|
fetchData();
|
72
|
setAddVisible(false);
|
73
|
setDocsVisible(false);
|
74
|
setEditVisible(false);
|
75
|
};
|
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
|
dataIndex: 'id',
|
85
|
render: (id: string) => (
|
86
|
<div>
|
87
|
<Space size={'middle'}>
|
88
|
<Button onClick={() => showDocsModal(id)}>Dokumenty</Button>
|
89
|
<Button onClick={() => showEditModal(id)}>Upravit</Button>
|
90
|
<Button onClick={() => deleteUser(id)}>Smazat</Button>
|
91
|
</Space>
|
92
|
</div>
|
93
|
),
|
94
|
},
|
95
|
];
|
96
|
|
97
|
return redirecting || role !== 'ADMINISTRATOR' ? null : (
|
98
|
<MainLayout>
|
99
|
<Typography.Title level={2}>
|
100
|
<FontAwesomeIcon icon={faUsers} /> Uživatelé
|
101
|
</Typography.Title>
|
102
|
<Button type={'primary'} onClick={showAddModal}>
|
103
|
Přidat uživatele
|
104
|
</Button>
|
105
|
{addVisible && <AddUserModal onCancel={hideModals} />}
|
106
|
{editVisible && <EditUserModal userInfo={user} onCancel={hideModals} />}
|
107
|
{docsVisible && <UserDocumentsModal userId={user.id} onCancel={hideModals} />}
|
108
|
|
109
|
<Table
|
110
|
columns={columns}
|
111
|
dataSource={users}
|
112
|
size="small"
|
113
|
scroll={{ y: 500 }}
|
114
|
/>
|
115
|
</MainLayout>
|
116
|
);
|
117
|
}
|
118
|
|
119
|
export default UsersPage;
|