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, Table, Tag, Typography } from 'antd';
|
7
|
import { faFileLines, faUser } from '@fortawesome/free-solid-svg-icons';
|
8
|
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
|
9
|
import { LoggedUserContext } from '../../../contexts/LoggedUserContext';
|
10
|
import { MainLayout } from '../../../layouts/MainLayout';
|
11
|
import AddDocumentModal from '../../../components/modals/AddDocumentModal';
|
12
|
import { AnnotationListInfo, DocumentListInfo, UserInfo } from '../../../api';
|
13
|
import { documentController, userController } from '../../../controllers';
|
14
|
|
15
|
function AdminDocumentPage() {
|
16
|
const redirecting = useUnauthRedirect('/login');
|
17
|
const { logout, role } = useContext(LoggedUserContext);
|
18
|
const [visible, setVisible] = React.useState(false);
|
19
|
const router = useRouter();
|
20
|
|
21
|
const [documents, setDocuments] = useState<DocumentListInfo[]>([]);
|
22
|
|
23
|
useEffect(() => {
|
24
|
async function fetchData() {
|
25
|
let docs = (await documentController.documentsGet(0, 1000)).data.documents;
|
26
|
if (!docs) {
|
27
|
setDocuments([]);
|
28
|
} else {
|
29
|
setDocuments(docs);
|
30
|
}
|
31
|
}
|
32
|
|
33
|
if (!redirecting && role === 'ADMINISTRATOR') {
|
34
|
fetchData();
|
35
|
}
|
36
|
}, [logout, redirecting, role, router]);
|
37
|
|
38
|
const showModal = () => {
|
39
|
setVisible(true);
|
40
|
};
|
41
|
|
42
|
const hideModal = () => {
|
43
|
setVisible(false);
|
44
|
};
|
45
|
|
46
|
const columns = [
|
47
|
{
|
48
|
title: 'Název dokumentu',
|
49
|
dataIndex: 'name',
|
50
|
key: 'name',
|
51
|
},
|
52
|
{
|
53
|
title: 'Délka',
|
54
|
dataIndex: 'length',
|
55
|
key: 'length',
|
56
|
},
|
57
|
{
|
58
|
title: 'Anotátoři',
|
59
|
dataIndex: 'annotatingUsers',
|
60
|
key: 'annotatingUsers',
|
61
|
render: (columnData: UserInfo[], record: DocumentListInfo, index: number) => {
|
62
|
return (
|
63
|
<div>
|
64
|
{columnData.map((e) => (
|
65
|
<span
|
66
|
key={e.username + '.' + record.id}
|
67
|
title={e.username ?? ''}
|
68
|
>
|
69
|
<FontAwesomeIcon
|
70
|
icon={faUser}
|
71
|
title={e.username ?? ''}
|
72
|
className={'me-2'}
|
73
|
/>
|
74
|
</span>
|
75
|
))}
|
76
|
</div>
|
77
|
);
|
78
|
},
|
79
|
},
|
80
|
];
|
81
|
|
82
|
return redirecting || role !== 'ADMINISTRATOR' ? null : (
|
83
|
<MainLayout>
|
84
|
<Typography.Title level={2}>
|
85
|
<FontAwesomeIcon icon={faFileLines} /> Dokumenty
|
86
|
</Typography.Title>
|
87
|
<Button type={'primary'} onClick={showModal}>
|
88
|
Nahrát dokument
|
89
|
</Button>
|
90
|
{visible && <AddDocumentModal onCancel={hideModal} />}
|
91
|
|
92
|
<Table
|
93
|
columns={columns}
|
94
|
dataSource={documents}
|
95
|
size="small"
|
96
|
scroll={{ y: 500 }}
|
97
|
/>
|
98
|
</MainLayout>
|
99
|
);
|
100
|
}
|
101
|
|
102
|
export default AdminDocumentPage;
|