Skip to content

Instantly share code, notes, and snippets.

@freele
Created September 3, 2018 11:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save freele/68048aeb65385b555496548d55d67349 to your computer and use it in GitHub Desktop.
Save freele/68048aeb65385b555496548d55d67349 to your computer and use it in GitHub Desktop.
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