2022-06-07 09:25:53 -07:00
|
|
|
import { List as ImmutableList, Record as ImmutableRecord } from 'immutable';
|
2022-01-30 09:46:57 -08:00
|
|
|
|
2022-02-02 05:33:12 -08:00
|
|
|
import { MODAL_OPEN, MODAL_CLOSE } from '../actions/modals';
|
2020-03-27 13:59:38 -07:00
|
|
|
|
2022-06-07 09:25:53 -07:00
|
|
|
import type { AnyAction } from 'redux';
|
2020-03-27 13:59:38 -07:00
|
|
|
|
2022-06-07 09:25:53 -07:00
|
|
|
const ModalRecord = ImmutableRecord({
|
|
|
|
modalType: '',
|
|
|
|
modalProps: null as Record<string, any> | null,
|
|
|
|
});
|
|
|
|
|
|
|
|
type Modal = ReturnType<typeof ModalRecord>;
|
|
|
|
type State = ImmutableList<Modal>;
|
|
|
|
|
|
|
|
export default function modal(state: State = ImmutableList<Modal>(), action: AnyAction) {
|
2022-05-11 10:40:34 -07:00
|
|
|
switch (action.type) {
|
2022-05-11 14:06:35 -07:00
|
|
|
case MODAL_OPEN:
|
2022-06-07 09:25:53 -07:00
|
|
|
return state.push(ModalRecord({ modalType: action.modalType, modalProps: action.modalProps }));
|
2022-05-11 14:06:35 -07:00
|
|
|
case MODAL_CLOSE:
|
|
|
|
if (state.size === 0) {
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
if (action.modalType === undefined) {
|
|
|
|
return state.pop();
|
|
|
|
}
|
|
|
|
if (state.some(({ modalType }) => action.modalType === modalType)) {
|
|
|
|
return state.slice(0, state.findLastIndex(({ modalType }) => action.modalType === modalType));
|
|
|
|
}
|
|
|
|
return state;
|
|
|
|
default:
|
2022-01-30 09:46:57 -08:00
|
|
|
return state;
|
2020-03-27 13:59:38 -07:00
|
|
|
}
|
2021-08-03 12:22:51 -07:00
|
|
|
}
|