Skip to content

Instantly share code, notes, and snippets.

@ernestofreyreg
Last active July 19, 2017 06:38
Show Gist options
  • Save ernestofreyreg/f8408decd9d13d8a942a497cf82cddc0 to your computer and use it in GitHub Desktop.
Save ernestofreyreg/f8408decd9d13d8a942a497cf82cddc0 to your computer and use it in GitHub Desktop.
const CHANGE_VALUE = 'CHANGE_VALUE'
const STOP_CHANGING = 'STOP_CHANGING'
const START_CHANGING = 'START_CHANGING'
export const initialState = {
editing: false,
changedValue: null
}
export const reducer = (state, action) => {
switch (action.type) {
case START_CHANGING: return {...state, editing: true, changedValue: action.value}
case CHANGE_VALUE: return {...state, changedValue: action.value}
case STOP_CHANGING: return {...state, editing: false}
}
return state
}
export const createActions = ({getState, dispatch}) => ({
startEditing: value => () => dispatch(START_CHANGING, {value}),
stopEditing: () => dispatch(STOP_CHANGING),
changeValue: ev => dispatch(CHANGE_VALUE, {value: ev.target.value}),
checkEndChanging: modifyValue => ev => {
if (ev.key === 'Enter') {
return dispatch(STOP_CHANGING)
.then(() => modifyValue(getState().changedValue))
}
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment