From e13e630cd6e4fc0b1ff92098a28a770794c7bb9a Mon Sep 17 00:00:00 2001 From: gabrhr <73925454+gabrhr@users.noreply.github.com> Date: Wed, 20 Apr 2022 10:19:29 -0500 Subject: =?UTF-8?q?A=C3=B1adir=20plantilla?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Base para front --- front/odiparpack/app/redux/modules/treeTable.js | 49 +++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 front/odiparpack/app/redux/modules/treeTable.js (limited to 'front/odiparpack/app/redux/modules/treeTable.js') 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; + } +} -- cgit v1.2.3