hook remove epoch to dispatch REMOVE_EPOCH

This commit is contained in:
Sorrel Bri 2019-12-18 21:02:20 -08:00
parent 368b6ea1fa
commit 2c98a28624
5 changed files with 35 additions and 29 deletions

View file

@ -18,14 +18,19 @@ const Epochs = props => {
const removeEpoch = (e, epochName) => { const removeEpoch = (e, epochName) => {
e.preventDefault() e.preventDefault()
let newEpochs = props.epochs.filter(epoch => epoch.name !== epochName); props.dispatch({
props.setEpochs(newEpochs) type: 'REMOVE_EPOCH',
value: {name: epochName}
});
} }
const updateEpoch = (epoch, epochIndex) => { const updateEpoch = (epoch, epochIndex) => {
let updatedEpochs = [...props.epochs] let updatedEpochs = [...props.epochs]
updatedEpochs[epochIndex] = epoch updatedEpochs[epochIndex] = epoch
props.setEpochs(updatedEpochs) props.dispatch({
type: "SET_EPOCH",
value: epoch
})
} }
return ( return (

View file

@ -23,9 +23,6 @@ const SoundChangeSuite = props => {
)} )}
></textarea> ></textarea>
{props.error
? <p><span className="error-message">{`Formatting errors in line(s) ${props.error.join(', ')}`}</span></p>
: <></>}
<textarea <textarea
name="changes" name="changes"
id="" cols="30" rows="10" id="" cols="30" rows="10"

View file

@ -2,7 +2,7 @@
import type { stateType } from './stateReducer'; import type { stateType } from './stateReducer';
export type epochAction = { export type epochAction = {
type: "ADD_EPOCH" | "SET_EPOCH", type: "ADD_EPOCH" | "SET_EPOCH" | "REMOVE_EPOCH",
value: { value: {
index?: number, index?: number,
name: string, name: string,
@ -28,4 +28,9 @@ export const setEpoch = (state: stateType, action: epochAction): stateType => {
? action.value.changes ? action.value.changes
: mutatedEpochs[index].changes; : mutatedEpochs[index].changes;
return {...state, epochs: [...mutatedEpochs]} return {...state, epochs: [...mutatedEpochs]}
}
export const removeEpoch = (state: stateType, action: epochAction): stateType => {
const mutatedEpochs = state.epochs.filter(epoch => epoch.name !== action.value.name )
return {...state, epochs: [...mutatedEpochs]}
} }

View file

@ -46,7 +46,17 @@ describe('Epochs', () => {
{name: 'epoch 2', changes: ['']} {name: 'epoch 2', changes: ['']}
] ]
} }
); );
});
it('epochs returned with deleted epoch removed', () => {
const firstAction = {type: 'ADD_EPOCH', value: { name: 'epoch 2', changes: ['']}};
const stateWithTwoEpochs = stateReducer(state, firstAction);
const secondAction = {type: 'REMOVE_EPOCH', value: {index: 0, name: 'epoch 1'}}
expect(stateReducer(stateWithTwoEpochs, secondAction)).toEqual({
...state,
epochs: [{ name: 'epoch 2', changes: ['']}]
});
}); });
}); });

View file

@ -1,7 +1,7 @@
// @flow // @flow
import { addLexeme, setLexicon } from './stateReducer.lexicon'; import { addLexeme, setLexicon } from './stateReducer.lexicon';
import type { lexiconAction } from './stateReducer.lexicon'; import type { lexiconAction } from './stateReducer.lexicon';
import { addEpoch, setEpoch } from './stateReducer.epochs'; import { addEpoch, setEpoch, removeEpoch } from './stateReducer.epochs';
import type { epochAction } from './stateReducer.epochs'; import type { epochAction } from './stateReducer.epochs';
import { addFeature } from './stateReducer.features'; import { addFeature } from './stateReducer.features';
import type { featureAction } from './stateReducer.features'; import type { featureAction } from './stateReducer.features';
@ -41,31 +41,20 @@ export const stateReducer = (state: stateType, action: actionType): stateType =>
return initState(); return initState();
} }
case 'ADD_LEXEME': { case 'ADD_LEXEME': return addLexeme(state, action);
return addLexeme(state, action);
}
case 'SET_LEXICON': { case 'SET_LEXICON': return setLexicon(state, action);
return setLexicon(state, action);
}
case 'ADD_EPOCH': { case 'ADD_EPOCH': return addEpoch(state, action);
return addEpoch(state, action);
}
case 'SET_EPOCH': { case 'SET_EPOCH': return setEpoch(state, action);
return setEpoch(state, action);
}
case 'ADD_FEATURE': { case 'REMOVE_EPOCH': return removeEpoch(state, action);
return addFeature(state, action);
}
case 'RUN': { case 'ADD_FEATURE': return addFeature(state, action);
return run(state, action);
}
default: case 'RUN': return run(state, action);
return state;
default: return state;
} }
} }