Projekt

Obecné

Profil

Stáhnout (5.91 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 e97b42bc Jaroslav Hrubý
        if (documentContent) {
69
            setPreviewDocName(name);
70
            setPreviewDocContent(documentContent);
71
            setVisiblePreview(true);
72
        }
73 d7167305 Jaroslav Hrubý
    };
74
75 4a7bae81 Jaroslav Hrubý
    const hideModal = () => {
76 43d49a98 Jaroslav Hrubý
        fetchData();
77 9bfa1e39 Jaroslav Hrubý
        setVisibleAdd(false);
78
        setVisibleAssign(false);
79 d7167305 Jaroslav Hrubý
        setVisiblePreview(false);
80 8c45ccb0 hrubyjar
    };
81
82 c6109e2d Lukáš Vlček
    const columns = [
83
        {
84
            title: 'Název dokumentu',
85
            dataIndex: 'name',
86
            key: 'name',
87
        },
88
        {
89
            title: 'Délka',
90
            dataIndex: 'length',
91
            key: 'length',
92
        },
93
        {
94
            title: 'Anotátoři',
95
            dataIndex: 'annotatingUsers',
96
            key: 'annotatingUsers',
97
            render: (columnData: UserInfo[], record: DocumentListInfo, index: number) => {
98
                return (
99
                    <div>
100
                        {columnData.map((e) => (
101
                            <span
102
                                key={e.username + '.' + record.id}
103
                                title={e.username ?? ''}
104
                            >
105
                                <FontAwesomeIcon
106
                                    icon={faUser}
107
                                    title={e.username ?? ''}
108
                                    className={'me-2'}
109
                                />
110
                            </span>
111
                        ))}
112
                    </div>
113
                );
114
            },
115
        },
116 d7167305 Jaroslav Hrubý
        {
117
            title: '',
118
            key: 'action',
119
            dataIndex: ['id', 'name'],
120
            // @ts-ignore
121
            render: (text, row) => (
122
                <Button key={row.id} onClick={() => showPreviewModal(row.id, row.name)}>
123
                    Náhled
124
                </Button>
125
            ),
126
        },
127 c6109e2d Lukáš Vlček
    ];
128
129 9bfa1e39 Jaroslav Hrubý
    const rowSelection = {
130
        onChange: (selectedRowKeys: React.Key[], selectedRows: DocumentListInfo[]) => {
131 43d49a98 Jaroslav Hrubý
            // @ts-ignore
132 9bfa1e39 Jaroslav Hrubý
            setSelectedDocs(selectedRows.map((row) => row.id));
133
        },
134
    };
135
136 8c45ccb0 hrubyjar
    return redirecting || role !== 'ADMINISTRATOR' ? null : (
137
        <MainLayout>
138
            <Typography.Title level={2}>
139
                <FontAwesomeIcon icon={faFileLines} /> Dokumenty
140
            </Typography.Title>
141 9bfa1e39 Jaroslav Hrubý
            <Button type={'primary'} onClick={showAddModal}>
142 8c45ccb0 hrubyjar
                Nahrát dokument
143
            </Button>
144 9bfa1e39 Jaroslav Hrubý
            <Button onClick={showAssignModal}>Přiřadit dokumenty</Button>
145
            {visibleAdd && <AddDocumentModal onCancel={hideModal} />}
146
            {visibleAssign && (
147
                <AssignDocumentModal documentsIds={selectedDocs} onCancel={hideModal} />
148
            )}
149 d7167305 Jaroslav Hrubý
            {visiblePreview && (
150
                <DocPreviewModal
151
                    onCancel={hideModal}
152 e97b42bc Jaroslav Hrubý
                    // @ts-ignore
153 d7167305 Jaroslav Hrubý
                    documentName={previewDocName}
154 e97b42bc Jaroslav Hrubý
                    // @ts-ignore
155 d7167305 Jaroslav Hrubý
                    content={previewDocContent}
156
                />
157
            )}
158 c6109e2d Lukáš Vlček
159
            <Table
160 9bfa1e39 Jaroslav Hrubý
                rowSelection={{
161
                    type: 'checkbox',
162
                    ...rowSelection,
163
                }}
164 c6109e2d Lukáš Vlček
                columns={columns}
165
                dataSource={documents}
166 9bfa1e39 Jaroslav Hrubý
                size="middle"
167
                scroll={{ y: 600 }}
168 c6109e2d Lukáš Vlček
            />
169 8c45ccb0 hrubyjar
        </MainLayout>
170
    );
171
}
172
173
export default AdminDocumentPage;