accounts-frontend/packages/app/components/ui/popup/reducer.ts

35 lines
734 B
TypeScript
Raw Normal View History

2019-12-07 16:58:52 +05:30
import React from 'react';
import { combineReducers } from 'redux';
import { Action } from './actions';
2019-12-07 16:58:52 +05:30
export interface PopupConfig {
Popup: React.ElementType;
props?: Record<string, any>;
// Don't allow hiding popup
2019-12-07 16:58:52 +05:30
disableOverlayClose?: boolean;
}
export type State = {
popups: Array<PopupConfig>;
2019-12-07 16:58:52 +05:30
};
export default combineReducers<State>({
popups,
});
function popups(state: Array<PopupConfig> = [], { type, payload }: Action) {
2019-12-07 16:58:52 +05:30
switch (type) {
case 'POPUP_CREATE':
2019-12-07 16:58:52 +05:30
if (!payload.Popup) {
throw new Error('Popup is required');
}
return state.concat(payload);
case 'POPUP_DESTROY':
return state.filter((popup) => popup !== payload);
2019-12-07 16:58:52 +05:30
default:
return state;
}
}