Projekt

Obecné

Profil

Stáhnout (3.55 KB) Statistiky
| Větev: | Tag: | Revize:
1
import { Button, Modal, Upload } from 'antd';
2
import { InboxOutlined } from '@ant-design/icons';
3
import 'antd/dist/antd.css';
4
import React from 'react';
5
import { DocumentAddInfo, EAddDocumentFormat } from '../../api';
6
import { documentController } from '../../controllers';
7
import { ShowToast } from '../../utils/alerts';
8

    
9
const { Dragger } = Upload;
10

    
11
interface ModalProps {
12
    onCancel: () => void;
13
}
14

    
15
/**
16
 * Creates a modal window that loads documents to the app.
17
 * @returns The modal window.
18
 */
19
const AddDocumentModal: React.FC<ModalProps> = ({ onCancel }) => {
20
    /**
21
     * Handles successful closing of the modal window.
22
     */
23
    const handleOk = () => {
24
        onCancel();
25
    };
26

    
27
    /**
28
     * Handles cancelling of the model window.
29
     */
30
    const handleCancel = () => {
31
        onCancel();
32
    };
33

    
34
    const [upload, setUpload] = React.useState(false);
35
    const tempDocs: DocumentAddInfo[] = [];
36

    
37
    const handleUpload = () => {
38
        if (tempDocs.length !== 0) {
39
            setUpload(true);
40
            documentController.documentsPost({ documents: tempDocs }).then((r) => {
41
                ShowToast(
42
                    'Nahrávání souborů proběhlo úspěšně',
43
                    'success',
44
                    3000,
45
                    'top-end'
46
                );
47
                setUpload(false);
48
                onCancel();
49
            });
50
        }
51
    };
52
    /**
53
     * Settings of a file loader.
54
     */
55
    const props = {
56
        name: 'file',
57
        multiple: true,
58
        directory: false,
59
        onRemove: (file: any) => {
60
            let docInfo = tempDocs.find((doc) => {
61
                return doc.name === file.name;
62
            });
63
            if (docInfo) {
64
                const index = tempDocs.indexOf(docInfo);
65
                const newFileList = tempDocs.slice();
66
                newFileList.splice(index, 1);
67
            }
68
        },
69
        beforeUpload: (file: any) => {
70
            let docInfo: DocumentAddInfo = { name: file.name };
71
            if ('application/x-zip-compressed' === file.type) {
72
                docInfo.format = EAddDocumentFormat.Zip;
73
            } else if ('text/html' === file.type || 'text/plain' === file.type) {
74
                docInfo.format = EAddDocumentFormat.Textfile;
75
            }
76

    
77
            const reader = new FileReader();
78
            reader.onload = (e) => {
79
                if (e.target && e.target.result) {
80
                    let content = e.target.result.toString();
81
                    docInfo.content = content.split('base64,').pop();
82
                }
83
            };
84
            reader.readAsDataURL(file);
85
            tempDocs.push(docInfo);
86
        },
87
    };
88

    
89
    return (
90
        <Modal
91
            title="Nový dokument"
92
            onOk={handleOk}
93
            visible={true}
94
            onCancel={handleCancel}
95
            footer={[
96
                <Button key="back" onClick={handleCancel}>
97
                    Storno
98
                </Button>,
99
                <Button
100
                    key="submit"
101
                    type="primary"
102
                    onClick={handleUpload}
103
                    loading={upload}
104
                >
105
                    Nahrát
106
                </Button>,
107
            ]}
108
        >
109
            <Dragger {...props} accept={'.txt, .zip, .html'}>
110
                <p className="ant-upload-drag-icon">
111
                    <InboxOutlined />
112
                </p>
113
                <p className="ant-upload-text">
114
                    Soubory lze nahrát stisknutím nebo přetažením...
115
                </p>
116
            </Dragger>
117
        </Modal>
118
    );
119
};
120

    
121
export default AddDocumentModal;
(1-1/11)