Use createHandleActions for adding/removing commands so itemMap is synced properly

(cherry picked from commit 99be6a7e4065b77c910df6444a468fedc23e90cc)
pull/5607/head
Mark McDowall 4 years ago committed by Qstick
parent 6de847a361
commit 56bb319934

@ -1,12 +1,10 @@
import _ from 'lodash';
import { createAction } from 'redux-actions';
import { batchActions } from 'redux-batched-actions'; import { batchActions } from 'redux-batched-actions';
import { messageTypes } from 'Helpers/Props'; import { messageTypes } from 'Helpers/Props';
import { createThunk, handleThunks } from 'Store/thunks'; import { createThunk, handleThunks } from 'Store/thunks';
import { isSameCommand } from 'Utilities/Command'; import { isSameCommand } from 'Utilities/Command';
import createAjaxRequest from 'Utilities/createAjaxRequest'; import createAjaxRequest from 'Utilities/createAjaxRequest';
import { hideMessage, showMessage } from './appActions'; import { hideMessage, showMessage } from './appActions';
import { updateItem } from './baseActions'; import { removeItem, updateItem } from './baseActions';
import createFetchHandler from './Creators/createFetchHandler'; import createFetchHandler from './Creators/createFetchHandler';
import createHandleActions from './Creators/createHandleActions'; import createHandleActions from './Creators/createHandleActions';
import createRemoveItemHandler from './Creators/createRemoveItemHandler'; import createRemoveItemHandler from './Creators/createRemoveItemHandler';
@ -37,9 +35,9 @@ export const defaultState = {
export const FETCH_COMMANDS = 'commands/fetchCommands'; export const FETCH_COMMANDS = 'commands/fetchCommands';
export const EXECUTE_COMMAND = 'commands/executeCommand'; export const EXECUTE_COMMAND = 'commands/executeCommand';
export const CANCEL_COMMAND = 'commands/cancelCommand'; export const CANCEL_COMMAND = 'commands/cancelCommand';
export const ADD_COMMAND = 'commands/updateCommand'; export const ADD_COMMAND = 'commands/addCommand';
export const UPDATE_COMMAND = 'commands/finishCommand'; export const UPDATE_COMMAND = 'commands/updateCommand';
export const FINISH_COMMAND = 'commands/addCommand'; export const FINISH_COMMAND = 'commands/finishCommand';
export const REMOVE_COMMAND = 'commands/removeCommand'; export const REMOVE_COMMAND = 'commands/removeCommand';
// //
@ -48,10 +46,10 @@ export const REMOVE_COMMAND = 'commands/removeCommand';
export const fetchCommands = createThunk(FETCH_COMMANDS); export const fetchCommands = createThunk(FETCH_COMMANDS);
export const executeCommand = createThunk(EXECUTE_COMMAND); export const executeCommand = createThunk(EXECUTE_COMMAND);
export const cancelCommand = createThunk(CANCEL_COMMAND); export const cancelCommand = createThunk(CANCEL_COMMAND);
export const addCommand = createThunk(ADD_COMMAND);
export const updateCommand = createThunk(UPDATE_COMMAND); export const updateCommand = createThunk(UPDATE_COMMAND);
export const finishCommand = createThunk(FINISH_COMMAND); export const finishCommand = createThunk(FINISH_COMMAND);
export const addCommand = createAction(ADD_COMMAND); export const removeCommand = createThunk(REMOVE_COMMAND);
export const removeCommand = createAction(REMOVE_COMMAND);
// //
// Helpers // Helpers
@ -161,6 +159,10 @@ export const actionHandlers = handleThunks({
[CANCEL_COMMAND]: createRemoveItemHandler(section, '/command'), [CANCEL_COMMAND]: createRemoveItemHandler(section, '/command'),
[ADD_COMMAND]: function(getState, payload, dispatch) {
dispatch(updateItem({ section: 'commands', ...payload }));
},
[UPDATE_COMMAND]: function(getState, payload, dispatch) { [UPDATE_COMMAND]: function(getState, payload, dispatch) {
dispatch(updateItem({ section: 'commands', ...payload })); dispatch(updateItem({ section: 'commands', ...payload }));
@ -183,6 +185,10 @@ export const actionHandlers = handleThunks({
dispatch(updateItem({ section: 'commands', ...payload })); dispatch(updateItem({ section: 'commands', ...payload }));
scheduleRemoveCommand(payload, dispatch); scheduleRemoveCommand(payload, dispatch);
showCommandMessage(payload, dispatch); showCommandMessage(payload, dispatch);
},
[ADD_COMMAND]: function(getState, payload, dispatch) {
dispatch(removeItem({ section: 'commands', ...payload }));
} }
}); });
@ -190,26 +196,4 @@ export const actionHandlers = handleThunks({
// //
// Reducers // Reducers
export const reducers = createHandleActions({ export const reducers = createHandleActions({}, defaultState, section);
[ADD_COMMAND]: (state, { payload }) => {
const newState = Object.assign({}, state);
newState.items = [...state.items, payload];
return newState;
},
[REMOVE_COMMAND]: (state, { payload }) => {
const newState = Object.assign({}, state);
newState.items = [...state.items];
const index = _.findIndex(newState.items, { id: payload.id });
if (index > -1) {
newState.items.splice(index, 1);
}
return newState;
}
}, defaultState, section);

Loading…
Cancel
Save