-
Notifications
You must be signed in to change notification settings - Fork 6
/
reducer.js
66 lines (61 loc) · 1.29 KB
/
reducer.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
import crawl from 'tree-crawl';
const initialState = {
data: [],
activeVideo: {
meta: {},
src: ''
},
newData: null,
editMode: false,
searchResults: null
};
const reducer = (state = initialState, action) => {
switch (action.type) {
case 'SET_ACTIVE_VIDEO':
return {
...state,
activeVideo: action.video
};
case 'SET_EDIT_MODE':
return {
...state,
editMode: action.editing
};
case 'SAVING_META':
return {
...state,
data: setNewMetaInTree({ ...state.data }, action.newMeta)
};
case 'RETRIEVE_DATA_SUCCESS':
case 'SAVE_META_SUCCESS':
return {
...state,
data: action.data
};
case 'SAVE_META_FAILURE':
return state;
case 'SET_SEARCH_RESULTS':
return {
...state,
searchResults: action.data
};
default:
return state;
}
};
const setNewMetaInTree = (tree, newMeta) => {
crawl(
tree,
(node, context) => {
if (node.meta && node.meta.id === newMeta.id) {
const newNode = node;
newNode.meta = newMeta;
context.parent.items[context.index] = newNode;
context.replace(newNode);
}
},
{ getChildren: node => node.items }
);
return tree;
};
export default reducer;