Projekt

Obecné

Profil

Stáhnout (7.65 KB) Statistiky
| Větev: | Tag: | Revize:
1
import {
2
    Button,
3
    DialogContent,
4
    DialogTitle,
5
    Grid,
6
    IconButton,
7
    Paper,
8
    Stack,
9
    TextField,
10
    Typography,
11
} from '@mui/material'
12
import { Fragment, FunctionComponent, useState } from 'react'
13
import ButtonOpenableDialog from '../../Reusables/ButtonOpenableDialog'
14
import * as yup from 'yup'
15
import { useDispatch } from 'react-redux'
16
import { mergeWithCurrentPath } from '../trackingToolSlice'
17
import { MapPointType, PathVariant } from '../Map/pathUtils'
18
import generateUuid from '../../../utils/id/uuidGenerator'
19
import { useFormik } from 'formik'
20
import ContextMenuDialogProps from './contextMenuDialogProps'
21
import ThemeWrapper from '../../Theme/ThemeWrapper'
22
import CloseIcon from '@mui/icons-material/Close'
23

    
24
interface AddCatalogItemFromCoords {
25
    latitude: number
26
    longitude: number
27
    name: string
28
}
29

    
30
const AddFromCoordinatesDialog: FunctionComponent<ContextMenuDialogProps> = ({
31
    latLng,
32
    closeContextMenu,
33
}) => {
34
    const [open, setOpen] = useState(false)
35
    const dispatch = useDispatch()
36

    
37
    const formik = useFormik({
38
        initialValues: {
39
            latitude: latLng[0],
40
            longitude: latLng[1],
41
            name: '',
42
        } as AddCatalogItemFromCoords,
43
        validationSchema: yup.object().shape({
44
            latitude: yup.number().required('Latitude is required'),
45
            longitude: yup.number().required('Longitude is required'),
46
            name: yup.string().required('Name is required'),
47
        }),
48
        onSubmit: (values: AddCatalogItemFromCoords) => {
49
            dispatch(
50
                mergeWithCurrentPath([
51
                    {
52
                        id: generateUuid(),
53
                        idx: -1,
54
                        addToPath: false,
55
                        type: MapPointType.FromCoordinates,
56
                        catalogItem: {
57
                            name: values.name,
58
                            latitude: values.latitude,
59
                            longitude: values.longitude,
60
                        },
61
                    },
62
                ] as PathVariant)
63
            )
64
        },
65
    })
66

    
67
    const onClose = () => {
68
        formik.resetForm()
69
        closeContextMenu()
70
        setOpen(false)
71
    }
72

    
73
    return (
74
        <ButtonOpenableDialog
75
            buttonText="Create Location"
76
            buttonColor="primary"
77
            buttonVariant="text"
78
            onCloseCallback={onClose}
79
            maxWidth="xs"
80
            open={open}
81
            setOpen={setOpen}
82
            size="small"
83
        >
84
            <ThemeWrapper>
85
                <Paper>
86
                    <DialogTitle>
87
                        <Stack
88
                            direction="row"
89
                            justifyContent="space-between"
90
                            alignItems="center"
91
                            spacing={1}
92
                        >
93
                            <Fragment>
94
                                Add New Location From Coordinates
95
                            </Fragment>
96
                            <IconButton onClick={onClose}>
97
                                <CloseIcon />
98
                            </IconButton>
99
                        </Stack>
100
                    </DialogTitle>
101
                    <DialogContent>
102
                        <form onSubmit={formik.handleSubmit}>
103
                            <Grid
104
                                container
105
                                sx={{ mt: 1 }}
106
                                // spacing={1}
107
                                justifyContent="space-around"
108
                                alignItems="center"
109
                            >
110
                                <Grid item xs={6} md={9}>
111
                                    <TextField
112
                                        fullWidth
113
                                        label="Name"
114
                                        size="small"
115
                                        name="name"
116
                                        variant="outlined"
117
                                        value={formik.values.name}
118
                                        onChange={formik.handleChange}
119
                                        error={
120
                                            Boolean(formik.errors.name) &&
121
                                            formik.touched.name
122
                                        }
123
                                        helperText={
124
                                            formik.errors.name &&
125
                                            formik.touched.name
126
                                        }
127
                                    />
128
                                </Grid>
129
                                <Grid
130
                                    item
131
                                    container
132
                                    xs={6}
133
                                    md={3}
134
                                    justifyContent="flex-end"
135
                                >
136
                                    <Button variant="contained" type="submit" color="primary">
137
                                        Import
138
                                    </Button>
139
                                </Grid>
140
                            </Grid>
141
                            <Grid container spacing={1} sx={{ mt: 1, mb: 1 }}>
142
                                <Grid item xs={12} md={6}>
143
                                    <TextField
144
                                        fullWidth
145
                                        label="Latitude"
146
                                        name="latitude"
147
                                        type="number"
148
                                        size="small"
149
                                        variant="outlined"
150
                                        value={formik.values.latitude}
151
                                        onChange={formik.handleChange}
152
                                        error={
153
                                            Boolean(formik.errors.latitude) &&
154
                                            formik.touched.latitude
155
                                        }
156
                                        helperText={
157
                                            formik.errors.latitude &&
158
                                            formik.touched.latitude
159
                                        }
160
                                    />
161
                                </Grid>
162
                                <Grid item xs={12} md={6}>
163
                                    <TextField
164
                                        fullWidth
165
                                        label="Longitude"
166
                                        name="longitude"
167
                                        type="number"
168
                                        variant="outlined"
169
                                        size="small"
170
                                        value={formik.values.longitude}
171
                                        onChange={formik.handleChange}
172
                                        error={
173
                                            Boolean(formik.errors.longitude) &&
174
                                            formik.touched.longitude
175
                                        }
176
                                        helperText={
177
                                            formik.errors.longitude &&
178
                                            formik.touched.longitude
179
                                        }
180
                                    />
181
                                </Grid>
182
                            </Grid>
183
                        </form>
184
                    </DialogContent>
185
                </Paper>
186
            </ThemeWrapper>
187
        </ButtonOpenableDialog>
188
    )
189
}
190

    
191
export default AddFromCoordinatesDialog
(1-1/4)