Projekt

Obecné

Profil

Stáhnout (2.58 KB) Statistiky
| Větev: | Tag: | Revize:
1
import {
2
    Button,
3
    DialogContent,
4
    DialogTitle,
5
    Link,
6
    Stack,
7
    Typography,
8
} from '@mui/material'
9
import { useFormik } from 'formik'
10
import { Fragment, useState } from 'react'
11
import * as yup from 'yup'
12
import axiosInstance from '../../../api/api'
13
import ButtonOpenableDialog from '../../Reusables/ButtonOpenableDialog'
14
import { useDispatch } from 'react-redux'
15
import { sendTextForProcessing } from '../trackingToolThunks'
16
import SingleFileSelectionForm from '../../Reusables/SingleFileSelectionForm'
17

    
18
interface UploadValues {
19
    file?: File
20
}
21

    
22
const initialValues: UploadValues = {}
23

    
24
const FileUpload = () => {
25
    const dispatch = useDispatch()
26

    
27
    const [filename, setFilename] = useState<string | undefined>(undefined)
28
    const [fileProcessing, setFileProcessing] = useState(false)
29
    const [open, setOpen] = useState(false)
30

    
31
    const validationSchema = yup.object().shape({
32
        file: yup.mixed().required('File is required'),
33
    })
34

    
35
    const formik = useFormik({
36
        initialValues,
37
        validationSchema,
38
        onSubmit: async (values) => {
39
            setFileProcessing(true)
40
            const reader = new FileReader()
41
            reader.readAsText(values.file as File)
42
            reader.onload = async () => {
43
                dispatch(sendTextForProcessing(reader.result as string))
44
                setFileProcessing(false)
45
            }
46
        },
47
    })
48

    
49
    // Callback when user selects the file
50
    const onFileSelected = (event: any) => {
51
        const file = event.currentTarget.files[0]
52
        if (file) {
53
            setFilename(file.name)
54
            formik.setFieldValue('file', file)
55
        }
56
    }
57

    
58
    const onClose = () => {
59
        if (fileProcessing) {
60
            return
61
        }
62
        setFilename(undefined)
63
        formik.resetForm()
64
    }
65

    
66
    const onClearSelectedFile = () => {
67
        setFilename(undefined)
68
        formik.setFieldValue('file', undefined)
69
    }
70

    
71
    return (
72
        <ButtonOpenableDialog
73
            buttonText="File"
74
            buttonColor="primary"
75
            buttonVariant="contained"
76
            onCloseCallback={onClose}
77
            maxWidth="xs"
78
            open={open}
79
            setOpen={setOpen}
80
        >
81
            <DialogTitle>Upload New File</DialogTitle>
82
            <DialogContent>
83
                <SingleFileSelectionForm
84
                    onFileSelected={onFileSelected}
85
                    onClearSelectedFile={onClearSelectedFile}
86
                    filename={filename}
87
                    formik={formik}
88
                />
89
            </DialogContent>
90
        </ButtonOpenableDialog>
91
    )
92
}
93

    
94
export default FileUpload
(1-1/5)