forked from CodecrumbsIO/codecrumbs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreducer.js
More file actions
190 lines (157 loc) · 5.14 KB
/
Copy pathreducer.js
File metadata and controls
190 lines (157 loc) · 5.14 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
import { FOLDER_OPEN_STATE } from 'core/constants';
import { ACTIONS } from './constants';
import { getFileNodesMap, getCodecrumbNodesMap } from './utils/treeLayout';
const DefaultState = {};
const DefaultNamespaceState = {
sourceTree: null,
filesMap: null,
foldersMap: null,
sourceLayoutTree: null,
filesLayoutMap: {},
codecrumbsLayoutMap: {},
openedFolders: {},
activeItemsMap: {},
selectedNode: null,
dependenciesEntryName: undefined,
selectedDependencyEdgeNodes: null,
selectedCodeCrumb: null,
codeCrumbedFlowsMap: {},
selectedCrumbedFlowKey: undefined
};
export const getMergeState = (state, namespace) => namespaceStateUpdate => ({
...state,
[namespace]: {
...state[namespace],
...namespaceStateUpdate
}
});
export default (state = DefaultState, action) => {
const namespace = action.namespace;
const namespaceState = state[namespace];
const mergeState = getMergeState(state, namespace);
switch (action.type) {
case ACTIONS.SET_INITIAL_SOURCE_DATA: {
const { dependenciesEntryName, filesMap, foldersMap } = action.payload;
return mergeState({
...DefaultNamespaceState,
...action.payload,
selectedNode: filesMap[dependenciesEntryName],
openedFolders: {
...Object.keys(foldersMap).reduce((res, item) => {
res[item] = FOLDER_OPEN_STATE.CLOSED;
return res;
}, {})
}
});
}
case ACTIONS.SET_CHANGED_SOURCE_DATA:
return mergeState(action.payload);
case ACTIONS.UPDATE_FILES_TREE_LAYOUT_NODES: {
const { payload } = action;
const filesLayoutMap = getFileNodesMap(payload);
const codecrumbsLayoutMap = getCodecrumbNodesMap(filesLayoutMap);
return mergeState({
sourceLayoutTree: payload,
filesLayoutMap,
codecrumbsLayoutMap
});
}
case ACTIONS.SELECT_NODE: {
const { payload } = action;
return mergeState({
selectedCodeCrumb: null,
selectedNode: payload,
dependenciesEntryName: payload.path,
filesMap: {
...namespaceState.filesMap,
[payload.path]: payload
}
});
}
case ACTIONS.TOGGLE_FOLDER: {
const { openedFolders, activeItemsMap } = namespaceState;
const folderPath = action.payload.path;
const openedStateValue =
activeItemsMap[folderPath] !== undefined
? openedFolders[folderPath] === FOLDER_OPEN_STATE.OPEN
? FOLDER_OPEN_STATE.CLOSED
: openedFolders[folderPath] + 1
: openedFolders[folderPath] === FOLDER_OPEN_STATE.OPEN
? FOLDER_OPEN_STATE.CLOSED
: FOLDER_OPEN_STATE.OPEN;
return mergeState({
openedFolders: { ...openedFolders, [folderPath]: openedStateValue }
});
}
case ACTIONS.SET_ACTIVE_ITEMS:
return mergeState({
activeItemsMap: action.payload
});
case ACTIONS.SET_FOLDERS_STATE:
return mergeState({
openedFolders: { ...namespaceState.openedFolders, ...action.payload }
});
case ACTIONS.OPEN_ALL_FOLDERS:
return mergeState({
openedFolders: {
...Object.keys(namespaceState.foldersMap).reduce((res, item) => {
res[item] = FOLDER_OPEN_STATE.OPEN;
return res;
}, {})
}
});
case ACTIONS.CLOSE_ALL_FOLDERS:
return mergeState({
openedFolders: {
// TODO: move to method, duplication
...Object.keys(namespaceState.foldersMap).reduce((res, item) => {
res[item] = FOLDER_OPEN_STATE.CLOSED;
return res;
}, {})
}
});
case ACTIONS.SELECT_CODE_CRUMB:
const { fileNode, codeCrumb } = action.payload;
//TODO: fileNode also can be folder, maybe don't use SELECT_CODE_CRUMB at all and use selected node as well
return mergeState({
selectedNode: fileNode,
selectedCodeCrumb: codeCrumb
});
case ACTIONS.SELECT_CODE_CRUMBED_FLOW:
return mergeState({
selectedCrumbedFlowKey: action.payload
});
case ACTIONS.SET_DEPENDENCIES_ENTRY_POINT: {
const { fileNode } = action.payload;
const dependenciesEntryName = fileNode ? fileNode.path : namespaceState.dependenciesEntryName;
// do some reducerr
return mergeState({
dependenciesEntryName,
selectedDependencyEdgeNodes: null
});
}
case ACTIONS.SELECT_DEPENDENCY_EDGE:
return mergeState({
selectedDependencyEdgeNodes: action.payload
});
case ACTIONS.UPDATE_FILES:
const { selectedNode } = namespaceState;
const fileForSelectedNode = action.payload.find(item => item.path === selectedNode.path);
return mergeState({
selectedNode: fileForSelectedNode
? { ...selectedNode, ...fileForSelectedNode }
: selectedNode,
filesMap: {
...namespaceState.filesMap,
...action.payload.reduce((acc, item) => {
acc[item.path] = { ...namespaceState.filesMap[item.path], ...item };
return acc;
}, {})
}
});
case ACTIONS.RESET_ALL:
return DefaultState;
default:
return state;
}
};