44 lines
1.1 KiB
TypeScript
Raw Normal View History

2021-04-07 13:11:39 +03:00
import { AnyAction } from 'redux';
import { produce } from 'immer';
import { USE_OPEN_AUTHORING } from '../actions/auth';
export type GlobalUI = {
isFetching: boolean;
useOpenAuthoring: boolean;
};
2020-06-03 12:44:03 +03:00
const LOADING_IGNORE_LIST = [
'DEPLOY_PREVIEW',
'STATUS_REQUEST',
'STATUS_SUCCESS',
'STATUS_FAILURE',
];
2021-04-07 13:11:39 +03:00
function ignoreWhenLoading(action: AnyAction) {
return LOADING_IGNORE_LIST.some(type => action.type.includes(type));
}
2021-04-07 13:11:39 +03:00
const defaultState: GlobalUI = {
isFetching: false,
useOpenAuthoring: false,
};
/**
2019-03-15 10:19:57 -04:00
* Reducer for some global UI state that we want to share between components
*/
2021-04-07 13:11:39 +03:00
const globalUI = produce((state: GlobalUI, action: AnyAction) => {
// Generic, global loading indicator
if (!ignoreWhenLoading(action) && action.type.includes('REQUEST')) {
2021-04-07 13:11:39 +03:00
state.isFetching = true;
} else if (
!ignoreWhenLoading(action) &&
(action.type.includes('SUCCESS') || action.type.includes('FAILURE'))
) {
2021-04-07 13:11:39 +03:00
state.isFetching = false;
} else if (action.type === USE_OPEN_AUTHORING) {
2021-04-07 13:11:39 +03:00
state.useOpenAuthoring = true;
}
2021-04-07 13:11:39 +03:00
}, defaultState);
export default globalUI;