-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathAppendGeoDatasetModal.js
More file actions
360 lines (335 loc) · 10.3 KB
/
AppendGeoDatasetModal.js
File metadata and controls
360 lines (335 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
import React, { useEffect, useState } from "react";
import { useSelector, useDispatch } from "react-redux";
import { calls } from "../../../../core/calls";
import { setModal, setSnackBarText } from "../../../../core/ConfigureStore";
import Typography from "@mui/material/Typography";
import Button from "@mui/material/Button";
import Dialog from "@mui/material/Dialog";
import DialogActions from "@mui/material/DialogActions";
import DialogContent from "@mui/material/DialogContent";
import DialogTitle from "@mui/material/DialogTitle";
import IconButton from "@mui/material/IconButton";
import CloseSharpIcon from "@mui/icons-material/CloseSharp";
import ControlPointDuplicateIcon from "@mui/icons-material/ControlPointDuplicate";
import InsertDriveFileIcon from "@mui/icons-material/InsertDriveFile";
import { useDropzone } from "react-dropzone";
import TextField from "@mui/material/TextField";
import { makeStyles, useTheme } from "@mui/styles";
import useMediaQuery from "@mui/material/useMediaQuery";
const useStyles = makeStyles((theme) => ({
Modal: {
margin: theme.headHeights[1],
[theme.breakpoints.down("xs")]: {
margin: "6px",
},
"& .MuiDialog-container": {
height: "unset !important",
transform: "translateX(-50%) translateY(-50%)",
left: "50%",
top: "50%",
position: "absolute",
},
},
contents: {
background: theme.palette.primary.main,
height: "100%",
width: "600px",
},
heading: {
height: theme.headHeights[2],
boxSizing: "border-box",
background: theme.palette.swatches.p[0],
borderBottom: `1px solid ${theme.palette.swatches.grey[800]}`,
padding: `4px ${theme.spacing(2)} 4px ${theme.spacing(4)} !important`,
},
title: {
padding: `8px 0px`,
fontSize: theme.typography.pxToRem(16),
fontWeight: "bold",
textTransform: "uppercase",
},
content: {
padding: "8px 16px 16px 16px !important",
height: `calc(100% - ${theme.headHeights[2]}px)`,
},
closeIcon: {
padding: theme.spacing(1.5),
height: "100%",
margin: "4px 0px",
},
flexBetween: {
display: "flex",
justifyContent: "space-between",
},
subtitle: {
fontSize: "14px !important",
width: "100%",
marginBottom: "8px !important",
color: theme.palette.swatches.grey[300],
letterSpacing: "0.2px",
},
subtitle2: {
fontSize: "12px !important",
fontStyle: "italic",
width: "100%",
marginBottom: "8px !important",
color: theme.palette.swatches.grey[400],
},
missionNameInput: {
width: "100%",
margin: "8px 0px 4px 0px !important",
},
backgroundIcon: {
margin: "7px 8px 0px 0px",
},
fileName: {
textAlign: "center",
fontWeight: "bold",
letterSpacing: "1px",
marginBottom: "10px",
paddingBottom: "10px",
borderBottom: `1px solid ${theme.palette.swatches.grey[500]}`,
},
dropzone: {
width: "100%",
minHeight: "100px",
margin: "16px 0px",
"& > div": {
flex: "1 1 0%",
display: "flex",
flexDirection: "column",
alignItems: "center",
padding: "20px",
borderWidth: "2px",
borderRadius: "2px",
borderColor: theme.palette.swatches.grey[300],
borderStyle: "dashed",
backgroundColor: theme.palette.swatches.grey[900],
color: theme.palette.swatches.grey[200],
outline: "none",
transition: "border 0.24s ease-in-out 0s",
"&:hover": {
borderColor: theme.palette.swatches.p[11],
},
},
},
dropzoneMessage: {
textAlign: "center",
color: theme.palette.swatches.p[11],
"& > p:first-child": { fontWeight: "bold", letterSpacing: "1px" },
"& > p:last-child": { fontSize: "14px", fontStyle: "italic" },
},
timeFields: {
display: "flex",
"& > div:first-child": {
marginRight: "5px",
},
"& > div:last-child": {
marginLeft: "5px",
},
},
}));
const MODAL_NAME = "appendGeoDataset";
const AppendGeoDatasetModal = (props) => {
const { queryGeoDatasets } = props;
const c = useStyles();
const modal = useSelector((state) => state.core.modal[MODAL_NAME]);
const theme = useTheme();
const isMobile = useMediaQuery(theme.breakpoints.down("md"));
const dispatch = useDispatch();
const [geojson, setGeojson] = useState(null);
const [fileName, setFileName] = useState(null);
const [startTimeField, setStartTimeField] = useState(null);
const [endTimeField, setEndTimeField] = useState(null);
useEffect(() => {
setStartTimeField(modal?.geoDataset?.start_time_field);
setEndTimeField(modal?.geoDataset?.end_time_field);
}, [JSON.stringify(modal)]);
const handleClose = () => {
// close modal
dispatch(setModal({ name: MODAL_NAME, on: false }));
};
const handleSubmit = () => {
const geoDatasetName = modal?.geoDataset?.name;
if (geojson == null || fileName === null) {
dispatch(
setSnackBarText({
text: "Please upload a file.",
severity: "error",
})
);
return;
}
if (geoDatasetName === null) {
dispatch(
setSnackBarText({
text: "No GeoDataset found to append to.",
severity: "error",
})
);
return;
}
const forceParams = {
filename: fileName,
};
if (startTimeField) forceParams.start_prop = startTimeField;
if (endTimeField) forceParams.end_prop = endTimeField;
calls.api(
"geodatasets_append",
{
urlReplacements: {
name: geoDatasetName,
},
forceParams,
type: geojson.type,
features: geojson.features,
},
(res) => {
if (res.status === "success") {
dispatch(
setSnackBarText({
text: "Successfully appended to GeoDataset.",
severity: "success",
})
);
queryGeoDatasets();
handleClose();
} else {
dispatch(
setSnackBarText({
text: res?.message || "Failed to append to GeoDataset.",
severity: "error",
})
);
}
},
(res) => {
dispatch(
setSnackBarText({
text: res?.message || "Failed to append to GeoDataset.",
severity: "error",
})
);
}
);
};
// Dropzone initialization
const {
getRootProps,
getInputProps,
isDragActive,
isDragAccept,
isDragReject,
} = useDropzone({
maxFiles: 1,
accept: {
"application/json": [".json", ".geojson"],
},
onDropAccepted: (files) => {
const file = files[0];
setFileName(file.name);
const reader = new FileReader();
reader.onload = (e) => {
setGeojson(JSON.parse(e.target.result));
};
reader.readAsText(file);
},
onDropRejected: () => {
setFileName(null);
setGeojson(null);
},
});
return (
<Dialog
className={c.Modal}
fullScreen={isMobile}
open={modal !== false}
onClose={handleClose}
aria-labelledby="responsive-dialog-title"
PaperProps={{
className: c.contents,
}}
>
<DialogTitle className={c.heading}>
<div className={c.flexBetween}>
<div className={c.flexBetween}>
<ControlPointDuplicateIcon className={c.backgroundIcon} />
<div
className={c.title}
>{`Append features to this GeoDataset: ${modal?.geoDataset?.name}`}</div>
</div>
<IconButton
className={c.closeIcon}
title="Close"
aria-label="close"
onClick={handleClose}
>
<CloseSharpIcon fontSize="inherit" />
</IconButton>
</div>
</DialogTitle>
<DialogContent className={c.content}>
<Typography className={c.subtitle}>
{`Appends the features of the uploaded file to the GeoDataset`}
</Typography>
<div className={c.dropzone}>
<div {...getRootProps({ className: "dropzone" })}>
<input {...getInputProps()} />
{isDragAccept && <p>All files will be accepted</p>}
{isDragReject && <p>Some files will be rejected</p>}
{!isDragActive && (
<div className={c.dropzoneMessage}>
<p>Drag 'n' drop or click to select files...</p>
<p>Only *.json and *.geojson files are accepted.</p>
</div>
)}
</div>
</div>
<div className={c.fileName}>
<InsertDriveFileIcon />
<div>{fileName || "No File Selected"}</div>
</div>
<div className={c.timeFields}>
<div>
<TextField
className={c.missionNameInput}
label="Start Time Field"
variant="filled"
value={startTimeField}
onChange={(e) => {
setStartTimeField(e.target.value);
}}
/>
<Typography className={c.subtitle2}>
{`If this GeoDataset already has a Start Time Field attached, the name of that start time field inside each feature's "properties" object for which to create a temporal index for the geodataset. Take care in using time field names for the appended GeoJSON features that are different from that of the existing features.`}
</Typography>
</div>
<div>
<TextField
className={c.missionNameInput}
label="End Time Field"
variant="filled"
value={endTimeField}
onChange={(e) => {
setEndTimeField(e.target.value);
}}
/>
<Typography className={c.subtitle2}>
{`If this GeoDataset already has a End Time Field attached, the name of that end time field inside each feature's "properties" object for which to create a temporal index for the geodataset. Take care in using time field names for the appended GeoJSON features that are different from that of the existing features.`}
</Typography>
</div>
</div>
</DialogContent>
<DialogActions>
<Button
className={c.addSelected}
variant="contained"
onClick={handleSubmit}
>
Append to GeoDataset
</Button>
</DialogActions>
</Dialog>
);
};
export default AppendGeoDatasetModal;