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 AttachmentIcon from "@mui/icons-material/Attachment"
|
15
|
import DeleteIcon from "@mui/icons-material/Delete"
|
16
|
import SendIcon from "@mui/icons-material/Send"
|
17
|
import { useDispatch } from "react-redux"
|
18
|
import { sendTextForProcessing } from "./trackingToolThunks"
|
19
|
|
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
|
|
32
|
const validationSchema = yup.object().shape({
|
33
|
file: yup.mixed().required("File is required"),
|
34
|
})
|
35
|
|
36
|
const formik = useFormik({
|
37
|
initialValues,
|
38
|
validationSchema,
|
39
|
onSubmit: async (values) => {
|
40
|
setFileProcessing(true)
|
41
|
const reader = new FileReader()
|
42
|
reader.readAsText(values.file as File)
|
43
|
reader.onload = async () => {
|
44
|
dispatch(sendTextForProcessing(reader.result as string))
|
45
|
setFileProcessing(false)
|
46
|
}
|
47
|
},
|
48
|
})
|
49
|
|
50
|
// Callback when user selects the file
|
51
|
const onFileSelected = (event: any) => {
|
52
|
const file = event.currentTarget.files[0]
|
53
|
if (file) {
|
54
|
setFilename(file.name)
|
55
|
formik.setFieldValue("file", file)
|
56
|
}
|
57
|
}
|
58
|
|
59
|
const onClose = () => {
|
60
|
if (fileProcessing) {
|
61
|
return
|
62
|
}
|
63
|
setFilename(undefined)
|
64
|
formik.resetForm()
|
65
|
}
|
66
|
|
67
|
const onClearSelectedFile = () => {
|
68
|
setFilename(undefined)
|
69
|
formik.setFieldValue("file", undefined)
|
70
|
}
|
71
|
|
72
|
return (
|
73
|
<ButtonOpenableDialog
|
74
|
buttonText="File"
|
75
|
buttonColor="primary"
|
76
|
buttonVariant="contained"
|
77
|
onCloseCallback={onClose}
|
78
|
maxWidth="xs"
|
79
|
>
|
80
|
<DialogTitle>Upload New File</DialogTitle>
|
81
|
<DialogContent>
|
82
|
<form onSubmit={formik.handleSubmit}>
|
83
|
{!filename ? (
|
84
|
<Fragment>
|
85
|
<Stack
|
86
|
direction="row"
|
87
|
justifyContent="flex-end"
|
88
|
alignItems="center"
|
89
|
>
|
90
|
<Button
|
91
|
variant="contained"
|
92
|
color="primary"
|
93
|
component="label"
|
94
|
// size="small"
|
95
|
startIcon={<AttachmentIcon />}
|
96
|
>
|
97
|
Select File
|
98
|
<input
|
99
|
id="file"
|
100
|
name="file"
|
101
|
type="file"
|
102
|
hidden
|
103
|
onChange={onFileSelected}
|
104
|
/>
|
105
|
</Button>
|
106
|
</Stack>
|
107
|
</Fragment>
|
108
|
) : (
|
109
|
<Fragment>
|
110
|
<Stack direction="row" spacing={1}>
|
111
|
<Typography
|
112
|
sx={
|
113
|
{
|
114
|
// textOverflow: 'ellipsis',
|
115
|
// overflow: 'hidden',
|
116
|
}
|
117
|
}
|
118
|
variant="body1"
|
119
|
>
|
120
|
Selected File:{" "}
|
121
|
</Typography>
|
122
|
<Typography
|
123
|
sx={{
|
124
|
textOverflow: "ellipsis",
|
125
|
overflow: "hidden",
|
126
|
}}
|
127
|
// color="text.secondary"
|
128
|
component={Link}
|
129
|
// download={(formik.values?.file as File).}
|
130
|
// align="right"
|
131
|
>
|
132
|
{filename}
|
133
|
</Typography>
|
134
|
</Stack>
|
135
|
<Stack
|
136
|
direction="row"
|
137
|
justifyContent="flex-end"
|
138
|
alignItems="center"
|
139
|
spacing={2}
|
140
|
sx={{ mt: 2 }}
|
141
|
>
|
142
|
<Button
|
143
|
// sx={{ mb: 2, mt: 1 }}
|
144
|
variant="contained"
|
145
|
size="small"
|
146
|
endIcon={<DeleteIcon />}
|
147
|
onClick={onClearSelectedFile}
|
148
|
>
|
149
|
Remove Selection
|
150
|
</Button>
|
151
|
<Button
|
152
|
size="small"
|
153
|
type="submit"
|
154
|
variant="contained"
|
155
|
startIcon={<SendIcon />}
|
156
|
>
|
157
|
Submit
|
158
|
</Button>
|
159
|
</Stack>
|
160
|
</Fragment>
|
161
|
)}
|
162
|
</form>
|
163
|
</DialogContent>
|
164
|
</ButtonOpenableDialog>
|
165
|
)
|
166
|
}
|
167
|
|
168
|
export default FileUpload
|