1 |
484adbb5
|
Vaclav Honzik
|
import { DialogContent, DialogTitle, IconButton, Stack, Typography } from '@mui/material'
|
2 |
a7ae217f
|
Vaclav Honzik
|
import { useFormik } from 'formik'
|
3 |
14588cb6
|
Vaclav Honzik
|
import { useState } from 'react'
|
4 |
a7ae217f
|
Vaclav Honzik
|
import * as yup from 'yup'
|
5 |
|
|
import ButtonOpenableDialog from '../../Reusables/ButtonOpenableDialog'
|
6 |
14588cb6
|
Vaclav Honzik
|
import { useDispatch, useSelector } from 'react-redux'
|
7 |
a7ae217f
|
Vaclav Honzik
|
import { sendTextForProcessing } from '../trackingToolThunks'
|
8 |
|
|
import SingleFileSelectionForm from '../../Reusables/SingleFileSelectionForm'
|
9 |
c0b66eaf
|
Vaclav Honzik
|
import AttachFileIcon from '@mui/icons-material/AttachFile'
|
10 |
14588cb6
|
Vaclav Honzik
|
import { RootState } from '../../redux/store'
|
11 |
484adbb5
|
Vaclav Honzik
|
import CloseIcon from '@mui/icons-material/Close'
|
12 |
a7ae217f
|
Vaclav Honzik
|
|
13 |
|
|
interface UploadValues {
|
14 |
|
|
file?: File
|
15 |
|
|
}
|
16 |
|
|
|
17 |
|
|
const initialValues: UploadValues = {}
|
18 |
|
|
|
19 |
|
|
const FileUpload = () => {
|
20 |
|
|
const dispatch = useDispatch()
|
21 |
|
|
|
22 |
|
|
const [filename, setFilename] = useState<string | undefined>(undefined)
|
23 |
|
|
const [fileProcessing, setFileProcessing] = useState(false)
|
24 |
7aa6893d
|
Vaclav Honzik
|
const [open, setOpen] = useState(false)
|
25 |
a7ae217f
|
Vaclav Honzik
|
|
26 |
|
|
const validationSchema = yup.object().shape({
|
27 |
|
|
file: yup.mixed().required('File is required'),
|
28 |
|
|
})
|
29 |
|
|
|
30 |
14588cb6
|
Vaclav Honzik
|
const loading = useSelector(
|
31 |
|
|
(state: RootState) => state.trackingTool.isLoading
|
32 |
|
|
)
|
33 |
|
|
|
34 |
a7ae217f
|
Vaclav Honzik
|
const formik = useFormik({
|
35 |
|
|
initialValues,
|
36 |
|
|
validationSchema,
|
37 |
|
|
onSubmit: async (values) => {
|
38 |
|
|
setFileProcessing(true)
|
39 |
|
|
const reader = new FileReader()
|
40 |
|
|
reader.readAsText(values.file as File)
|
41 |
|
|
reader.onload = async () => {
|
42 |
|
|
dispatch(sendTextForProcessing(reader.result as string))
|
43 |
|
|
setFileProcessing(false)
|
44 |
|
|
}
|
45 |
|
|
},
|
46 |
|
|
})
|
47 |
|
|
|
48 |
|
|
// Callback when user selects the file
|
49 |
|
|
const onFileSelected = (event: any) => {
|
50 |
|
|
const file = event.currentTarget.files[0]
|
51 |
|
|
if (file) {
|
52 |
|
|
setFilename(file.name)
|
53 |
|
|
formik.setFieldValue('file', file)
|
54 |
|
|
}
|
55 |
|
|
}
|
56 |
|
|
|
57 |
|
|
const onClose = () => {
|
58 |
|
|
if (fileProcessing) {
|
59 |
|
|
return
|
60 |
|
|
}
|
61 |
|
|
setFilename(undefined)
|
62 |
|
|
formik.resetForm()
|
63 |
|
|
}
|
64 |
|
|
|
65 |
|
|
const onClearSelectedFile = () => {
|
66 |
|
|
setFilename(undefined)
|
67 |
|
|
formik.setFieldValue('file', undefined)
|
68 |
|
|
}
|
69 |
|
|
|
70 |
|
|
return (
|
71 |
|
|
<ButtonOpenableDialog
|
72 |
|
|
buttonText="File"
|
73 |
|
|
buttonColor="primary"
|
74 |
|
|
buttonVariant="contained"
|
75 |
|
|
onCloseCallback={onClose}
|
76 |
|
|
maxWidth="xs"
|
77 |
7aa6893d
|
Vaclav Honzik
|
open={open}
|
78 |
|
|
setOpen={setOpen}
|
79 |
c0b66eaf
|
Vaclav Honzik
|
startIcon={<AttachFileIcon />}
|
80 |
a7ae217f
|
Vaclav Honzik
|
>
|
81 |
484adbb5
|
Vaclav Honzik
|
<DialogTitle>
|
82 |
|
|
<Stack
|
83 |
|
|
direction="row"
|
84 |
|
|
justifyContent="space-between"
|
85 |
|
|
alignItems="center"
|
86 |
|
|
>
|
87 |
|
|
<Typography variant="h5" fontWeight="600">
|
88 |
|
|
Upload File
|
89 |
|
|
</Typography>
|
90 |
|
|
<IconButton onClick={() => {
|
91 |
|
|
if (fileProcessing) {
|
92 |
|
|
return
|
93 |
|
|
}
|
94 |
|
|
setOpen(false)
|
95 |
|
|
}}>
|
96 |
|
|
<CloseIcon />
|
97 |
|
|
</IconButton>
|
98 |
|
|
</Stack>
|
99 |
|
|
</DialogTitle>
|
100 |
a7ae217f
|
Vaclav Honzik
|
<DialogContent>
|
101 |
|
|
<SingleFileSelectionForm
|
102 |
|
|
onFileSelected={onFileSelected}
|
103 |
|
|
onClearSelectedFile={onClearSelectedFile}
|
104 |
|
|
filename={filename}
|
105 |
|
|
formik={formik}
|
106 |
14588cb6
|
Vaclav Honzik
|
loading={loading}
|
107 |
a7ae217f
|
Vaclav Honzik
|
/>
|
108 |
|
|
</DialogContent>
|
109 |
|
|
</ButtonOpenableDialog>
|
110 |
|
|
)
|
111 |
|
|
}
|
112 |
|
|
|
113 |
|
|
export default FileUpload
|