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 '../trackingToolUtils'
|
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
|
const item = {
|
50
|
id: generateUuid(),
|
51
|
idx: -1,
|
52
|
addToPath: false,
|
53
|
type: MapPointType.FromCoordinates,
|
54
|
catalogItem: {
|
55
|
name: values.name,
|
56
|
latitude: values.latitude,
|
57
|
longitude: values.longitude,
|
58
|
},
|
59
|
}
|
60
|
console.log(`Creating item: ${JSON.stringify(item)}`)
|
61
|
dispatch(mergeWithCurrentPath([item] as PathVariant))
|
62
|
onClose()
|
63
|
},
|
64
|
})
|
65
|
|
66
|
const onClose = () => {
|
67
|
formik.resetForm()
|
68
|
closeContextMenu()
|
69
|
setOpen(false)
|
70
|
}
|
71
|
|
72
|
return (
|
73
|
<ButtonOpenableDialog
|
74
|
buttonText="Create Location"
|
75
|
buttonColor="primary"
|
76
|
buttonVariant="text"
|
77
|
onCloseCallback={onClose}
|
78
|
maxWidth="xs"
|
79
|
open={open}
|
80
|
setOpen={setOpen}
|
81
|
size="small"
|
82
|
>
|
83
|
<ThemeWrapper>
|
84
|
<Paper>
|
85
|
<DialogTitle>
|
86
|
<Stack
|
87
|
direction="row"
|
88
|
justifyContent="space-between"
|
89
|
alignItems="center"
|
90
|
spacing={1}
|
91
|
>
|
92
|
<Fragment>
|
93
|
Add New Location From Coordinates
|
94
|
</Fragment>
|
95
|
<IconButton onClick={onClose}>
|
96
|
<CloseIcon />
|
97
|
</IconButton>
|
98
|
</Stack>
|
99
|
</DialogTitle>
|
100
|
<DialogContent>
|
101
|
<form onSubmit={formik.handleSubmit}>
|
102
|
<Grid
|
103
|
container
|
104
|
sx={{ mt: 1 }}
|
105
|
// spacing={1}
|
106
|
justifyContent="space-around"
|
107
|
alignItems="center"
|
108
|
>
|
109
|
<Grid item xs={6} md={9}>
|
110
|
<TextField
|
111
|
fullWidth
|
112
|
label="Name"
|
113
|
size="small"
|
114
|
name="name"
|
115
|
variant="outlined"
|
116
|
value={formik.values.name}
|
117
|
onChange={formik.handleChange}
|
118
|
error={
|
119
|
Boolean(formik.errors.name) &&
|
120
|
formik.touched.name
|
121
|
}
|
122
|
helperText={
|
123
|
formik.errors.name &&
|
124
|
formik.touched.name
|
125
|
}
|
126
|
/>
|
127
|
</Grid>
|
128
|
<Grid
|
129
|
item
|
130
|
container
|
131
|
xs={6}
|
132
|
md={3}
|
133
|
justifyContent="flex-end"
|
134
|
>
|
135
|
<Button
|
136
|
variant="contained"
|
137
|
type="submit"
|
138
|
color="primary"
|
139
|
>
|
140
|
Import
|
141
|
</Button>
|
142
|
</Grid>
|
143
|
</Grid>
|
144
|
<Grid container spacing={1} sx={{ mt: 1, mb: 1 }}>
|
145
|
<Grid item xs={12} md={6}>
|
146
|
<TextField
|
147
|
fullWidth
|
148
|
label="Latitude"
|
149
|
name="latitude"
|
150
|
type="number"
|
151
|
size="small"
|
152
|
variant="outlined"
|
153
|
value={formik.values.latitude}
|
154
|
onChange={formik.handleChange}
|
155
|
error={
|
156
|
Boolean(formik.errors.latitude) &&
|
157
|
formik.touched.latitude
|
158
|
}
|
159
|
helperText={
|
160
|
formik.errors.latitude &&
|
161
|
formik.touched.latitude
|
162
|
}
|
163
|
/>
|
164
|
</Grid>
|
165
|
<Grid item xs={12} md={6}>
|
166
|
<TextField
|
167
|
fullWidth
|
168
|
label="Longitude"
|
169
|
name="longitude"
|
170
|
type="number"
|
171
|
variant="outlined"
|
172
|
size="small"
|
173
|
value={formik.values.longitude}
|
174
|
onChange={formik.handleChange}
|
175
|
error={
|
176
|
Boolean(formik.errors.longitude) &&
|
177
|
formik.touched.longitude
|
178
|
}
|
179
|
helperText={
|
180
|
formik.errors.longitude &&
|
181
|
formik.touched.longitude
|
182
|
}
|
183
|
/>
|
184
|
</Grid>
|
185
|
</Grid>
|
186
|
</form>
|
187
|
</DialogContent>
|
188
|
</Paper>
|
189
|
</ThemeWrapper>
|
190
|
</ButtonOpenableDialog>
|
191
|
)
|
192
|
}
|
193
|
|
194
|
export default AddFromCoordinatesDialog
|