Created
September 3, 2018 11:30
-
-
Save freele/68048aeb65385b555496548d55d67349 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { createAction, handleActions } from 'redux-actions'; | |
import Promise from 'bluebird'; | |
import { LOCATION_CHANGE } from 'react-router-redux'; | |
export const open = createAction('modal/OPEN'); | |
export const hide = createAction('modal/HIDE'); | |
export const close = createAction('modal/CLOSE', () => (dispatch) => { | |
dispatch(hide()); | |
return Promise.delay(500); | |
}); | |
export const update = createAction('modal/UPDATE'); | |
const initialState = { | |
modalType: null, | |
opened: false, | |
hiding: false, | |
modalProps: {}, | |
}; | |
export default handleActions({ | |
[open]: (state, { payload }) => ({ | |
...state, | |
hiding: false, | |
opened: true, | |
modalType: payload.modalType, | |
modalProps: payload.modalProps, | |
}), | |
[hide]: state => ({ | |
...state, | |
hiding: true, | |
opened: false, | |
}), | |
[close]: (state) => { | |
if (state.hiding) { | |
return initialState; | |
} | |
return state; | |
}, | |
[update]: (state, { payload }) => ({ | |
...state, | |
modalProps: { | |
...state.modalProps, | |
...payload, | |
}, | |
}), | |
[LOCATION_CHANGE]: () => initialState, | |
}, initialState); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment