-
Notifications
You must be signed in to change notification settings - Fork 383
/
importer.js
408 lines (397 loc) · 14.3 KB
/
importer.js
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/**
* Copyright 2016, GeoSolutions Sas.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/
import {
IMPORTS_LOADING,
IMPORTS_LIST_LOADED,
IMPORTS_LIST_LOAD_ERROR,
IMPORT_CREATED,
IMPORT_LOADED,
IMPORT_RUN_SUCCESS,
IMPORT_RUN_ERROR,
IMPORT_DELETE,
IMPORT_DELETE_ERROR,
IMPORTS_TASK_CREATED,
IMPORTS_TASK_CREATION_ERROR,
IMPORTS_TASK_LOADED,
IMPORTS_TASK_UPDATED,
IMPORTS_TASK_DELETE,
TASK_PROGRESS_UPDATED,
LAYER_LOADED,
LAYER_UPDATED,
IMPORTS_TRANSFORM_LOAD,
IMPORTS_TRANSFORM_CHANGE,
IMPORTS_TRANSFORM_DELETE,
IMPORTS_FILE_UPLOADED,
IMPORTS_UPLOAD_PROGRESS,
IMPORTER_WORKSPACE_LOADED,
IMPORTER_WORKSPACE_SELECTED,
IMPORTER_WORKSPACE_CREATED,
IMPORTER_WORKSPACE_CREATION_ERROR,
IMPORTER_WORKSPACE_STATUS_CHANGE
} from '../actions/importer';
import { MANAGER_ITEM_SELECTED } from '../actions/manager';
import assign from 'object-assign';
import { head, findIndex } from 'lodash';
// constant used to reset the state only if the importer tool is passed to the reducer
// see case: MANAGER_ITEM_SELECTED
const importerTool = "importer";
/** ****************************************************************************/
/* UTILITY FUNCTIONS **********************************************************/
/** ****************************************************************************/
function updateArray(tasks, newTask) {
if (!newTask || !tasks) {
return tasks;
}
let taskIndex = findIndex(tasks, (task) => task.id === newTask.id );
if (taskIndex >= 0 && newTask !== tasks[taskIndex]) {
let newTasks = tasks.slice();
newTasks[taskIndex] = newTask;
return newTasks;
}
return tasks;
}
function updateImportLoadingStatus(state, action, loading = true) {
let importId = action.importId !== undefined ? action.importId : action.details && action.details.importId;
let selectedImport = state && state.selectedImport;
// TODO state.tasks update this if needed
// update selected import
let imports = state && state.imports;
if (selectedImport && selectedImport.id === importId) {
selectedImport = assign({}, selectedImport, {loading: loading});
}
// update imports list
imports = updateArray(imports, assign({}, head(imports.filter((imp) => imp.id === importId), {loading: loading})));
return assign({}, state, {
uploading: action.details && action.details.uploadingFiles !== undefined || state.uploading,
loading: loading,
selectedImport,
imports
});
}
function updateImportTaskLoadingStatus(state, action, loading = true) {
let selectedImport = state && state.selectedImport;
let selectedTask = state && state.selectedTask;
// TODO state.tasks update this if needed
let imports = state && state.imports;
let importId = action.importId !== undefined ? action.importId : action.details && action.details.importId;
let taskId = action.taskId !== undefined ? action.taskId : action.details && action.details.taskId;
// update selected import
if (selectedImport && importId === selectedImport.id) {
if ( selectedTask && selectedTask.id === taskId ) {
selectedTask = assign({}, selectedTask, {
loading: loading,
message: action.details && action.details.message,
element: action.details && action.details.element
});
}
let newTask = assign({}, head(selectedImport.tasks.filter((task) => task.id === taskId)), {
loading: loading,
message: action.details && action.details.message,
element: action.details && action.details.element
});
// update selected task
if (selectedImport && selectedImport.tasks && newTask) {
selectedImport = assign({}, selectedImport);
selectedImport.tasks = updateArray(selectedImport.tasks, newTask);
}
}
// update imports list's task
let impIndex = imports && findIndex(imports, (imp) => imp.id === importId);
if ( imports && impIndex >= 0 ) {
imports = imports.concat();
let imp = imports[impIndex];
let taskIndex = imp && imp.tasks && findIndex(imp.tasks, (task) => task.id === taskId);
if ( imp && imp.tasks && taskIndex >= 0 ) {
let task = assign({}, imp.tasks[taskIndex], {
loading: loading,
message: action.details && action.details.message,
element: action.details && action.details.element
});
imports[impIndex] = assign({}, imp);
imports[impIndex].tasks = updateArray(imports[impIndex].tasks, task);
}
}
return assign({}, state, {
uploading: action.details && action.details.uploadingFiles !== undefined || state.uploading,
loading: loading,
selectedTask,
selectedImport,
imports
});
}
/** ****************************************************************************/
/* REDUCER ********************************************************************/
/** ****************************************************************************/
function importer(state = {}, action) {
switch (action.type) {
case IMPORTS_LOADING: {
if (!action.details) {
// loading full list
return assign({}, state, {loading: action.loading, uploading: action.details && action.details.uploadingFiles !== undefined || state.uploading});
} else if (action.details.importId !== undefined && action.details.taskId === undefined) {
// loading an import
return updateImportLoadingStatus(state, action, action.loading);
} else if (action.details.importId !== undefined && action.details.taskId !== undefined) {
// loading a task
return updateImportTaskLoadingStatus(state, action, action.loading);
}
return state;
}
case MANAGER_ITEM_SELECTED: {
const toolId = action.toolId;
if (toolId === importerTool) {
return assign({}, state, {
loadingError: null,
imports: state.imports,
selectedImport: null,
selectedTask: null,
selectedTransform: null
});
}
return state;
}
case IMPORTS_LIST_LOADED:
return assign({}, state, {
loadingError: null,
imports: action.imports,
selectedImport: null,
selectedTask: null,
selectedTransform: null
});
case IMPORTS_LIST_LOAD_ERROR:
return assign({}, {
loadingError: action.error
});
case IMPORT_CREATED:
return assign({}, state, {
selectedImport: action.import,
selectedTask: null,
selectedTransform: null
});
case IMPORTS_TASK_CREATED:
if (action.importId === (state.selectedImport && state.selectedImport.id) ) {
let selectedImport = assign({}, state.selectedImport, {
tasks: [...(state.selectedImport.tasks || []), ...action.tasks]
});
return assign({}, state, {taskCreationError: null, selectedImport});
}
return state;
case IMPORTS_TASK_UPDATED: {
let selectedTask = state && state.selectedTask;
if ( action.task && selectedTask && selectedTask.id === action.task.id) {
selectedTask = action.task;
}
let tasks = state.tasks;
if (tasks && action.task) {
let index = findIndex(tasks, (task) => task.id === action.task.id);
if (index >= 0) {
tasks = tasks.concat();
tasks[index] = action.task;
}
}
return assign({}, state, {
selectedTask,
tasks
});
}
case IMPORTS_TASK_CREATION_ERROR: {
return assign({}, state, {
uploading: false,
taskCreationError: action.error
});
}
case TASK_PROGRESS_UPDATED: {
let selectedTask = state.selectedTask;
let selectedImport = state.selectedImport;
let tasks = selectedImport && selectedImport.tasks;
if (selectedTask && selectedTask.id === action.taskId) {
selectedTask = assign({}, selectedTask, action.info);
}
if (selectedImport && selectedImport.id === action.importId) {
let index = findIndex(tasks, (task) => task.id === action.taskId);
if (index >= 0) {
tasks = tasks.concat();
tasks[index] = assign({}, tasks[index], action.info);
selectedImport = assign({}, selectedImport, {tasks: tasks});
}
}
return assign({}, state, {
selectedTask,
selectedImport
});
}
case LAYER_LOADED: {
let task = state.selectedTask;
let importObj = state.selectedImport;
if ( importObj && task && importObj.id === action.importId && task.id === action.taskId) {
task = assign({}, task, {
layer: action.layer
});
return assign({}, state, {
selectedTask: task
});
}
return assign({}, state, {
layer: action.layer
});
}
case LAYER_UPDATED: {
let task = state.selectedTask;
let importObj = state.selectedImport;
if ( importObj && task && importObj.id === action.importId && task.id === action.taskId) {
task = assign({}, task, {
layer: action.layer
});
return assign({}, state, {
selectedTask: task
});
}
return state;
}
case IMPORTS_TRANSFORM_LOAD: {
let transform = assign({}, action.transform);
transform.id = action.transformId;
return assign({}, state, {
selectedTransform: transform
});
}
case IMPORTS_TRANSFORM_CHANGE: {
let transform = assign({}, state.selectedTransform || {}, action.transform, {status: "modified"});
return assign({}, state, {
selectedTransform: transform
});
}
case IMPORTS_TRANSFORM_DELETE: {
if (state.selectedTask &&
state.selectedTask.transformChain &&
state.selectedTask.transformChain.transforms &&
state.selectedTask.transformChain.transforms[action.transformId]
) {
let newSelectedTask = assign({}, state.selectedTask, {
transformsChain: assign({}, state.selectedTask.transformChain, {
transforms: state.selectedTask.transformChain.transforms.filter((obj, index) => index !== action.transformId)
})
});
return assign({}, state, {
selectedTask: newSelectedTask,
selectedTransform: state.selectedTransform && state.selectedTransform.id === action.transformId ? null : state.selectedTransform
});
}
return state;
}
case IMPORT_LOADED: {
return assign({}, state, {
selectedImport: action.import,
selectedTask: null,
selectedTransform: null
});
}
case IMPORT_RUN_SUCCESS: {
return state;
}
case IMPORT_RUN_ERROR: {
return state;
}
case IMPORTS_TASK_LOADED: {
return assign({}, state, {
selectedTask: action.task,
selectedTransform: null
});
}
case IMPORTS_TASK_DELETE: {
let task = state.selectedTask;
let selectedImport = state.selectedImport;
if (task && task.id === action.taskId && selectedImport && selectedImport.id === action.importId) {
task = null;
}
if (selectedImport && selectedImport.id === action.importId) {
selectedImport = assign({}, selectedImport);
selectedImport.tasks = selectedImport.tasks.filter((tasko) => tasko.id !== action.taskId);
}
return assign({}, state, {
selectedTask: task,
selectedImport
});
}
case IMPORT_DELETE: {
let imports = state && state.imports;
let importIndex = imports && findIndex(imports, (imp) => imp.id === action.id);
if (importIndex >= 0) {
imports = state.imports.filter((imp) => imp.id !== action.id);
}
if (state && state.selectedImport && state.selectedImport.id === action.id) {
return assign({}, state, {
imports,
selectedImport: null,
selectedTask: null,
selectedTransform: null
});
}
return assign({}, state, {
imports
});
}
case IMPORT_DELETE_ERROR: {
let imports = state && state.imports;
imports = imports && imports.map((imp) => {
if (imp.id === action.id) {
return {...imp, error: action.error};
}
return imp;
});
return assign({}, state, {
imports
});
}
case IMPORTS_FILE_UPLOADED: {
return assign({}, state, {
uploading: false
});
}
case IMPORTS_UPLOAD_PROGRESS: {
return assign({}, state, {
uploading: {
progress: action.progress
}
});
}
case IMPORTER_WORKSPACE_LOADED: {
return assign({}, state, {
workspaces: action.workspaces
});
}
case IMPORTER_WORKSPACE_SELECTED: {
return assign({}, state, {
selectedWorkSpace: action.workspace
});
}
case IMPORTER_WORKSPACE_CREATED: {
let workspaces = state.workspaces.concat({
name: action.name,
href: action.href
});
return assign({}, state, {
workspaces,
workspaceCreationStatus: {status: "success", workspace: action.name}
});
}
case IMPORTER_WORKSPACE_CREATION_ERROR: {
return assign({}, state, {
workspaceCreationStatus: {status: "error", error: action.error}
});
}
case IMPORTER_WORKSPACE_STATUS_CHANGE: {
return assign({}, state, {
workspaceCreationStatus: action.state
});
}
default:
return state;
}
}
export default importer;