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

35 lines
800 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 {
2020-05-24 04:38:24 +05:30
Popup: React.ElementType;
props?: Record<string, any>;
// Don't allow hiding popup
disableOverlayClose?: boolean;
2019-12-07 16:58:52 +05:30
}
export type State = {
2020-05-24 04:38:24 +05:30
popups: Array<PopupConfig>;
2019-12-07 16:58:52 +05:30
};
export default combineReducers<State>({
2020-05-24 04:38:24 +05:30
popups,
2019-12-07 16:58:52 +05:30
});
function popups(state: Array<PopupConfig> = [], { type, payload }: Action) {
2020-05-24 04:38:24 +05:30
switch (type) {
case 'POPUP_CREATE':
if (!payload.Popup) {
throw new Error('Popup is required');
}
2019-12-07 16:58:52 +05:30
2020-05-24 04:38:24 +05:30
return state.concat(payload);
case 'POPUP_DESTROY':
return state.filter((popup) => popup !== payload);
default:
return state;
}
2019-12-07 16:58:52 +05:30
}