1 |
8c45ccb0
|
hrubyjar
|
import 'antd/dist/antd.css';
|
2 |
c6109e2d
|
Lukáš Vlček
|
import React, { useContext, useEffect, useState } from 'react';
|
3 |
8c45ccb0
|
hrubyjar
|
|
4 |
|
|
import { useUnauthRedirect } from '../../../hooks';
|
5 |
|
|
import { useRouter } from 'next/router';
|
6 |
669ffe38
|
Jaroslav Hrubý
|
import { Button, Space, Table, Typography } from 'antd';
|
7 |
c6109e2d
|
Lukáš Vlček
|
import { faFileLines, faUser } from '@fortawesome/free-solid-svg-icons';
|
8 |
8c45ccb0
|
hrubyjar
|
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
|
9 |
|
|
import { LoggedUserContext } from '../../../contexts/LoggedUserContext';
|
10 |
bae9fbba
|
Jaroslav Hrubý
|
import { MainLayout } from '../../../layouts/MainLayout';
|
11 |
4a7bae81
|
Jaroslav Hrubý
|
import AddDocumentModal from '../../../components/modals/AddDocumentModal';
|
12 |
0db53e25
|
Jaroslav Hrubý
|
import { DocumentListInfo, UserInfo } from '../../../api';
|
13 |
c6109e2d
|
Lukáš Vlček
|
import { documentController, userController } from '../../../controllers';
|
14 |
9bfa1e39
|
Jaroslav Hrubý
|
import AssignDocumentModal from '../../../components/modals/AssignDocumentModal';
|
15 |
669ffe38
|
Jaroslav Hrubý
|
import { ShowConfirmDelete, ShowToast } from '../../../utils/alerts';
|
16 |
43d49a98
|
Jaroslav Hrubý
|
import { TableDocInfo } from '../../../components/types/TableDocInfo';
|
17 |
0db53e25
|
Jaroslav Hrubý
|
import { UserFilter } from '../../../components/types/UserFilter';
|
18 |
669ffe38
|
Jaroslav Hrubý
|
import { getColumnSearchProps, getLocaleProps } from '../../../utils/tableUtils';
|
19 |
|
|
import { UserOutlined } from '@ant-design/icons';
|
20 |
|
|
import Swal from 'sweetalert2';
|
21 |
8c45ccb0
|
hrubyjar
|
|
22 |
|
|
function AdminDocumentPage() {
|
23 |
|
|
const redirecting = useUnauthRedirect('/login');
|
24 |
|
|
const { logout, role } = useContext(LoggedUserContext);
|
25 |
9bfa1e39
|
Jaroslav Hrubý
|
const [visibleAdd, setVisibleAdd] = React.useState(false);
|
26 |
|
|
const [visibleAssign, setVisibleAssign] = React.useState(false);
|
27 |
8c45ccb0
|
hrubyjar
|
const router = useRouter();
|
28 |
|
|
|
29 |
43d49a98
|
Jaroslav Hrubý
|
const [documents, setDocuments] = useState<TableDocInfo[]>([]);
|
30 |
0db53e25
|
Jaroslav Hrubý
|
const [userFilters, setUserFilters] = useState<UserFilter[]>([]);
|
31 |
9bfa1e39
|
Jaroslav Hrubý
|
const [selectedDocs, setSelectedDocs] = useState<string[] | undefined[]>([]);
|
32 |
c6109e2d
|
Lukáš Vlček
|
|
33 |
43d49a98
|
Jaroslav Hrubý
|
async function fetchData() {
|
34 |
|
|
const docs = (await documentController.documentsGet(0, 1000)).data.documents;
|
35 |
|
|
// @ts-ignore
|
36 |
|
|
const tableDocs: TableDocInfo[] = docs?.map((doc, index) => {
|
37 |
|
|
return { key: index, ...doc };
|
38 |
|
|
});
|
39 |
9bfa1e39
|
Jaroslav Hrubý
|
|
40 |
0db53e25
|
Jaroslav Hrubý
|
const users = (await userController.usersGet()).data.users;
|
41 |
|
|
// @ts-ignore
|
42 |
|
|
const filters: UserFilter[] = users?.map((user) => {
|
43 |
|
|
return {
|
44 |
|
|
text: user.name + ' ' + user.surname,
|
45 |
|
|
value: user.username,
|
46 |
|
|
};
|
47 |
|
|
});
|
48 |
|
|
setUserFilters(filters);
|
49 |
|
|
|
50 |
43d49a98
|
Jaroslav Hrubý
|
if (!docs) {
|
51 |
|
|
setDocuments([]);
|
52 |
|
|
} else {
|
53 |
|
|
setDocuments(tableDocs);
|
54 |
c6109e2d
|
Lukáš Vlček
|
}
|
55 |
43d49a98
|
Jaroslav Hrubý
|
}
|
56 |
c6109e2d
|
Lukáš Vlček
|
|
57 |
43d49a98
|
Jaroslav Hrubý
|
useEffect(() => {
|
58 |
06d1aa21
|
Jaroslav Hrubý
|
if (!redirecting && role === 'ADMINISTRATOR') {
|
59 |
c6109e2d
|
Lukáš Vlček
|
fetchData();
|
60 |
8c45ccb0
|
hrubyjar
|
}
|
61 |
|
|
}, [logout, redirecting, role, router]);
|
62 |
|
|
|
63 |
9bfa1e39
|
Jaroslav Hrubý
|
const showAssignModal = () => {
|
64 |
|
|
if (selectedDocs.length == 0) {
|
65 |
|
|
ShowToast('Vyberte dokument pro přiřazení', 'warning', 3000, 'top-end');
|
66 |
|
|
} else {
|
67 |
|
|
setVisibleAssign(true);
|
68 |
|
|
}
|
69 |
|
|
};
|
70 |
|
|
|
71 |
|
|
const showAddModal = () => {
|
72 |
|
|
setVisibleAdd(true);
|
73 |
4a7bae81
|
Jaroslav Hrubý
|
};
|
74 |
|
|
|
75 |
|
|
const hideModal = () => {
|
76 |
43d49a98
|
Jaroslav Hrubý
|
fetchData();
|
77 |
9bfa1e39
|
Jaroslav Hrubý
|
setVisibleAdd(false);
|
78 |
|
|
setVisibleAssign(false);
|
79 |
8c45ccb0
|
hrubyjar
|
};
|
80 |
|
|
|
81 |
c6109e2d
|
Lukáš Vlček
|
const columns = [
|
82 |
|
|
{
|
83 |
|
|
title: 'Název dokumentu',
|
84 |
|
|
dataIndex: 'name',
|
85 |
|
|
key: 'name',
|
86 |
0db53e25
|
Jaroslav Hrubý
|
...getColumnSearchProps('name', 'název'),
|
87 |
669ffe38
|
Jaroslav Hrubý
|
sorter: {
|
88 |
|
|
// @ts-ignore
|
89 |
|
|
compare: (a, b) => a.name.localeCompare(b.name),
|
90 |
|
|
multiple: 2,
|
91 |
|
|
},
|
92 |
c6109e2d
|
Lukáš Vlček
|
},
|
93 |
|
|
{
|
94 |
|
|
title: 'Délka',
|
95 |
|
|
dataIndex: 'length',
|
96 |
|
|
key: 'length',
|
97 |
0db53e25
|
Jaroslav Hrubý
|
sorter: {
|
98 |
|
|
// @ts-ignore
|
99 |
|
|
compare: (a, b) => a.length - b.length,
|
100 |
|
|
multiple: 1,
|
101 |
|
|
},
|
102 |
c6109e2d
|
Lukáš Vlček
|
},
|
103 |
|
|
{
|
104 |
|
|
title: 'Anotátoři',
|
105 |
|
|
dataIndex: 'annotatingUsers',
|
106 |
|
|
key: 'annotatingUsers',
|
107 |
|
|
render: (columnData: UserInfo[], record: DocumentListInfo, index: number) => {
|
108 |
|
|
return (
|
109 |
|
|
<div>
|
110 |
669ffe38
|
Jaroslav Hrubý
|
<Space>
|
111 |
|
|
{columnData.map((e) => (
|
112 |
|
|
<span
|
113 |
|
|
key={e.username + '.' + record.id}
|
114 |
c6109e2d
|
Lukáš Vlček
|
title={e.username ?? ''}
|
115 |
669ffe38
|
Jaroslav Hrubý
|
>
|
116 |
|
|
<FontAwesomeIcon
|
117 |
|
|
icon={faUser}
|
118 |
|
|
title={e.username ?? ''}
|
119 |
|
|
className={'me-2'}
|
120 |
|
|
/>
|
121 |
|
|
</span>
|
122 |
|
|
))}
|
123 |
|
|
</Space>
|
124 |
c6109e2d
|
Lukáš Vlček
|
</div>
|
125 |
|
|
);
|
126 |
|
|
},
|
127 |
0db53e25
|
Jaroslav Hrubý
|
filters: userFilters,
|
128 |
|
|
filterSearch: true,
|
129 |
|
|
// @ts-ignore
|
130 |
|
|
onFilter: (value, record) =>
|
131 |
|
|
// @ts-ignore
|
132 |
|
|
record.annotatingUsers.find((user) => user['username'] === value),
|
133 |
|
|
sorter: {
|
134 |
|
|
// @ts-ignore
|
135 |
|
|
compare: (a, b) => a.annotatingUsers.length - b.annotatingUsers.length,
|
136 |
669ffe38
|
Jaroslav Hrubý
|
multiple: 3,
|
137 |
0db53e25
|
Jaroslav Hrubý
|
},
|
138 |
c6109e2d
|
Lukáš Vlček
|
},
|
139 |
|
|
];
|
140 |
|
|
|
141 |
9bfa1e39
|
Jaroslav Hrubý
|
const rowSelection = {
|
142 |
|
|
onChange: (selectedRowKeys: React.Key[], selectedRows: DocumentListInfo[]) => {
|
143 |
43d49a98
|
Jaroslav Hrubý
|
// @ts-ignore
|
144 |
9bfa1e39
|
Jaroslav Hrubý
|
setSelectedDocs(selectedRows.map((row) => row.id));
|
145 |
|
|
},
|
146 |
|
|
};
|
147 |
|
|
|
148 |
8c45ccb0
|
hrubyjar
|
return redirecting || role !== 'ADMINISTRATOR' ? null : (
|
149 |
|
|
<MainLayout>
|
150 |
|
|
<Typography.Title level={2}>
|
151 |
|
|
<FontAwesomeIcon icon={faFileLines} /> Dokumenty
|
152 |
|
|
</Typography.Title>
|
153 |
9bfa1e39
|
Jaroslav Hrubý
|
<Button type={'primary'} onClick={showAddModal}>
|
154 |
8c45ccb0
|
hrubyjar
|
Nahrát dokument
|
155 |
|
|
</Button>
|
156 |
9bfa1e39
|
Jaroslav Hrubý
|
<Button onClick={showAssignModal}>Přiřadit dokumenty</Button>
|
157 |
|
|
{visibleAdd && <AddDocumentModal onCancel={hideModal} />}
|
158 |
|
|
{visibleAssign && (
|
159 |
|
|
<AssignDocumentModal documentsIds={selectedDocs} onCancel={hideModal} />
|
160 |
|
|
)}
|
161 |
c6109e2d
|
Lukáš Vlček
|
|
162 |
|
|
<Table
|
163 |
669ffe38
|
Jaroslav Hrubý
|
locale={{ ...getLocaleProps() }}
|
164 |
9bfa1e39
|
Jaroslav Hrubý
|
rowSelection={{
|
165 |
|
|
type: 'checkbox',
|
166 |
|
|
...rowSelection,
|
167 |
|
|
}}
|
168 |
0db53e25
|
Jaroslav Hrubý
|
// @ts-ignore
|
169 |
c6109e2d
|
Lukáš Vlček
|
columns={columns}
|
170 |
|
|
dataSource={documents}
|
171 |
9bfa1e39
|
Jaroslav Hrubý
|
size="middle"
|
172 |
|
|
scroll={{ y: 600 }}
|
173 |
c6109e2d
|
Lukáš Vlček
|
/>
|
174 |
8c45ccb0
|
hrubyjar
|
</MainLayout>
|
175 |
|
|
);
|
176 |
|
|
}
|
177 |
|
|
|
178 |
|
|
export default AdminDocumentPage;
|