Projekt

Obecné

Profil

Stáhnout (7.55 KB) Statistiky
| Větev: | Tag: | Revize:
1
import {
2
    Button,
3
    DialogContent,
4
    DialogTitle,
5
    FormControl,
6
    Grid,
7
    IconButton,
8
    InputLabel,
9
    MenuItem,
10
    Paper,
11
    Select,
12
    Stack,
13
    TextField,
14
} from '@mui/material'
15
import { Fragment, FunctionComponent, useState } from 'react'
16
import ButtonOpenableDialog from '../../Reusables/ButtonOpenableDialog'
17
import ContextMenuDialogProps from './contextMenuDialogProps'
18
import * as yup from 'yup'
19
import { useFormik } from 'formik'
20
import ThemeWrapper from '../../Theme/ThemeWrapper'
21
import CloseIcon from '@mui/icons-material/Close'
22

    
23
const importTypes = {
24
    localCatalog: 'Local Catalog',
25
    websiteCatalogs: 'Website Catalogs',
26
}
27

    
28
const externalCatalogs = {
29
    PLEIADES: 'Pleiades',
30
    GEONAMES: 'Geonames',
31
    CIGS: 'CIGS',
32
    ANE: 'ANE',
33
}
34

    
35
interface ImportData {
36
    name: string // used always
37
    externalCatalogType?: string // only used for website catalogs variant
38
}
39

    
40
const ImportLocationDialog: FunctionComponent<ContextMenuDialogProps> = ({
41
    latLng,
42
    closeContextMenu,
43
}) => {
44
    const [open, setOpen] = useState(false)
45
    const [importType, setImportType] = useState('localCatalog')
46

    
47
    const formik = useFormik({
48
        initialValues: {
49
            name: '',
50
        } as ImportData,
51
        validationSchema: yup.object().shape({
52
            name: yup.string().required('Name is required'),
53
        }),
54
        onSubmit: async (values: ImportData) => {
55
            if (importType === 'localCatalog') {
56
                values.externalCatalogType = undefined
57
            }
58
        },
59
    })
60

    
61
    const NameTextField = () => (
62
        <TextField
63
            sx={{ mt: 1 }}
64
            fullWidth
65
            label="Name"
66
            name="name"
67
            size="small"
68
            value={formik.values.name}
69
            onChange={formik.handleChange}
70
            variant="outlined"
71
            error={Boolean(formik.errors.name) && formik.touched.name}
72
            helperText={formik.errors.name && formik.touched.name}
73
        />
74
    )
75

    
76
    const onClose = () => {
77
        formik.resetForm()
78
        setImportType('localCatalog')
79
        setOpen(false)
80
        closeContextMenu()
81
    }
82

    
83
    return (
84
        <ButtonOpenableDialog
85
            buttonText="Import Location"
86
            buttonColor="primary"
87
            buttonVariant="text"
88
            onCloseCallback={onClose}
89
            maxWidth="xs"
90
            open={open}
91
            setOpen={setOpen}
92
            size="small"
93
            onOpenCallback={() => {}}
94
        >
95
            <ThemeWrapper>
96
                <Paper>
97
                    <DialogTitle>
98
                        <Stack
99
                            direction="row"
100
                            justifyContent="space-between"
101
                            alignItems="center"
102
                            spacing={1}
103
                        >
104
                            <Fragment>Import Locations</Fragment>
105
                            <IconButton onClick={onClose}>
106
                                <CloseIcon />
107
                            </IconButton>
108
                        </Stack>
109
                    </DialogTitle>
110
                    <DialogContent>
111
                        <Grid
112
                            container
113
                            sx={{ mt: 1 }}
114
                            // spacing={1}
115
                            justifyContent="space-around"
116
                            alignItems="center"
117
                        >
118
                            <Grid item xs={6} md={9}>
119
                                <FormControl fullWidth>
120
                                    <InputLabel id="importType">
121
                                        Import
122
                                    </InputLabel>
123
                                    <Select
124
                                        labelId="importType"
125
                                        id="importTypeSelect"
126
                                        value={importType}
127
                                        label="Import"
128
                                        size="small"
129
                                        onChange={(e) =>
130
                                            setImportType(e.target.value)
131
                                        }
132
                                    >
133
                                        {Object.keys(importTypes).map((key) => (
134
                                            <MenuItem key={key} value={key}>
135
                                                {
136
                                                    //@ts-ignore
137
                                                    importTypes[key]
138
                                                }
139
                                            </MenuItem>
140
                                        ))}
141
                                    </Select>
142
                                </FormControl>
143
                            </Grid>
144
                            <Grid
145
                                item
146
                                container
147
                                xs={6}
148
                                md={3}
149
                                justifyContent="flex-end"
150
                            >
151
                                <Button
152
                                    variant="contained"
153
                                    type="submit"
154
                                    color="primary"
155
                                >
156
                                    Import
157
                                </Button>
158
                            </Grid>
159
                            {importType === 'localCatalog' ? (
160
                                <NameTextField />
161
                            ) : (
162
                                <Fragment>
163
                                    <FormControl
164
                                        fullWidth
165
                                        variant="outlined"
166
                                        sx={{ mt: 1 }}
167
                                    >
168
                                        <InputLabel id="externalCatalogType">
169
                                            Catalog Type
170
                                        </InputLabel>
171
                                        <Select
172
                                            labelId="externalCatalogType"
173
                                            name="externalCatalogType"
174
                                            value={
175
                                                formik.values
176
                                                    .externalCatalogType ||
177
                                                'PLEIADES'
178
                                            }
179
                                            onChange={formik.handleChange}
180
                                            label="Catalog Type"
181
                                            size="small"
182
                                        >
183
                                            {Object.entries(
184
                                                externalCatalogs
185
                                            ).map(([key, value], idx) => (
186
                                                <MenuItem key={key} value={key}>
187
                                                    {value}
188
                                                </MenuItem>
189
                                            ))}
190
                                        </Select>
191
                                    </FormControl>
192
                                    <NameTextField />
193
                                </Fragment>
194
                            )}
195
                        </Grid>
196
                    </DialogContent>
197
                </Paper>
198
            </ThemeWrapper>
199
        </ButtonOpenableDialog>
200
    )
201
}
202

    
203
export default ImportLocationDialog
(3-3/4)