Allow stacking modals See merge request soapbox-pub/soapbox-fe!1017merge-requests/1039/head
commit
8334d278a2
@ -1,46 +0,0 @@
|
||||
import { MODAL_OPEN, MODAL_CLOSE } from 'soapbox/actions/modal';
|
||||
|
||||
import reducer from '../modal';
|
||||
|
||||
describe('modal reducer', () => {
|
||||
it('should return the initial state', () => {
|
||||
expect(reducer(undefined, {})).toEqual({
|
||||
modalType: null,
|
||||
modalProps: {},
|
||||
noPop: false,
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle MODAL_OPEN', () => {
|
||||
const state = {
|
||||
modalType: null,
|
||||
modalProps: {},
|
||||
noPop: false,
|
||||
};
|
||||
const action = {
|
||||
type: MODAL_OPEN,
|
||||
modalType: 'type1',
|
||||
modalProps: { props1: '1' },
|
||||
};
|
||||
expect(reducer(state, action)).toMatchObject({
|
||||
modalType: 'type1',
|
||||
modalProps: { props1: '1' },
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle MODAL_CLOSE', () => {
|
||||
const state = {
|
||||
modalType: 'type1',
|
||||
modalProps: { props1: '1' },
|
||||
};
|
||||
const action = {
|
||||
type: MODAL_CLOSE,
|
||||
};
|
||||
expect(reducer(state, action)).toMatchObject({
|
||||
modalType: null,
|
||||
modalProps: {},
|
||||
noPop: false,
|
||||
});
|
||||
});
|
||||
|
||||
});
|
@ -0,0 +1,57 @@
|
||||
import { List as ImmutableList } from 'immutable';
|
||||
|
||||
import { MODAL_OPEN, MODAL_CLOSE } from 'soapbox/actions/modals';
|
||||
|
||||
import reducer from '../modals';
|
||||
|
||||
describe('modal reducer', () => {
|
||||
it('should return the initial state', () => {
|
||||
expect(reducer(undefined, {})).toEqual(ImmutableList());
|
||||
});
|
||||
|
||||
it('should handle MODAL_OPEN', () => {
|
||||
const state = ImmutableList();
|
||||
const action = {
|
||||
type: MODAL_OPEN,
|
||||
modalType: 'type1',
|
||||
modalProps: { props1: '1' },
|
||||
};
|
||||
expect(reducer(state, action)).toMatchObject(ImmutableList([{
|
||||
modalType: 'type1',
|
||||
modalProps: { props1: '1' },
|
||||
}]));
|
||||
});
|
||||
|
||||
it('should handle MODAL_CLOSE', () => {
|
||||
const state = ImmutableList([{
|
||||
modalType: 'type1',
|
||||
modalProps: { props1: '1' },
|
||||
}]);
|
||||
const action = {
|
||||
type: MODAL_CLOSE,
|
||||
};
|
||||
expect(reducer(state, action)).toMatchObject(ImmutableList());
|
||||
});
|
||||
|
||||
it('should handle MODAL_CLOSE with specified modalType', () => {
|
||||
const state = ImmutableList([
|
||||
{
|
||||
modalType: 'type1',
|
||||
},
|
||||
{
|
||||
modalType: 'type2',
|
||||
},
|
||||
{
|
||||
modalType: 'type1',
|
||||
},
|
||||
]);
|
||||
const action = {
|
||||
type: MODAL_CLOSE,
|
||||
modalType: 'type2',
|
||||
};
|
||||
expect(reducer(state, action)).toMatchObject(ImmutableList([{
|
||||
modalType: 'type1',
|
||||
}]));
|
||||
});
|
||||
|
||||
});
|
@ -1,23 +0,0 @@
|
||||
import { MODAL_OPEN, MODAL_CLOSE } from '../actions/modal';
|
||||
|
||||
const initialState = {
|
||||
modalType: null,
|
||||
modalProps: {},
|
||||
noPop: false,
|
||||
};
|
||||
|
||||
export default function modal(state = initialState, action) {
|
||||
switch(action.type) {
|
||||
case MODAL_OPEN:
|
||||
return { modalType: action.modalType, modalProps: action.modalProps };
|
||||
case MODAL_CLOSE:
|
||||
return {
|
||||
...(action.modalType === undefined || action.modalType === state.modalType)
|
||||
? initialState
|
||||
: state,
|
||||
noPop: !!action.noPop,
|
||||
};
|
||||
default:
|
||||
return state;
|
||||
}
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
import { List as ImmutableList } from 'immutable';
|
||||
|
||||
import { MODAL_OPEN, MODAL_CLOSE } from '../actions/modals';
|
||||
|
||||
const initialState = ImmutableList();
|
||||
|
||||
export default function modal(state = initialState, action) {
|
||||
switch(action.type) {
|
||||
case MODAL_OPEN:
|
||||
return state.push({ modalType: action.modalType, modalProps: action.modalProps });
|
||||
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:
|
||||
return state;
|
||||
}
|
||||
}
|
Loading…
Reference in new issue