Projekt

Obecné

Profil

Stáhnout (4.3 KB) Statistiky
| Větev: | Tag: | Revize:
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
                }
60
            } catch (err: any) {
61
                dispatch(showNotification)
62
                closeDialog = true
63
            }
64
            setSubmitButtonDisabled(false)
65
66
            if (closeDialog) {
67
                onCloseDialog()
68
            }
69 0d90d67b Vaclav Honzik
        },
70
    })
71
72 dda6e56e Vaclav Honzik
    const onCloseDialog = () => {
73
        formik.resetForm()
74
        setOpen(false)
75
    }
76
77 0d90d67b Vaclav Honzik
    const resetForm = () => {
78
        formik.resetForm()
79
    }
80
81
    return (
82
        <Fragment>
83 dda6e56e Vaclav Honzik
            <Button variant="contained" onClick={() => setOpen(true)}>
84
                Text
85
            </Button>
86
            <Dialog
87
                open={open}
88
                fullWidth={true}
89
                onClose={onCloseDialog}
90
                maxWidth="lg"
91 0d90d67b Vaclav Honzik
            >
92
                <DialogTitle>Plaintext Input</DialogTitle>
93
                <DialogContent>
94 dda6e56e Vaclav Honzik
                    <form onSubmit={formik.handleSubmit}>
95 0d90d67b Vaclav Honzik
                        <TextField
96
                            sx={{ my: 2 }}
97
                            fullWidth
98
                            multiline
99
                            label="Plaintext input"
100
                            rows={10}
101
                            name="text"
102
                            value={formik.values.text}
103
                            onChange={formik.handleChange}
104
                        />
105
                        <Stack
106
                            alignItems="flex-end"
107
                            justifyContent="flex-end"
108
                            spacing={2}
109
                            direction="row"
110
                        >
111
                            <Button
112
                                variant="contained"
113
                                color="secondary"
114
                                onClick={resetForm}
115
                                startIcon={<ClearIcon />}
116
                            >
117
                                Clear
118
                            </Button>
119 dda6e56e Vaclav Honzik
                            <Button
120
                                type="submit"
121
                                variant="contained"
122
                                disabled={submitButtonDisabled}
123
                                startIcon={<SendIcon />}
124
                            >
125 0d90d67b Vaclav Honzik
                                Submit
126
                            </Button>
127
                        </Stack>
128
                    </form>
129
                </DialogContent>
130 dda6e56e Vaclav Honzik
            </Dialog>
131 0d90d67b Vaclav Honzik
        </Fragment>
132
    )
133
}
134
135
export default PlaintextUpload