Projekt

Obecné

Profil

Stáhnout (3.31 KB) Statistiky
| Větev: | Tag: | Revize:
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 c6109e2d Lukáš Vlček
import { Button, Table, Tag, Typography } from 'antd';
7
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 c6109e2d Lukáš Vlček
import { AnnotationListInfo, DocumentListInfo, UserInfo } from '../../../api';
13
import { documentController, userController } from '../../../controllers';
14 8c45ccb0 hrubyjar
15
function AdminDocumentPage() {
16
    const redirecting = useUnauthRedirect('/login');
17
    const { logout, role } = useContext(LoggedUserContext);
18 4a7bae81 Jaroslav Hrubý
    const [visible, setVisible] = React.useState(false);
19 8c45ccb0 hrubyjar
    const router = useRouter();
20
21 c6109e2d Lukáš Vlček
    const [documents, setDocuments] = useState<DocumentListInfo[]>([]);
22
23 06d1aa21 Jaroslav Hrubý
    useEffect(() => {
24 c6109e2d Lukáš Vlček
        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 06d1aa21 Jaroslav Hrubý
        if (!redirecting && role === 'ADMINISTRATOR') {
34 c6109e2d Lukáš Vlček
            fetchData();
35 8c45ccb0 hrubyjar
        }
36
    }, [logout, redirecting, role, router]);
37
38
    const showModal = () => {
39 4a7bae81 Jaroslav Hrubý
        setVisible(true);
40
    };
41
42
    const hideModal = () => {
43
        setVisible(false);
44 8c45ccb0 hrubyjar
    };
45
46 c6109e2d Lukáš Vlček
    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 8c45ccb0 hrubyjar
    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 4a7bae81 Jaroslav Hrubý
            {visible && <AddDocumentModal onCancel={hideModal} />}
91 c6109e2d Lukáš Vlček
92
            <Table
93
                columns={columns}
94
                dataSource={documents}
95
                size="small"
96
                scroll={{ y: 500 }}
97
            />
98 8c45ccb0 hrubyjar
        </MainLayout>
99
    );
100
}
101
102
export default AdminDocumentPage;