From ab02f945c65af085da2792ba315432d41fb3344a Mon Sep 17 00:00:00 2001 From: Sorrel Bri Date: Wed, 4 Dec 2019 21:22:12 -0800 Subject: [PATCH] add reducer action for epoch mutation in place --- src/reducers/stateReducer.epochs.test.js | 14 ++++++++++++++ src/reducers/stateReducer.js | 14 ++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/src/reducers/stateReducer.epochs.test.js b/src/reducers/stateReducer.epochs.test.js index 5fc01da..cfc781a 100644 --- a/src/reducers/stateReducer.epochs.test.js +++ b/src/reducers/stateReducer.epochs.test.js @@ -20,4 +20,18 @@ describe('Epochs', () => { const action = {type: 'ADD_EPOCH', value: { name: 'epoch 2', changes: []}}; expect(stateReducer(state, action)).toEqual({...state, epochs: [...state.epochs, action.value]}) }) + + it('epoch name mutation returns new epochs list with mutation', () => { + const firstAction = {type: 'ADD_EPOCH', value: { name: 'epoch 2', changes: []}}; + const secondAction = {type: 'SET_EPOCH', value: { index: 0, name: 'proto-lang'}}; + const secondState = stateReducer(state, firstAction); + expect(stateReducer(secondState, secondAction)).toEqual( + {...state, + epochs: [ + {name: 'proto-lang', changes: []}, + {name: 'epoch 2', changes: []} + ] + } + ); + }); }); \ No newline at end of file diff --git a/src/reducers/stateReducer.js b/src/reducers/stateReducer.js index da57b23..83811ec 100644 --- a/src/reducers/stateReducer.js +++ b/src/reducers/stateReducer.js @@ -57,6 +57,20 @@ const stateReducer = (state, action) => { return {...state, epochs: [...state.epochs, newEpoch]} } + case 'SET_EPOCH': { + let mutatedEpochs = state.epochs; + let index = [action.value.index] + + mutatedEpochs[index].name = action.value.name + ? action.value.name + : mutatedEpochs[index].name; + + mutatedEpochs[index].changes = action.value.changes + ? action.value.changes + : mutatedEpochs[index].changes; + return {...state, epochs: [...mutatedEpochs]} + } + case 'ADD_FEATURE': { let positivePhones = action.value.positivePhones || []; let negativePhones = action.value.negativePhones || [];