1 |
a7ae217f
|
Vaclav Honzik
|
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 |
c0b66eaf
|
Vaclav Honzik
|
import AttachFileIcon from '@mui/icons-material/AttachFile'
|
18 |
|
|
import TextSnippetIcon from '@mui/icons-material/TextSnippet'
|
19 |
a7ae217f
|
Vaclav Honzik
|
|
20 |
|
|
interface UploadValues {
|
21 |
|
|
file?: File
|
22 |
|
|
}
|
23 |
|
|
|
24 |
|
|
const initialValues: UploadValues = {}
|
25 |
|
|
|
26 |
|
|
const FileUpload = () => {
|
27 |
|
|
const dispatch = useDispatch()
|
28 |
|
|
|
29 |
|
|
const [filename, setFilename] = useState<string | undefined>(undefined)
|
30 |
|
|
const [fileProcessing, setFileProcessing] = useState(false)
|
31 |
7aa6893d
|
Vaclav Honzik
|
const [open, setOpen] = useState(false)
|
32 |
a7ae217f
|
Vaclav Honzik
|
|
33 |
|
|
const validationSchema = yup.object().shape({
|
34 |
|
|
file: yup.mixed().required('File is required'),
|
35 |
|
|
})
|
36 |
|
|
|
37 |
|
|
const formik = useFormik({
|
38 |
|
|
initialValues,
|
39 |
|
|
validationSchema,
|
40 |
|
|
onSubmit: async (values) => {
|
41 |
|
|
setFileProcessing(true)
|
42 |
|
|
const reader = new FileReader()
|
43 |
|
|
reader.readAsText(values.file as File)
|
44 |
|
|
reader.onload = async () => {
|
45 |
|
|
dispatch(sendTextForProcessing(reader.result as string))
|
46 |
|
|
setFileProcessing(false)
|
47 |
|
|
}
|
48 |
|
|
},
|
49 |
|
|
})
|
50 |
|
|
|
51 |
|
|
// Callback when user selects the file
|
52 |
|
|
const onFileSelected = (event: any) => {
|
53 |
|
|
const file = event.currentTarget.files[0]
|
54 |
|
|
if (file) {
|
55 |
|
|
setFilename(file.name)
|
56 |
|
|
formik.setFieldValue('file', file)
|
57 |
|
|
}
|
58 |
|
|
}
|
59 |
|
|
|
60 |
|
|
const onClose = () => {
|
61 |
|
|
if (fileProcessing) {
|
62 |
|
|
return
|
63 |
|
|
}
|
64 |
|
|
setFilename(undefined)
|
65 |
|
|
formik.resetForm()
|
66 |
|
|
}
|
67 |
|
|
|
68 |
|
|
const onClearSelectedFile = () => {
|
69 |
|
|
setFilename(undefined)
|
70 |
|
|
formik.setFieldValue('file', undefined)
|
71 |
|
|
}
|
72 |
|
|
|
73 |
|
|
return (
|
74 |
|
|
<ButtonOpenableDialog
|
75 |
|
|
buttonText="File"
|
76 |
|
|
buttonColor="primary"
|
77 |
|
|
buttonVariant="contained"
|
78 |
|
|
onCloseCallback={onClose}
|
79 |
|
|
maxWidth="xs"
|
80 |
7aa6893d
|
Vaclav Honzik
|
open={open}
|
81 |
|
|
setOpen={setOpen}
|
82 |
c0b66eaf
|
Vaclav Honzik
|
startIcon={<AttachFileIcon />}
|
83 |
a7ae217f
|
Vaclav Honzik
|
>
|
84 |
|
|
<DialogTitle>Upload New File</DialogTitle>
|
85 |
|
|
<DialogContent>
|
86 |
|
|
<SingleFileSelectionForm
|
87 |
|
|
onFileSelected={onFileSelected}
|
88 |
|
|
onClearSelectedFile={onClearSelectedFile}
|
89 |
|
|
filename={filename}
|
90 |
|
|
formik={formik}
|
91 |
|
|
/>
|
92 |
|
|
</DialogContent>
|
93 |
|
|
</ButtonOpenableDialog>
|
94 |
|
|
)
|
95 |
|
|
}
|
96 |
|
|
|
97 |
|
|
export default FileUpload
|