Projekt

Obecné

Profil

Stáhnout (3.67 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 a7ae217f Vaclav Honzik
import { Fragment, useEffect, useState } from 'react'
11 0d90d67b Vaclav Honzik
import SendIcon from '@mui/icons-material/Send'
12
import ClearIcon from '@mui/icons-material/Clear'
13 8c57f958 Vaclav Honzik
import { useDispatch, useSelector } from 'react-redux'
14 a7ae217f Vaclav Honzik
import { RootState } from '../../redux/store'
15
import { sendTextForProcessing } from '../trackingToolThunks'
16 8c57f958 Vaclav Honzik
import * as yup from 'yup'
17 a7ae217f Vaclav Honzik
import { resetDialogApiCallSuccess } from '../trackingToolSlice'
18 dda6e56e Vaclav Honzik
19 8c57f958 Vaclav Honzik
const PlaintextUpload = () => {
20
    const loading = useSelector(
21
        (state: RootState) => state.trackingTool.isLoading
22
    )
23 dda6e56e Vaclav Honzik
    const [open, setOpen] = useState(false) // controls whether the dialog is open
24
25 8c57f958 Vaclav Honzik
    // 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 dda6e56e Vaclav Honzik
    const dispatch = useDispatch()
31 0d90d67b Vaclav Honzik
32 8c57f958 Vaclav Honzik
    const validationSchema = yup.object().shape({
33
        text: yup.mixed().required('Text is required'),
34
    })
35
36 0d90d67b Vaclav Honzik
    const formik = useFormik({
37
        initialValues: {
38
            text: '',
39
        },
40 8c57f958 Vaclav Honzik
        validationSchema,
41 dda6e56e Vaclav Honzik
        onSubmit: async () => {
42 8c57f958 Vaclav Honzik
            // Dispatch the thunk
43
            dispatch(sendTextForProcessing(formik.values.text))
44 0d90d67b Vaclav Honzik
        },
45
    })
46
47 dda6e56e Vaclav Honzik
    const onCloseDialog = () => {
48
        formik.resetForm()
49
        setOpen(false)
50
    }
51
52 0d90d67b Vaclav Honzik
    const resetForm = () => {
53
        formik.resetForm()
54
    }
55
56 8c57f958 Vaclav Honzik
    useEffect(() => {
57
        if (!dialogApiCallSuccess) {
58
            return
59
        }
60
        dispatch(resetDialogApiCallSuccess())
61
        setOpen(false)
62
    }, [dialogApiCallSuccess, dispatch])
63
64 0d90d67b Vaclav Honzik
    return (
65
        <Fragment>
66 dda6e56e Vaclav Honzik
            <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 0d90d67b Vaclav Honzik
            >
75
                <DialogTitle>Plaintext Input</DialogTitle>
76
                <DialogContent>
77 dda6e56e Vaclav Honzik
                    <form onSubmit={formik.handleSubmit}>
78 0d90d67b Vaclav Honzik
                        <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 dda6e56e Vaclav Honzik
                            <Button
103
                                type="submit"
104
                                variant="contained"
105
                                startIcon={<SendIcon />}
106 8c57f958 Vaclav Honzik
                                disabled={loading}
107 dda6e56e Vaclav Honzik
                            >
108 0d90d67b Vaclav Honzik
                                Submit
109
                            </Button>
110
                        </Stack>
111
                    </form>
112
                </DialogContent>
113 dda6e56e Vaclav Honzik
            </Dialog>
114 0d90d67b Vaclav Honzik
        </Fragment>
115
    )
116
}
117
118
export default PlaintextUpload