Projekt

Obecné

Profil

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