Projekt

Obecné

Profil

Stáhnout (7.71 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 87a3d8d1 Lukáš Vlček
import { Button, Table, Typography } from 'antd';
7 c6109e2d Lukáš Vlček
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
    DocumentListInfo,
14 87a3d8d1 Lukáš Vlček
    DocumentListResponse,
15 ff61085f Lukáš Vlček
    DocumentUserInfo,
16
    EState,
17
} from '../../../api';
18 87a3d8d1 Lukáš Vlček
import { documentController } from '../../../controllers';
19 9bfa1e39 Jaroslav Hrubý
import AssignDocumentModal from '../../../components/modals/AssignDocumentModal';
20
import { ShowToast } from '../../../utils/alerts';
21 43d49a98 Jaroslav Hrubý
import { TableDocInfo } from '../../../components/types/TableDocInfo';
22 ff61085f Lukáš Vlček
import {
23
    getAnnotationStateColor,
24
    getNameTruncated,
25
    getUserInfoAlt,
26
} from '../../../utils/strings';
27 87a3d8d1 Lukáš Vlček
import { ABadge, BadgeStyle } from '../../../components/common/ABadge';
28 e7eca311 Lukáš Vlček
import SetRequiredAnnotationsCountModal from '../../../components/modals/SetRequiredAnnotationsCountModal';
29 8c45ccb0 hrubyjar
30
function AdminDocumentPage() {
31
    const redirecting = useUnauthRedirect('/login');
32
    const { logout, role } = useContext(LoggedUserContext);
33 9bfa1e39 Jaroslav Hrubý
    const [visibleAdd, setVisibleAdd] = React.useState(false);
34
    const [visibleAssign, setVisibleAssign] = React.useState(false);
35 e7eca311 Lukáš Vlček
    const [visibleSetCount, setVisibleSetCount] = React.useState(false);
36
37 8c45ccb0 hrubyjar
    const router = useRouter();
38
39 43d49a98 Jaroslav Hrubý
    const [documents, setDocuments] = useState<TableDocInfo[]>([]);
40 9bfa1e39 Jaroslav Hrubý
    const [selectedDocs, setSelectedDocs] = useState<string[] | undefined[]>([]);
41 c6109e2d Lukáš Vlček
42 43d49a98 Jaroslav Hrubý
    async function fetchData() {
43
        const docs = (await documentController.documentsGet(0, 1000)).data.documents;
44
45
        // @ts-ignore
46
        const tableDocs: TableDocInfo[] = docs?.map((doc, index) => {
47
            return { key: index, ...doc };
48
        });
49 9bfa1e39 Jaroslav Hrubý
50 43d49a98 Jaroslav Hrubý
        if (!docs) {
51
            setDocuments([]);
52
        } else {
53
            setDocuments(tableDocs);
54 c6109e2d Lukáš Vlček
        }
55 43d49a98 Jaroslav Hrubý
    }
56 c6109e2d Lukáš Vlček
57 43d49a98 Jaroslav Hrubý
    useEffect(() => {
58 06d1aa21 Jaroslav Hrubý
        if (!redirecting && role === 'ADMINISTRATOR') {
59 c6109e2d Lukáš Vlček
            fetchData();
60 8c45ccb0 hrubyjar
        }
61
    }, [logout, redirecting, role, router]);
62
63 9bfa1e39 Jaroslav Hrubý
    const showAssignModal = () => {
64
        if (selectedDocs.length == 0) {
65
            ShowToast('Vyberte dokument pro přiřazení', 'warning', 3000, 'top-end');
66
        } else {
67
            setVisibleAssign(true);
68
        }
69
    };
70 e7eca311 Lukáš Vlček
    const showRequiredAnnotationsCountModal = () => {
71
        if (selectedDocs.length == 0) {
72
            ShowToast(
73
                'Vyberte dokument, pro které chcete nastavit požadovaný počet anotací',
74
                'warning',
75
                3000,
76
                'top-end'
77
            );
78
        } else {
79
            setVisibleSetCount(true);
80
        }
81
    };
82 9bfa1e39 Jaroslav Hrubý
    const showAddModal = () => {
83
        setVisibleAdd(true);
84 4a7bae81 Jaroslav Hrubý
    };
85
86
    const hideModal = () => {
87 43d49a98 Jaroslav Hrubý
        fetchData();
88 9bfa1e39 Jaroslav Hrubý
        setVisibleAdd(false);
89
        setVisibleAssign(false);
90 e7eca311 Lukáš Vlček
        setVisibleSetCount(false);
91 8c45ccb0 hrubyjar
    };
92
93 c6109e2d Lukáš Vlček
    const columns = [
94
        {
95
            title: 'Název dokumentu',
96
            dataIndex: 'name',
97
            key: 'name',
98
        },
99
        {
100
            title: 'Délka',
101
            dataIndex: 'length',
102
            key: 'length',
103
        },
104 87a3d8d1 Lukáš Vlček
        {
105
            title: 'Dokončeno | přiřazeno | vyžadováno',
106
            key: 'annotationCounts',
107
            render: (
108
                columnData: DocumentListResponse,
109
                record: DocumentListInfo,
110
                index: number
111
            ) => {
112
                const finished =
113
                    record.annotatingUsers?.filter((d) => d.state === EState.Done)
114
                        .length ?? 0;
115
116
                return (
117
                    <div>
118
                        <ABadge
119
                            style={
120
                                finished === record.annotatingUsers?.length
121
                                    ? BadgeStyle.SUCCESS
122
                                    : BadgeStyle.WARNING
123
                            }
124
                        >
125
                            {finished}
126
                        </ABadge>
127
                        {' | '}
128
                        <ABadge
129
                            style={
130 ef2143b8 Lukáš Vlček
                                (record.annotatingUsers?.length ?? 0) >=
131
                                (record.requiredAnnotations ?? 0)
132 87a3d8d1 Lukáš Vlček
                                    ? BadgeStyle.SUCCESS
133
                                    : BadgeStyle.WARNING
134
                            }
135
                        >
136
                            {record.annotatingUsers?.length}
137
                        </ABadge>
138
                        {' | '}
139
                        <ABadge style={BadgeStyle.GENERAL}>
140
                            {record.requiredAnnotations}
141
                        </ABadge>
142
                    </div>
143
                );
144
            },
145
        },
146 c6109e2d Lukáš Vlček
        {
147
            title: 'Anotátoři',
148
            dataIndex: 'annotatingUsers',
149
            key: 'annotatingUsers',
150 ff61085f Lukáš Vlček
            render: (
151
                columnData: DocumentUserInfo[],
152
                record: DocumentListInfo,
153
                index: number
154
            ) => {
155 c6109e2d Lukáš Vlček
                return (
156
                    <div>
157
                        {columnData.map((e) => (
158
                            <span
159
                                key={e.username + '.' + record.id}
160 ff61085f Lukáš Vlček
                                title={getUserInfoAlt(e) + '\nStav: ' + e.state}
161
                                style={{
162
                                    color: getAnnotationStateColor(e.state),
163
                                    padding: 3,
164
                                }}
165
                                className={'me-3'}
166 c6109e2d Lukáš Vlček
                            >
167
                                <FontAwesomeIcon
168
                                    icon={faUser}
169 ff61085f Lukáš Vlček
                                    title={getUserInfoAlt(e)}
170 c6109e2d Lukáš Vlček
                                    className={'me-2'}
171
                                />
172 ff61085f Lukáš Vlček
                                {getNameTruncated(e)}
173 c6109e2d Lukáš Vlček
                            </span>
174
                        ))}
175
                    </div>
176
                );
177
            },
178
        },
179
    ];
180
181 9bfa1e39 Jaroslav Hrubý
    const rowSelection = {
182
        onChange: (selectedRowKeys: React.Key[], selectedRows: DocumentListInfo[]) => {
183 43d49a98 Jaroslav Hrubý
            // @ts-ignore
184 9bfa1e39 Jaroslav Hrubý
            setSelectedDocs(selectedRows.map((row) => row.id));
185
        },
186
    };
187
188 8c45ccb0 hrubyjar
    return redirecting || role !== 'ADMINISTRATOR' ? null : (
189
        <MainLayout>
190
            <Typography.Title level={2}>
191
                <FontAwesomeIcon icon={faFileLines} /> Dokumenty
192
            </Typography.Title>
193 9bfa1e39 Jaroslav Hrubý
            <Button type={'primary'} onClick={showAddModal}>
194 8c45ccb0 hrubyjar
                Nahrát dokument
195
            </Button>
196 9bfa1e39 Jaroslav Hrubý
            <Button onClick={showAssignModal}>Přiřadit dokumenty</Button>
197 e7eca311 Lukáš Vlček
            <Button onClick={showRequiredAnnotationsCountModal}>
198
                Nastavit požadovaný počet anotací
199
            </Button>
200 9bfa1e39 Jaroslav Hrubý
            {visibleAdd && <AddDocumentModal onCancel={hideModal} />}
201
            {visibleAssign && (
202
                <AssignDocumentModal documentsIds={selectedDocs} onCancel={hideModal} />
203
            )}
204 e7eca311 Lukáš Vlček
            {visibleSetCount && (
205
                <SetRequiredAnnotationsCountModal
206
                    documentsIds={selectedDocs}
207
                    onCancel={hideModal}
208
                />
209
            )}
210 c6109e2d Lukáš Vlček
211
            <Table
212 9bfa1e39 Jaroslav Hrubý
                rowSelection={{
213
                    type: 'checkbox',
214
                    ...rowSelection,
215
                }}
216 c6109e2d Lukáš Vlček
                columns={columns}
217
                dataSource={documents}
218 9bfa1e39 Jaroslav Hrubý
                size="middle"
219
                scroll={{ y: 600 }}
220 c6109e2d Lukáš Vlček
            />
221 8c45ccb0 hrubyjar
        </MainLayout>
222
    );
223
}
224
225
export default AdminDocumentPage;