Projekt

Obecné

Profil

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