1
|
import { DialogContent, DialogTitle, IconButton, Stack, Typography } from '@mui/material'
|
2
|
import { useFormik } from 'formik'
|
3
|
import { useState } from 'react'
|
4
|
import * as yup from 'yup'
|
5
|
import ButtonOpenableDialog from '../../Reusables/ButtonOpenableDialog'
|
6
|
import { useDispatch, useSelector } from 'react-redux'
|
7
|
import { sendTextForProcessing } from '../trackingToolThunks'
|
8
|
import SingleFileSelectionForm from '../../Reusables/SingleFileSelectionForm'
|
9
|
import AttachFileIcon from '@mui/icons-material/AttachFile'
|
10
|
import { RootState } from '../../redux/store'
|
11
|
import CloseIcon from '@mui/icons-material/Close'
|
12
|
|
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
|
const [open, setOpen] = useState(false)
|
25
|
|
26
|
const validationSchema = yup.object().shape({
|
27
|
file: yup.mixed().required('File is required'),
|
28
|
})
|
29
|
|
30
|
const loading = useSelector(
|
31
|
(state: RootState) => state.trackingTool.isLoading
|
32
|
)
|
33
|
|
34
|
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
|
open={open}
|
78
|
setOpen={setOpen}
|
79
|
startIcon={<AttachFileIcon />}
|
80
|
>
|
81
|
<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
|
<DialogContent>
|
101
|
<SingleFileSelectionForm
|
102
|
onFileSelected={onFileSelected}
|
103
|
onClearSelectedFile={onClearSelectedFile}
|
104
|
filename={filename}
|
105
|
formik={formik}
|
106
|
loading={loading}
|
107
|
/>
|
108
|
</DialogContent>
|
109
|
</ButtonOpenableDialog>
|
110
|
)
|
111
|
}
|
112
|
|
113
|
export default FileUpload
|