1 |
0d90d67b
|
Vaclav Honzik
|
import {
|
2 |
|
|
Button,
|
3 |
dda6e56e
|
Vaclav Honzik
|
Dialog,
|
4 |
0d90d67b
|
Vaclav Honzik
|
DialogContent,
|
5 |
|
|
DialogTitle,
|
6 |
|
|
Stack,
|
7 |
|
|
TextField,
|
8 |
|
|
} from '@mui/material'
|
9 |
|
|
import { useFormik } from 'formik'
|
10 |
dda6e56e
|
Vaclav Honzik
|
import { Fragment, FunctionComponent, useState } from 'react'
|
11 |
0d90d67b
|
Vaclav Honzik
|
import SendIcon from '@mui/icons-material/Send'
|
12 |
|
|
import ClearIcon from '@mui/icons-material/Clear'
|
13 |
dda6e56e
|
Vaclav Honzik
|
import { PathDto } from '../../swagger/data-contracts'
|
14 |
|
|
import axiosInstance from '../../api/api'
|
15 |
|
|
import { useDispatch } from 'react-redux'
|
16 |
|
|
import { showNotification } from '../Notification/notificationSlice'
|
17 |
|
|
|
18 |
|
|
export interface PlaintextUploadProps {
|
19 |
|
|
setPaths: React.Dispatch<React.SetStateAction<PathDto | undefined>>
|
20 |
|
|
}
|
21 |
|
|
|
22 |
|
|
const PlaintextUpload: FunctionComponent<PlaintextUploadProps> = ({
|
23 |
|
|
setPaths,
|
24 |
|
|
}) => {
|
25 |
|
|
const [submitButtonDisabled, setSubmitButtonDisabled] = useState(false)
|
26 |
|
|
const [open, setOpen] = useState(false) // controls whether the dialog is open
|
27 |
|
|
|
28 |
|
|
const dispatch = useDispatch()
|
29 |
0d90d67b
|
Vaclav Honzik
|
|
30 |
|
|
const formik = useFormik({
|
31 |
|
|
initialValues: {
|
32 |
|
|
text: '',
|
33 |
|
|
},
|
34 |
dda6e56e
|
Vaclav Honzik
|
onSubmit: async () => {
|
35 |
|
|
let closeDialog = false
|
36 |
|
|
setSubmitButtonDisabled(true)
|
37 |
|
|
try {
|
38 |
|
|
const { data, status } = await axiosInstance.post('path', {
|
39 |
|
|
text: formik.values.text,
|
40 |
|
|
})
|
41 |
|
|
|
42 |
|
|
if (status !== 200) {
|
43 |
|
|
dispatch(
|
44 |
|
|
showNotification({
|
45 |
|
|
message:
|
46 |
|
|
'Error while fetching map, please try again later.',
|
47 |
|
|
severity: 'error',
|
48 |
|
|
})
|
49 |
|
|
)
|
50 |
|
|
} else {
|
51 |
|
|
dispatch(
|
52 |
|
|
showNotification({
|
53 |
|
|
message: 'Map fetched successfully.',
|
54 |
|
|
severity: 'success',
|
55 |
|
|
})
|
56 |
|
|
)
|
57 |
|
|
setPaths(data)
|
58 |
|
|
closeDialog = true
|
59 |
|
|
console.log(data)
|
60 |
|
|
}
|
61 |
|
|
} catch (err: any) {
|
62 |
|
|
dispatch(showNotification)
|
63 |
|
|
closeDialog = true
|
64 |
|
|
}
|
65 |
|
|
setSubmitButtonDisabled(false)
|
66 |
|
|
|
67 |
|
|
if (closeDialog) {
|
68 |
|
|
onCloseDialog()
|
69 |
|
|
}
|
70 |
0d90d67b
|
Vaclav Honzik
|
},
|
71 |
|
|
})
|
72 |
|
|
|
73 |
dda6e56e
|
Vaclav Honzik
|
const onCloseDialog = () => {
|
74 |
|
|
formik.resetForm()
|
75 |
|
|
setOpen(false)
|
76 |
|
|
}
|
77 |
|
|
|
78 |
0d90d67b
|
Vaclav Honzik
|
const resetForm = () => {
|
79 |
|
|
formik.resetForm()
|
80 |
|
|
}
|
81 |
|
|
|
82 |
|
|
return (
|
83 |
|
|
<Fragment>
|
84 |
dda6e56e
|
Vaclav Honzik
|
<Button variant="contained" onClick={() => setOpen(true)}>
|
85 |
|
|
Text
|
86 |
|
|
</Button>
|
87 |
|
|
<Dialog
|
88 |
|
|
open={open}
|
89 |
|
|
fullWidth={true}
|
90 |
|
|
onClose={onCloseDialog}
|
91 |
|
|
maxWidth="lg"
|
92 |
0d90d67b
|
Vaclav Honzik
|
>
|
93 |
|
|
<DialogTitle>Plaintext Input</DialogTitle>
|
94 |
|
|
<DialogContent>
|
95 |
dda6e56e
|
Vaclav Honzik
|
<form onSubmit={formik.handleSubmit}>
|
96 |
0d90d67b
|
Vaclav Honzik
|
<TextField
|
97 |
|
|
sx={{ my: 2 }}
|
98 |
|
|
fullWidth
|
99 |
|
|
multiline
|
100 |
|
|
label="Plaintext input"
|
101 |
|
|
rows={10}
|
102 |
|
|
name="text"
|
103 |
|
|
value={formik.values.text}
|
104 |
|
|
onChange={formik.handleChange}
|
105 |
|
|
/>
|
106 |
|
|
<Stack
|
107 |
|
|
alignItems="flex-end"
|
108 |
|
|
justifyContent="flex-end"
|
109 |
|
|
spacing={2}
|
110 |
|
|
direction="row"
|
111 |
|
|
>
|
112 |
|
|
<Button
|
113 |
|
|
variant="contained"
|
114 |
|
|
color="secondary"
|
115 |
|
|
onClick={resetForm}
|
116 |
|
|
startIcon={<ClearIcon />}
|
117 |
|
|
>
|
118 |
|
|
Clear
|
119 |
|
|
</Button>
|
120 |
dda6e56e
|
Vaclav Honzik
|
<Button
|
121 |
|
|
type="submit"
|
122 |
|
|
variant="contained"
|
123 |
|
|
disabled={submitButtonDisabled}
|
124 |
|
|
startIcon={<SendIcon />}
|
125 |
|
|
>
|
126 |
0d90d67b
|
Vaclav Honzik
|
Submit
|
127 |
|
|
</Button>
|
128 |
|
|
</Stack>
|
129 |
|
|
</form>
|
130 |
|
|
</DialogContent>
|
131 |
dda6e56e
|
Vaclav Honzik
|
</Dialog>
|
132 |
0d90d67b
|
Vaclav Honzik
|
</Fragment>
|
133 |
|
|
)
|
134 |
|
|
}
|
135 |
|
|
|
136 |
|
|
export default PlaintextUpload
|