UNPKG

my-test123

Version:
111 lines 4.84 kB
import { cloneDeep } from 'lodash'; import * as IterationActions from '.././actions/iteration.actions'; import { initialState, initialUIState } from './../states/iteration.state'; export var iterationReducer = function (state, action) { if (state === void 0) { state = initialState; } switch (action.type) { case IterationActions.GET_SUCCESS: return action.payload; case IterationActions.ADD_SUCCESS: var parent_1 = action.payload.parent; var newIteration = action.payload.iteration; if (parent_1) { var parentIndex = state.findIndex(function (i) { return i.id === parent_1.id; }); if (parentIndex > -1) { state[parentIndex].hasChildren = true; } if (state[parentIndex].children.length) { state[parentIndex].children = [ action.payload.iteration ].concat(state[parentIndex].children); } else { state[parentIndex].children = [ action.payload.iteration ]; } } return [action.payload.iteration].concat(state); case IterationActions.UPDATE_SUCCESS: var updatedIteration_1 = action.payload; updatedIteration_1.children = state.filter(function (i) { return i.parentId === updatedIteration_1.id; }); var index = state.findIndex(function (i) { return i.id === updatedIteration_1.id; }); if (index > -1) { updatedIteration_1.selected = state[index].selected; state = state.slice(0, index).concat([ updatedIteration_1 ], state.slice(index + 1)); var parentIndex = state.findIndex(function (i) { return i.id === updatedIteration_1.parentId; }); if (parentIndex > -1 && state[parentIndex].children) { var childIndex = state[parentIndex].children.findIndex(function (i) { return i.id === updatedIteration_1.id; }); if (childIndex > -1) { state[parentIndex].children = state[parentIndex].children.slice(0, childIndex).concat([ state[index] ], state[parentIndex].children.slice(childIndex + 1)); } } } return state.slice(); case IterationActions.SELECT: if (action.payload !== null) { var itIndex = state.findIndex(function (item) { return item.id === action.payload.id; }); if (itIndex > -1) { for (var i = 0; i < state.length; i++) { state[i].selected = i === itIndex; } } // Expand all the parents var pId_1 = state[itIndex].parentId; while (pId_1) { var pIndex = state.findIndex(function (item) { return item.id === pId_1; }); if (pIndex > -1) { state[pIndex].showChildren = true; pId_1 = state[pIndex].parentId; } } } else { for (var i = 0; i < state.length; i++) { state[i].selected = false; state[i].showChildren = false; } } return state.slice(); // This is important for change detection case IterationActions.GET_ERROR: return state; case IterationActions.ADD_ERROR: return state; case IterationActions.UPDATE_ERROR: return state; default: return state; } }; export var iterationUiReducer = function (s, action) { if (s === void 0) { s = initialUIState; } var state = cloneDeep(s); switch (action.type) { case IterationActions.UPDATE_SUCCESS: case IterationActions.ADD_SUCCESS: state.error = ''; state.modalLoading = false; return state; case IterationActions.UPDATE: case IterationActions.ADD: state.error = ''; state.modalLoading = true; return state; case IterationActions.ADD_ERROR: state.modalLoading = false; state.error = 'Some error has occured on adding iteration'; return state; case IterationActions.UPDATE_ERROR: state.modalLoading = false; state.error = 'Some error has occured on updating iteration'; return state; default: return state; } }; //# sourceMappingURL=iteration-reducer.js.map