Projekt

Obecné

Profil

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