Use createHandleActions for adding/removing commands so itemMap is synced properly
This commit is contained in:
parent
c1d060ff58
commit
99be6a7e40
|
@ -1,5 +1,4 @@
|
||||||
import _ from 'lodash';
|
import _ from 'lodash';
|
||||||
import { createAction } from 'redux-actions';
|
|
||||||
import { batchActions } from 'redux-batched-actions';
|
import { batchActions } from 'redux-batched-actions';
|
||||||
import createAjaxRequest from 'Utilities/createAjaxRequest';
|
import createAjaxRequest from 'Utilities/createAjaxRequest';
|
||||||
import { isSameCommand } from 'Utilities/Command';
|
import { isSameCommand } from 'Utilities/Command';
|
||||||
|
@ -9,7 +8,7 @@ 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';
|
||||||
import { showMessage, hideMessage } from './appActions';
|
import { showMessage, hideMessage } from './appActions';
|
||||||
import { updateItem } from './baseActions';
|
import { updateItem, removeItem } from './baseActions';
|
||||||
|
|
||||||
//
|
//
|
||||||
// Variables
|
// Variables
|
||||||
|
@ -37,9 +36,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 +47,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 +160,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 +186,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 +197,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…
Reference in New Issue