1
|
import {
|
2
|
Button,
|
3
|
Dialog,
|
4
|
DialogContent,
|
5
|
DialogTitle,
|
6
|
Stack,
|
7
|
TextField,
|
8
|
} from '@mui/material'
|
9
|
import { useFormik } from 'formik'
|
10
|
import { Fragment, useEffect, useState } from 'react'
|
11
|
import SendIcon from '@mui/icons-material/Send'
|
12
|
import ClearIcon from '@mui/icons-material/Clear'
|
13
|
import { useDispatch, useSelector } from 'react-redux'
|
14
|
import { RootState } from '../../redux/store'
|
15
|
import { sendTextForProcessing } from '../trackingToolThunks'
|
16
|
import * as yup from 'yup'
|
17
|
import { resetDialogApiCallSuccess } from '../trackingToolSlice'
|
18
|
|
19
|
const PlaintextUpload = () => {
|
20
|
const loading = useSelector(
|
21
|
(state: RootState) => state.trackingTool.isLoading
|
22
|
)
|
23
|
const [open, setOpen] = useState(false) // controls whether the dialog is open
|
24
|
|
25
|
// This controls whether to keep dialog open after sending the request to the API
|
26
|
const dialogApiCallSuccess = useSelector(
|
27
|
(state: RootState) => state.trackingTool.dialogApiCallSuccess
|
28
|
)
|
29
|
|
30
|
const dispatch = useDispatch()
|
31
|
|
32
|
const validationSchema = yup.object().shape({
|
33
|
text: yup.mixed().required('Text is required'),
|
34
|
})
|
35
|
|
36
|
const formik = useFormik({
|
37
|
initialValues: {
|
38
|
text: '',
|
39
|
},
|
40
|
validationSchema,
|
41
|
onSubmit: async () => {
|
42
|
// Dispatch the thunk
|
43
|
dispatch(sendTextForProcessing(formik.values.text))
|
44
|
},
|
45
|
})
|
46
|
|
47
|
const onCloseDialog = () => {
|
48
|
formik.resetForm()
|
49
|
setOpen(false)
|
50
|
}
|
51
|
|
52
|
const resetForm = () => {
|
53
|
formik.resetForm()
|
54
|
}
|
55
|
|
56
|
useEffect(() => {
|
57
|
if (!dialogApiCallSuccess) {
|
58
|
return
|
59
|
}
|
60
|
dispatch(resetDialogApiCallSuccess())
|
61
|
setOpen(false)
|
62
|
}, [dialogApiCallSuccess, dispatch])
|
63
|
|
64
|
return (
|
65
|
<Fragment>
|
66
|
<Button variant="contained" onClick={() => setOpen(true)}>
|
67
|
Text
|
68
|
</Button>
|
69
|
<Dialog
|
70
|
open={open}
|
71
|
fullWidth={true}
|
72
|
onClose={onCloseDialog}
|
73
|
maxWidth="lg"
|
74
|
>
|
75
|
<DialogTitle>Plaintext Input</DialogTitle>
|
76
|
<DialogContent>
|
77
|
<form onSubmit={formik.handleSubmit}>
|
78
|
<TextField
|
79
|
sx={{ my: 2 }}
|
80
|
fullWidth
|
81
|
multiline
|
82
|
label="Plaintext input"
|
83
|
rows={10}
|
84
|
name="text"
|
85
|
value={formik.values.text}
|
86
|
onChange={formik.handleChange}
|
87
|
/>
|
88
|
<Stack
|
89
|
alignItems="flex-end"
|
90
|
justifyContent="flex-end"
|
91
|
spacing={2}
|
92
|
direction="row"
|
93
|
>
|
94
|
<Button
|
95
|
variant="contained"
|
96
|
color="secondary"
|
97
|
onClick={resetForm}
|
98
|
startIcon={<ClearIcon />}
|
99
|
>
|
100
|
Clear
|
101
|
</Button>
|
102
|
<Button
|
103
|
type="submit"
|
104
|
variant="contained"
|
105
|
startIcon={<SendIcon />}
|
106
|
disabled={loading}
|
107
|
>
|
108
|
Submit
|
109
|
</Button>
|
110
|
</Stack>
|
111
|
</form>
|
112
|
</DialogContent>
|
113
|
</Dialog>
|
114
|
</Fragment>
|
115
|
)
|
116
|
}
|
117
|
|
118
|
export default PlaintextUpload
|