Projekt

Obecné

Profil

Stáhnout (4.42 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, Input, Space, Table, Tag, Typography } from 'antd';
7
import { faFileLines } 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 { userController } from '../../../controllers';
12
import { AnnotationListInfo, EState } from '../../../api';
13
import {
14
    getAnnotationStateString,
15
    getAnnotationStateColor,
16
} from '../../../utils/strings';
17
import {
18
    CheckCircleOutlined,
19
    ClockCircleOutlined,
20
    SyncOutlined,
21
} from '@ant-design/icons';
22
import { getColumnSearchProps, getLocaleProps } from '../../../utils/tableUtils';
23

    
24
function UserDocumentPage() {
25
    const redirecting = useUnauthRedirect('/login');
26
    const { logout, role } = useContext(LoggedUserContext);
27
    const [documents, setDocuments] = useState<AnnotationListInfo[]>([]);
28
    const router = useRouter();
29

    
30
    useEffect(() => {
31
        async function fetchData() {
32
            let docs = (await userController.userAnnotationsGet()).data.annotations;
33
            if (!docs) {
34
                setDocuments([]);
35
            } else {
36
                setDocuments(docs);
37
            }
38
        }
39

    
40
        if (!redirecting /* && role === 'ANNOTATOR'*/) {
41
            // admin can also fetch data if he likes so
42
            fetchData();
43
        }
44
    }, [logout, redirecting, role, router]);
45

    
46
    const columns = [
47
        {
48
            title: 'Název dokumentu',
49
            dataIndex: 'documentName',
50
            key: 'documentName',
51
            ...getColumnSearchProps('documentName', 'název'),
52
            // @ts-ignore
53
            sorter: (a, b) => a.documentName.localeCompare(b.documentName),
54
        },
55
        {
56
            title: 'Stav anotace',
57
            key: 'state',
58
            dataIndex: 'state',
59
            render: (state: EState) => {
60
                const color = getAnnotationStateColor(state);
61
                const label = getAnnotationStateString(state);
62
                let icon = <CheckCircleOutlined />;
63
                if (state === 'NEW') {
64
                    icon = <ClockCircleOutlined />;
65
                }
66
                if (state === 'IN_PROGRESS') {
67
                    icon = <SyncOutlined />;
68
                }
69

    
70
                return (
71
                    <Tag icon={icon} color={color} key={label}>
72
                        {label.toUpperCase()}
73
                    </Tag>
74
                );
75
            },
76
            filters: [
77
                {
78
                    text: 'Nový',
79
                    value: 'NEW',
80
                },
81
                {
82
                    text: 'Rozpracováno',
83
                    value: 'IN_PROGRESS',
84
                },
85
                {
86
                    text: 'Hotovo',
87
                    value: 'DONE',
88
                },
89
            ],
90
            // @ts-ignore
91
            onFilter: (value, record) => record.state.indexOf(value) === 0,
92
            // @ts-ignore
93
            sorter: (a, b) => a.state.length - b.state.length,
94
            sortDirections: ['descend', 'ascend'],
95
        },
96
        {
97
            title: '',
98
            key: 'action',
99
            dataIndex: 'annotationId',
100
            render: (anotationId: number) => (
101
                <Button
102
                    key={anotationId}
103
                    type={'primary'}
104
                    onClick={() =>
105
                        router.push({
106
                            pathname: '/annotation/[annotationId]',
107
                            query: { annotationId: anotationId, final: false },
108
                        })
109
                    }
110
                >
111
                    Anotovat
112
                </Button>
113
            ),
114
        },
115
    ];
116

    
117
    return redirecting /*|| role !== 'ANNOTATOR' */ ? null : (
118
        <MainLayout>
119
            <Typography.Title level={2}>
120
                <FontAwesomeIcon icon={faFileLines} /> Dokumenty
121
            </Typography.Title>
122
            <Table
123
                locale={{ ...getLocaleProps() }}
124
                // @ts-ignore
125
                columns={columns}
126
                // @ts-ignore
127
                dataSource={documents}
128
                size="small"
129
                scroll={{ y: 500 }}
130
            />
131
        </MainLayout>
132
    );
133
}
134

    
135
export default UserDocumentPage;
    (1-1/1)