summaryrefslogtreecommitdiffstats
path: root/front/odiparpack/app/redux/modules/treeTable.js
diff options
context:
space:
mode:
Diffstat (limited to 'front/odiparpack/app/redux/modules/treeTable.js')
-rw-r--r--front/odiparpack/app/redux/modules/treeTable.js49
1 files changed, 49 insertions, 0 deletions
diff --git a/front/odiparpack/app/redux/modules/treeTable.js b/front/odiparpack/app/redux/modules/treeTable.js
new file mode 100644
index 0000000..96fa08a
--- /dev/null
+++ b/front/odiparpack/app/redux/modules/treeTable.js
@@ -0,0 +1,49 @@
+import { fromJS, List } from 'immutable';
+import { TOGGLE_TREE } from 'ba-actions/actionTypes';
+
+const initialState = {
+ treeOpen: List([]),
+ arrowMore: List([])
+};
+
+const initialImmutableState = fromJS(initialState);
+
+// Collect existing child and parent id's
+function collectId(id, listedId, collapsed, arrowLess) {
+ arrowLess.push(id);
+ for (let i = 0; i < listedId.size; i += 1) {
+ if (listedId.getIn([i]).startsWith(id + '_')) {
+ collapsed.push(listedId.getIn([i]));
+ arrowLess.push(listedId.getIn([i]));
+ }
+ }
+}
+
+export default function reducer(state = initialImmutableState, action = {}) {
+ const { branch } = action;
+ switch (action.type) {
+ case `${branch}/${TOGGLE_TREE}`:
+ return state.withMutations((mutableState) => {
+ const listedId = state.get('treeOpen');
+ const collapsed = [];
+ const arrowLess = [];
+
+ // Collect existing id
+ collectId(action.keyID, listedId, collapsed, arrowLess);
+
+ // Collapse and Expand row
+ if (collapsed.length > 0) { // Collapse tree table
+ mutableState.update('treeOpen', treeOpen => treeOpen.filter(x => collapsed.indexOf(x) < 0));
+ mutableState.update('arrowMore', arrowMore => arrowMore.filter(x => arrowLess.indexOf(x) < 0));
+ } else { // Expand tree table
+ mutableState.update('arrowMore', arrowMore => arrowMore.push(action.keyID));
+ action.child.map(item => {
+ mutableState.update('treeOpen', treeOpen => treeOpen.push(item.id));
+ return true;
+ });
+ }
+ });
+ default:
+ return state;
+ }
+}