-
Notifications
You must be signed in to change notification settings - Fork 383
/
additionallayers.js
60 lines (51 loc) · 1.73 KB
/
additionallayers.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
/*
* Copyright 2018, 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 {
UPDATE_ADDITIONAL_LAYER,
REMOVE_ADDITIONAL_LAYER,
UPDATE_OPTIONS_BY_OWNER,
REMOVE_ALL_ADDITIONAL_LAYERS
} from '../actions/additionallayers';
import { head, pickBy, identity, isObject, isArray } from 'lodash';
function additionallayers(state = [], action) {
switch (action.type) {
case UPDATE_ADDITIONAL_LAYER: {
const newLayerItem = pickBy({
id: action.id,
owner: action.owner,
actionType: action.actionType,
options: action.options
}, identity);
const currentLayerItem = head(state.filter(({id}) => id === newLayerItem.id));
if (currentLayerItem) {
return state.map(layerItem => layerItem.id === newLayerItem.id ? {...currentLayerItem, ...newLayerItem} : {...layerItem});
}
return [
...state,
newLayerItem
];
}
case UPDATE_OPTIONS_BY_OWNER: {
const {options, owner} = action;
return state.map((layerItem, idx) => layerItem.owner === owner ? {
...layerItem,
options: isObject(options) && options[layerItem.id] || isArray(options) && options[idx] || {}
} : {...layerItem});
}
case REMOVE_ADDITIONAL_LAYER: {
const {id, owner} = action;
return owner ? state.filter(layerItem => layerItem.owner !== owner) : state.filter(layerItem => layerItem.id !== id);
}
case REMOVE_ALL_ADDITIONAL_LAYERS: {
return [];
}
default:
return state;
}
}
export default additionallayers;