Projekt

Obecné

Profil

Stáhnout (4.45 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 9bfa1e39 Jaroslav Hrubý
import AssignDocumentModal from '../../../components/modals/AssignDocumentModal';
15
import { ShowToast } from '../../../utils/alerts';
16 8c45ccb0 hrubyjar
17
function AdminDocumentPage() {
18
    const redirecting = useUnauthRedirect('/login');
19
    const { logout, role } = useContext(LoggedUserContext);
20 9bfa1e39 Jaroslav Hrubý
    const [visibleAdd, setVisibleAdd] = React.useState(false);
21
    const [visibleAssign, setVisibleAssign] = React.useState(false);
22 8c45ccb0 hrubyjar
    const router = useRouter();
23
24 c6109e2d Lukáš Vlček
    const [documents, setDocuments] = useState<DocumentListInfo[]>([]);
25 9bfa1e39 Jaroslav Hrubý
    const [selectedDocs, setSelectedDocs] = useState<string[] | undefined[]>([]);
26 c6109e2d Lukáš Vlček
27 06d1aa21 Jaroslav Hrubý
    useEffect(() => {
28 c6109e2d Lukáš Vlček
        async function fetchData() {
29
            let docs = (await documentController.documentsGet(0, 1000)).data.documents;
30 9bfa1e39 Jaroslav Hrubý
            docs?.forEach((doc) => (doc.key = doc.id));
31
32 c6109e2d Lukáš Vlček
            if (!docs) {
33
                setDocuments([]);
34
            } else {
35
                setDocuments(docs);
36
            }
37
        }
38
39 06d1aa21 Jaroslav Hrubý
        if (!redirecting && role === 'ADMINISTRATOR') {
40 c6109e2d Lukáš Vlček
            fetchData();
41 8c45ccb0 hrubyjar
        }
42
    }, [logout, redirecting, role, router]);
43
44 9bfa1e39 Jaroslav Hrubý
    const showAssignModal = () => {
45
        if (selectedDocs.length == 0) {
46
            ShowToast('Vyberte dokument pro přiřazení', 'warning', 3000, 'top-end');
47
        } else {
48
            setVisibleAssign(true);
49
        }
50
    };
51
52
    const showAddModal = () => {
53
        setVisibleAdd(true);
54 4a7bae81 Jaroslav Hrubý
    };
55
56
    const hideModal = () => {
57 9bfa1e39 Jaroslav Hrubý
        setVisibleAdd(false);
58
        setVisibleAssign(false);
59 8c45ccb0 hrubyjar
    };
60
61 c6109e2d Lukáš Vlček
    const columns = [
62
        {
63
            title: 'Název dokumentu',
64
            dataIndex: 'name',
65
            key: 'name',
66
        },
67
        {
68
            title: 'Délka',
69
            dataIndex: 'length',
70
            key: 'length',
71
        },
72
        {
73
            title: 'Anotátoři',
74
            dataIndex: 'annotatingUsers',
75
            key: 'annotatingUsers',
76
            render: (columnData: UserInfo[], record: DocumentListInfo, index: number) => {
77
                return (
78
                    <div>
79
                        {columnData.map((e) => (
80
                            <span
81
                                key={e.username + '.' + record.id}
82
                                title={e.username ?? ''}
83
                            >
84
                                <FontAwesomeIcon
85
                                    icon={faUser}
86
                                    title={e.username ?? ''}
87
                                    className={'me-2'}
88
                                />
89
                            </span>
90
                        ))}
91
                    </div>
92
                );
93
            },
94
        },
95
    ];
96
97 9bfa1e39 Jaroslav Hrubý
    const rowSelection = {
98
        onChange: (selectedRowKeys: React.Key[], selectedRows: DocumentListInfo[]) => {
99
            setSelectedDocs(selectedRows.map((row) => row.id));
100
        },
101
    };
102
103 8c45ccb0 hrubyjar
    return redirecting || role !== 'ADMINISTRATOR' ? null : (
104
        <MainLayout>
105
            <Typography.Title level={2}>
106
                <FontAwesomeIcon icon={faFileLines} /> Dokumenty
107
            </Typography.Title>
108 9bfa1e39 Jaroslav Hrubý
            <Button type={'primary'} onClick={showAddModal}>
109 8c45ccb0 hrubyjar
                Nahrát dokument
110
            </Button>
111 9bfa1e39 Jaroslav Hrubý
            <Button onClick={showAssignModal}>Přiřadit dokumenty</Button>
112
            {visibleAdd && <AddDocumentModal onCancel={hideModal} />}
113
            {visibleAssign && (
114
                <AssignDocumentModal documentsIds={selectedDocs} onCancel={hideModal} />
115
            )}
116 c6109e2d Lukáš Vlček
117
            <Table
118 9bfa1e39 Jaroslav Hrubý
                rowSelection={{
119
                    type: 'checkbox',
120
                    ...rowSelection,
121
                }}
122 c6109e2d Lukáš Vlček
                columns={columns}
123
                dataSource={documents}
124 9bfa1e39 Jaroslav Hrubý
                size="middle"
125
                scroll={{ y: 600 }}
126 c6109e2d Lukáš Vlček
            />
127 8c45ccb0 hrubyjar
        </MainLayout>
128
    );
129
}
130
131
export default AdminDocumentPage;