Projekt

Obecné

Profil

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