Projekt

Obecné

Profil

« Předchozí | Další » 

Revize dda6e56e

Přidáno uživatelem Václav Honzík před asi 2 roky(ů)

Send text query to the api

re #9629

Zobrazit rozdíly:

frontend/src/features/TrackingTool/PlaintextUpload.tsx
1 1
import {
2 2
    Button,
3
    Dialog,
3 4
    DialogContent,
4 5
    DialogTitle,
5 6
    Stack,
6 7
    TextField,
7 8
} from '@mui/material'
8 9
import { useFormik } from 'formik'
9
import { Fragment } from 'react'
10
import ButtonOpenableDialog from '../Reusables/ButtonOpenableDialog'
10
import { Fragment, FunctionComponent, useState } from 'react'
11 11
import SendIcon from '@mui/icons-material/Send'
12 12
import ClearIcon from '@mui/icons-material/Clear'
13
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()
13 29

  
14
const PlaintextUpload = () => {
15 30
    const formik = useFormik({
16 31
        initialValues: {
17 32
            text: '',
18 33
        },
19
        onSubmit: () => {
34
        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
                    console.log(data)
60
                }
61
            } catch (err: any) {
62
                dispatch(showNotification)
63
                closeDialog = true
64
            }
65
            setSubmitButtonDisabled(false)
66

  
67
            if (closeDialog) {
68
                onCloseDialog()
69
            }
20 70
        },
21 71
    })
22 72

  
73
    const onCloseDialog = () => {
74
        formik.resetForm()
75
        setOpen(false)
76
    }
77

  
23 78
    const resetForm = () => {
24 79
        formik.resetForm()
25 80
    }
26 81

  
27 82
    return (
28 83
        <Fragment>
29
            <ButtonOpenableDialog
30
                buttonText="Plaintext"
31
                buttonColor="primary"
32
                buttonVariant="contained"
84
            <Button variant="contained" onClick={() => setOpen(true)}>
85
                Text
86
            </Button>
87
            <Dialog
88
                open={open}
89
                fullWidth={true}
90
                onClose={onCloseDialog}
91
                maxWidth="lg"
33 92
            >
34 93
                <DialogTitle>Plaintext Input</DialogTitle>
35 94
                <DialogContent>
36
                    <form onSubmit={formik.handleChange}>
95
                    <form onSubmit={formik.handleSubmit}>
37 96
                        <TextField
38 97
                            sx={{ my: 2 }}
39 98
                            fullWidth
......
58 117
                            >
59 118
                                Clear
60 119
                            </Button>
61
                            <Button type="submit" variant="contained" startIcon={<SendIcon />}>
120
                            <Button
121
                                type="submit"
122
                                variant="contained"
123
                                disabled={submitButtonDisabled}
124
                                startIcon={<SendIcon />}
125
                            >
62 126
                                Submit
63 127
                            </Button>
64 128
                        </Stack>
65 129
                    </form>
66 130
                </DialogContent>
67
            </ButtonOpenableDialog>
131
            </Dialog>
68 132
        </Fragment>
69 133
    )
70 134
}

Také k dispozici: Unified diff