Revize 8c57f958
Přidáno uživatelem Václav Honzík před asi 2 roky(ů)
frontend/src/features/TrackingTool/PlaintextUpload.tsx | ||
---|---|---|
7 | 7 |
TextField, |
8 | 8 |
} from '@mui/material' |
9 | 9 |
import { useFormik } from 'formik' |
10 |
import { Fragment, FunctionComponent, useState } from 'react' |
|
10 |
import { Fragment, FunctionComponent, useEffect, useState } from 'react'
|
|
11 | 11 |
import SendIcon from '@mui/icons-material/Send' |
12 | 12 |
import ClearIcon from '@mui/icons-material/Clear' |
13 | 13 |
import { PathDto } from '../../swagger/data-contracts' |
14 | 14 |
import axiosInstance from '../../api/api' |
15 |
import { useDispatch } from 'react-redux' |
|
15 |
import { useDispatch, useSelector } from 'react-redux'
|
|
16 | 16 |
import { showNotification } from '../Notification/notificationSlice' |
17 |
import { RootState } from '../redux/store' |
|
18 |
import { sendTextForProcessing } from './trackingToolThunks' |
|
19 |
import * as yup from 'yup' |
|
20 |
import { resetDialogApiCallSuccess } from './trackingToolSlice' |
|
17 | 21 |
|
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) |
|
22 |
const PlaintextUpload = () => { |
|
23 |
const loading = useSelector( |
|
24 |
(state: RootState) => state.trackingTool.isLoading |
|
25 |
) |
|
26 | 26 |
const [open, setOpen] = useState(false) // controls whether the dialog is open |
27 | 27 |
|
28 |
// This controls whether to keep dialog open after sending the request to the API |
|
29 |
const dialogApiCallSuccess = useSelector( |
|
30 |
(state: RootState) => state.trackingTool.dialogApiCallSuccess |
|
31 |
) |
|
32 |
|
|
28 | 33 |
const dispatch = useDispatch() |
29 | 34 |
|
35 |
const validationSchema = yup.object().shape({ |
|
36 |
text: yup.mixed().required('Text is required'), |
|
37 |
}) |
|
38 |
|
|
30 | 39 |
const formik = useFormik({ |
31 | 40 |
initialValues: { |
32 | 41 |
text: '', |
33 | 42 |
}, |
43 |
validationSchema, |
|
34 | 44 |
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 |
} |
|
60 |
} catch (err: any) { |
|
61 |
dispatch(showNotification) |
|
62 |
closeDialog = true |
|
63 |
} |
|
64 |
setSubmitButtonDisabled(false) |
|
65 |
|
|
66 |
if (closeDialog) { |
|
67 |
onCloseDialog() |
|
68 |
} |
|
45 |
// Dispatch the thunk |
|
46 |
dispatch(sendTextForProcessing(formik.values.text)) |
|
69 | 47 |
}, |
70 | 48 |
}) |
71 | 49 |
|
... | ... | |
78 | 56 |
formik.resetForm() |
79 | 57 |
} |
80 | 58 |
|
59 |
useEffect(() => { |
|
60 |
if (!dialogApiCallSuccess) { |
|
61 |
return |
|
62 |
} |
|
63 |
dispatch(resetDialogApiCallSuccess()) |
|
64 |
setOpen(false) |
|
65 |
}, [dialogApiCallSuccess, dispatch]) |
|
66 |
|
|
81 | 67 |
return ( |
82 | 68 |
<Fragment> |
83 | 69 |
<Button variant="contained" onClick={() => setOpen(true)}> |
... | ... | |
119 | 105 |
<Button |
120 | 106 |
type="submit" |
121 | 107 |
variant="contained" |
122 |
disabled={submitButtonDisabled} |
|
123 | 108 |
startIcon={<SendIcon />} |
109 |
disabled={loading} |
|
124 | 110 |
> |
125 | 111 |
Submit |
126 | 112 |
</Button> |
Také k dispozici: Unified diff
refactor to use Redux instead of localstate
re #9629