Projekt

Obecné

Profil

Stáhnout (4.89 KB) Statistiky
| Větev: | Tag: | Revize:
1
import { DialogContent, DialogTitle, Grid, TextField } from '@mui/material'
2
import { FunctionComponent, useState } from 'react'
3
import ButtonOpenableDialog from '../../Reusables/ButtonOpenableDialog'
4
import * as yup from 'yup'
5
import { useDispatch } from 'react-redux'
6
import { mergeWithCurrentPath } from '../trackingToolSlice'
7
import { MapPointType, PathVariant } from '../Map/pathUtils'
8
import generateUuid from '../../../utils/id/uuidGenerator'
9
import { useFormik } from 'formik'
10
import ContextMenuDialogProps from './contextMenuDialogProps'
11

    
12
interface AddCatalogItemFromCoords {
13
    latitude: number
14
    longitude: number
15
    name: string
16
}
17

    
18
const AddFromCoordinatesDialog: FunctionComponent<ContextMenuDialogProps> = ({
19
    latLng,
20
    closeContextMenu,
21
}) => {
22
    const [open, setOpen] = useState(false)
23
    const dispatch = useDispatch()
24

    
25
    const formik = useFormik({
26
        initialValues: {
27
            latitude: latLng[0],
28
            longitude: latLng[1],
29
            name: '',
30
        } as AddCatalogItemFromCoords,
31
        validationSchema: yup.object().shape({
32
            latitude: yup.number().required('Latitude is required'),
33
            longitude: yup.number().required('Longitude is required'),
34
            name: yup.string().required('Name is required'),
35
        }),
36
        onSubmit: (values: AddCatalogItemFromCoords) => {
37
            dispatch(
38
                mergeWithCurrentPath([
39
                    {
40
                        id: generateUuid(),
41
                        idx: -1,
42
                        addToPath: false,
43
                        type: MapPointType.FromCoordinates,
44
                        catalogItem: {
45
                            name: values.name,
46
                            latitude: values.latitude,
47
                            longitude: values.longitude,
48
                        },
49
                    },
50
                ] as PathVariant)
51
            )
52
        },
53
    })
54

    
55
    return (
56
        <ButtonOpenableDialog
57
            buttonText="Create Location"
58
            buttonColor="primary"
59
            buttonVariant="text"
60
            onCloseCallback={closeContextMenu}
61
            maxWidth="xs"
62
            open={open}
63
            setOpen={setOpen}
64
            size="small"
65
        >
66
            <DialogTitle>Add New Location From Coordinates</DialogTitle>
67
            <DialogContent>
68
                <form onSubmit={formik.handleSubmit}>
69
                    <Grid container spacing={1} sx={{ mt: 1, mb: 1 }}>
70
                        <Grid item xs={12} md={6}>
71
                            <TextField
72
                                fullWidth
73
                                label="Latitude"
74
                                name="latitude"
75
                                type="number"
76
                                variant="outlined"
77
                                value={formik.values.latitude}
78
                                onChange={formik.handleChange}
79
                                error={
80
                                    Boolean(formik.errors.latitude) &&
81
                                    formik.touched.latitude
82
                                }
83
                                helperText={
84
                                    formik.errors.latitude &&
85
                                    formik.touched.latitude
86
                                }
87
                            />
88
                        </Grid>
89
                        <Grid item xs={12} md={6}>
90
                            <TextField
91
                                fullWidth
92
                                label="Longitude"
93
                                name="longitude"
94
                                type="number"
95
                                variant="outlined"
96
                                value={formik.values.longitude}
97
                                onChange={formik.handleChange}
98
                                error={
99
                                    Boolean(formik.errors.longitude) &&
100
                                    formik.touched.longitude
101
                                }
102
                                helperText={
103
                                    formik.errors.longitude &&
104
                                    formik.touched.longitude
105
                                }
106
                            />
107
                        </Grid>
108
                    </Grid>
109
                    <TextField
110
                        fullWidth
111
                        label="Name"
112
                        name="name"
113
                        variant="outlined"
114
                        value={formik.values.name}
115
                        onChange={formik.handleChange}
116
                        error={
117
                            Boolean(formik.errors.name) && formik.touched.name
118
                        }
119
                        helperText={formik.errors.name && formik.touched.name}
120
                    />
121
                </form>
122
            </DialogContent>
123
        </ButtonOpenableDialog>
124
    )
125
}
126

    
127
export default AddFromCoordinatesDialog
(1-1/4)