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/crudTable.js | 94 +++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 front/odiparpack/app/redux/modules/crudTable.js (limited to 'front/odiparpack/app/redux/modules/crudTable.js') diff --git a/front/odiparpack/app/redux/modules/crudTable.js b/front/odiparpack/app/redux/modules/crudTable.js new file mode 100644 index 0000000..2bdb1e8 --- /dev/null +++ b/front/odiparpack/app/redux/modules/crudTable.js @@ -0,0 +1,94 @@ +import { fromJS, List, Map } from 'immutable'; +import notif from 'ba-api/notifMessage'; +import { + FETCH_DATA, + ADD_EMPTY_ROW, + UPDATE_ROW, + REMOVE_ROW, + EDIT_ROW, + SAVE_ROW, + CLOSE_NOTIF +} from 'ba-actions/actionTypes'; + +const initialState = { + dataTable: List([]), + notifMsg: '', +}; + +const initialItem = (keyTemplate, anchor) => { + const [...rawKey] = keyTemplate.keys(); + const staticKey = { + id: (+new Date() + Math.floor(Math.random() * 999999)).toString(36), + }; + for (let i = 0; i < rawKey.length; i += 1) { + if (rawKey[i] !== 'id' && rawKey[i] !== 'edited') { + staticKey[rawKey[i]] = anchor[i].initialValue; + } + } + // Push another static key + staticKey.edited = true; + + return Map(staticKey); +}; + +const initialImmutableState = fromJS(initialState); + +export default function reducer(state = initialImmutableState, action = {}) { + const { branch } = action; + switch (action.type) { + case `${branch}/${FETCH_DATA}`: + return state.withMutations((mutableState) => { + const items = fromJS(action.items); + mutableState.set('dataTable', items); + }); + case `${branch}/${ADD_EMPTY_ROW}`: + return state.withMutations((mutableState) => { + const raw = state.get('dataTable').last(); + const initial = initialItem(raw, action.anchor); + mutableState.update('dataTable', dataTable => dataTable.unshift(initial)); + }); + case `${branch}/${REMOVE_ROW}`: + return state.withMutations((mutableState) => { + const index = state.get('dataTable').indexOf(action.item); + mutableState + .update('dataTable', dataTable => dataTable.splice(index, 1)) + .set('notifMsg', notif.removed); + }); + case `${branch}/${UPDATE_ROW}`: + return state.withMutations((mutableState) => { + const index = state.get('dataTable').indexOf(action.item); + const cellTarget = action.event.target.name; + const newVal = type => { + if (type === 'checkbox') { + return action.event.target.checked; + } + return action.event.target.value; + }; + mutableState.update('dataTable', dataTable => dataTable + .setIn([index, cellTarget], newVal(action.event.target.type)) + ); + }); + case `${branch}/${EDIT_ROW}`: + return state.withMutations((mutableState) => { + const index = state.get('dataTable').indexOf(action.item); + mutableState.update('dataTable', dataTable => dataTable + .setIn([index, 'edited'], true) + ); + }); + case `${branch}/${SAVE_ROW}`: + return state.withMutations((mutableState) => { + const index = state.get('dataTable').indexOf(action.item); + mutableState + .update('dataTable', dataTable => dataTable + .setIn([index, 'edited'], false) + ) + .set('notifMsg', notif.saved); + }); + case `${branch}/${CLOSE_NOTIF}`: + return state.withMutations((mutableState) => { + mutableState.set('notifMsg', ''); + }); + default: + return state; + } +} -- cgit v1.2.3