refactor Epoch component

This commit is contained in:
Sorrel Bri 2020-03-01 15:41:25 -08:00
parent c13cc33697
commit 74bbca028f

View file

@ -7,23 +7,25 @@ import { render } from 'react-dom';
const Epochs = ({epochs, errors, dispatch}) => { const Epochs = ({epochs, errors, dispatch}) => {
const handleEvent = func => e => {
e.preventDefault();
return func;
}
const addEpoch = e => { const addEpoch = e => handleEvent(() => {
e.preventDefault()
let index = epochs.length + 1; let index = epochs.length + 1;
dispatch({ dispatch({
type: 'ADD_EPOCH', type: 'ADD_EPOCH',
value: {name: `Epoch ${index}`} value: {name: `epoch ${index}`}
}) })
} })(e)
const removeEpoch = (e, epochName) => { const removeEpoch = e => handleEvent(
e.preventDefault()
dispatch({ dispatch({
type: 'REMOVE_EPOCH', type: 'REMOVE_EPOCH',
value: {name: epochName} value: {name: epochName}
}); })
} )(e);
const updateEpoch = (epoch, epochIndex) => { const updateEpoch = (epoch, epochIndex) => {
const dispatchValue = { const dispatchValue = {
@ -48,23 +50,25 @@ const Epochs = ({epochs, errors, dispatch}) => {
} }
const renderEpochs = () => { const renderEpochs = () => {
if (epochs && epochs.length) return epochs.map((epoch, index) => { if (epochs && epochs.length) {
return epochs.map((epoch, index) => {
const epochError = errors.epoch ? errors.error : null const epochError = errors.epoch ? errors.error : null
return ( return (
<div <div
className="SoundChangeSuite" className="SoundChangeSuite"
data-testid={`${epoch.name}_SoundChangeSuite`} data-testid={`${epoch.name}_SoundChangeSuite`}
key={`epoch-${index}`} key={`epoch-${index}`}
> >
<SoundChangeSuite <SoundChangeSuite
epochIndex={index} epoch={epoch} epochIndex={index} epoch={epoch}
updateEpoch={updateEpoch} removeEpoch={removeEpoch} updateEpoch={updateEpoch} removeEpoch={removeEpoch}
epochs={epochs} epochs={epochs}
error={epochError} error={epochError}
/> />
{renderAddEpochButton(index)} {renderAddEpochButton(index)}
</div> </div>
)}); )});
}
return renderAddEpochButton(-1) return renderAddEpochButton(-1)
} }