Projekt

Obecné

Profil

Stáhnout (7.71 KB) Statistiky
| Větev: | Tag: | Revize:
1
import 'antd/dist/antd.css';
2
import React, { useContext, useEffect, useState } from 'react';
3

    
4
import { useUnauthRedirect } from '../../../hooks';
5
import { useRouter } from 'next/router';
6
import { Button, Table, Typography } from 'antd';
7
import { faFileLines, faUser } from '@fortawesome/free-solid-svg-icons';
8
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
9
import { LoggedUserContext } from '../../../contexts/LoggedUserContext';
10
import { MainLayout } from '../../../layouts/MainLayout';
11
import AddDocumentModal from '../../../components/modals/AddDocumentModal';
12
import {
13
    DocumentListInfo,
14
    DocumentListResponse,
15
    DocumentUserInfo,
16
    EState,
17
} from '../../../api';
18
import { documentController } from '../../../controllers';
19
import AssignDocumentModal from '../../../components/modals/AssignDocumentModal';
20
import { ShowToast } from '../../../utils/alerts';
21
import { TableDocInfo } from '../../../components/types/TableDocInfo';
22
import {
23
    getAnnotationStateColor,
24
    getNameTruncated,
25
    getUserInfoAlt,
26
} from '../../../utils/strings';
27
import { ABadge, BadgeStyle } from '../../../components/common/ABadge';
28
import SetRequiredAnnotationsCountModal from '../../../components/modals/SetRequiredAnnotationsCountModal';
29

    
30
function AdminDocumentPage() {
31
    const redirecting = useUnauthRedirect('/login');
32
    const { logout, role } = useContext(LoggedUserContext);
33
    const [visibleAdd, setVisibleAdd] = React.useState(false);
34
    const [visibleAssign, setVisibleAssign] = React.useState(false);
35
    const [visibleSetCount, setVisibleSetCount] = React.useState(false);
36

    
37
    const router = useRouter();
38

    
39
    const [documents, setDocuments] = useState<TableDocInfo[]>([]);
40
    const [selectedDocs, setSelectedDocs] = useState<string[] | undefined[]>([]);
41

    
42
    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

    
50
        if (!docs) {
51
            setDocuments([]);
52
        } else {
53
            setDocuments(tableDocs);
54
        }
55
    }
56

    
57
    useEffect(() => {
58
        if (!redirecting && role === 'ADMINISTRATOR') {
59
            fetchData();
60
        }
61
    }, [logout, redirecting, role, router]);
62

    
63
    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
    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
    const showAddModal = () => {
83
        setVisibleAdd(true);
84
    };
85

    
86
    const hideModal = () => {
87
        fetchData();
88
        setVisibleAdd(false);
89
        setVisibleAssign(false);
90
        setVisibleSetCount(false);
91
    };
92

    
93
    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
        {
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
                                (record.annotatingUsers?.length ?? 0) >=
131
                                (record.requiredAnnotations ?? 0)
132
                                    ? 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
        {
147
            title: 'Anotátoři',
148
            dataIndex: 'annotatingUsers',
149
            key: 'annotatingUsers',
150
            render: (
151
                columnData: DocumentUserInfo[],
152
                record: DocumentListInfo,
153
                index: number
154
            ) => {
155
                return (
156
                    <div>
157
                        {columnData.map((e) => (
158
                            <span
159
                                key={e.username + '.' + record.id}
160
                                title={getUserInfoAlt(e) + '\nStav: ' + e.state}
161
                                style={{
162
                                    color: getAnnotationStateColor(e.state),
163
                                    padding: 3,
164
                                }}
165
                                className={'me-3'}
166
                            >
167
                                <FontAwesomeIcon
168
                                    icon={faUser}
169
                                    title={getUserInfoAlt(e)}
170
                                    className={'me-2'}
171
                                />
172
                                {getNameTruncated(e)}
173
                            </span>
174
                        ))}
175
                    </div>
176
                );
177
            },
178
        },
179
    ];
180

    
181
    const rowSelection = {
182
        onChange: (selectedRowKeys: React.Key[], selectedRows: DocumentListInfo[]) => {
183
            // @ts-ignore
184
            setSelectedDocs(selectedRows.map((row) => row.id));
185
        },
186
    };
187

    
188
    return redirecting || role !== 'ADMINISTRATOR' ? null : (
189
        <MainLayout>
190
            <Typography.Title level={2}>
191
                <FontAwesomeIcon icon={faFileLines} /> Dokumenty
192
            </Typography.Title>
193
            <Button type={'primary'} onClick={showAddModal}>
194
                Nahrát dokument
195
            </Button>
196
            <Button onClick={showAssignModal}>Přiřadit dokumenty</Button>
197
            <Button onClick={showRequiredAnnotationsCountModal}>
198
                Nastavit požadovaný počet anotací
199
            </Button>
200
            {visibleAdd && <AddDocumentModal onCancel={hideModal} />}
201
            {visibleAssign && (
202
                <AssignDocumentModal documentsIds={selectedDocs} onCancel={hideModal} />
203
            )}
204
            {visibleSetCount && (
205
                <SetRequiredAnnotationsCountModal
206
                    documentsIds={selectedDocs}
207
                    onCancel={hideModal}
208
                />
209
            )}
210

    
211
            <Table
212
                rowSelection={{
213
                    type: 'checkbox',
214
                    ...rowSelection,
215
                }}
216
                columns={columns}
217
                dataSource={documents}
218
                size="middle"
219
                scroll={{ y: 600 }}
220
            />
221
        </MainLayout>
222
    );
223
}
224

    
225
export default AdminDocumentPage;
    (1-1/1)