Projekt

Obecné

Profil

Stáhnout (3.89 KB) Statistiky
| Větev: | Tag: | Revize:
1
import {
2
    Button,
3
    DialogContent,
4
    DialogTitle,
5
    Stack,
6
    TextField,
7
} from '@mui/material'
8
import { useFormik } from 'formik'
9
import { Fragment, useEffect, useState } from 'react'
10
import SendIcon from '@mui/icons-material/Send'
11
import ClearIcon from '@mui/icons-material/Clear'
12
import { useDispatch, useSelector } from 'react-redux'
13
import { RootState } from '../../redux/store'
14
import { sendTextForProcessing } from '../trackingToolThunks'
15
import * as yup from 'yup'
16
import { resetDialogApiCallSuccess } from '../trackingToolSlice'
17
import ButtonOpenableDialog from '../../Reusables/ButtonOpenableDialog'
18
import TextSnippetIcon from '@mui/icons-material/TextSnippet'
19

    
20
const PlaintextUpload = () => {
21
    const loading = useSelector(
22
        (state: RootState) => state.trackingTool.isLoading
23
    )
24
    const [open, setOpen] = useState(false) // controls whether the dialog is open
25

    
26
    // This controls whether to keep dialog open after sending the request to the API
27
    const dialogApiCallSuccess = useSelector(
28
        (state: RootState) => state.trackingTool.dialogApiCallSuccess
29
    )
30

    
31
    const dispatch = useDispatch()
32

    
33
    const validationSchema = yup.object().shape({
34
        text: yup.mixed().required('Text is required'),
35
    })
36

    
37
    // Form control
38
    const formik = useFormik({
39
        initialValues: {
40
            text: '',
41
        },
42
        validationSchema,
43
        onSubmit: async () => {
44
            // Dispatch the thunk
45
            dispatch(sendTextForProcessing(formik.values.text))
46
        },
47
    })
48

    
49
    const onCloseDialog = () => {
50
        formik.resetForm()
51
        setOpen(false)
52
    }
53

    
54
    const resetForm = () => {
55
        formik.resetForm()
56
    }
57

    
58
    useEffect(() => {
59
        if (!dialogApiCallSuccess) {
60
            return
61
        }
62
        dispatch(resetDialogApiCallSuccess())
63
        setOpen(false)
64
    }, [dialogApiCallSuccess, dispatch])
65

    
66
    return (
67
        <Fragment>
68
            <ButtonOpenableDialog
69
                open={open}
70
                setOpen={setOpen}
71
                maxWidth="lg"
72
                buttonColor="primary"
73
                buttonVariant="contained"
74
                buttonText="Text"
75
                startIcon={<TextSnippetIcon />}
76
                onCloseCallback={onCloseDialog}
77
            >
78
                <DialogTitle>Plaintext Input</DialogTitle>
79
                <DialogContent>
80
                    <form onSubmit={formik.handleSubmit}>
81
                        <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
                            <Button
106
                                type="submit"
107
                                variant="contained"
108
                                startIcon={<SendIcon />}
109
                                disabled={loading}
110
                            >
111
                                Submit
112
                            </Button>
113
                        </Stack>
114
                    </form>
115
                </DialogContent>
116
            </ButtonOpenableDialog>
117
        </Fragment>
118
    )
119
}
120

    
121
export default PlaintextUpload
(5-5/5)