chore: refactor globalUI (#5224)

This commit is contained in:
Vladislav Shkodin
2021-04-07 13:11:39 +03:00
committed by GitHub
parent 9ad9c8acd8
commit d9d40d90c6
6 changed files with 36 additions and 31 deletions

View File

@ -1,50 +1,43 @@
import { Map } from 'immutable';
import { USE_OPEN_AUTHORING } from 'Actions/auth';
import { USE_OPEN_AUTHORING } from '../../actions/auth';
import {
DEPLOY_PREVIEW_REQUEST,
DEPLOY_PREVIEW_SUCCESS,
DEPLOY_PREVIEW_FAILURE,
} from 'Actions/deploys';
import { ENTRY_REQUEST, ENTRY_SUCCESS, ENTRY_FAILURE } from 'Actions/entries';
} from '../../actions/deploys';
import { ENTRY_REQUEST, ENTRY_SUCCESS, ENTRY_FAILURE } from '../../actions/entries';
import reducer from '../globalUI';
describe('globalUI', () => {
it('should set isFetching to true on entry request', () => {
expect(reducer(Map({ isFetching: false }), { type: ENTRY_REQUEST })).toEqual(
Map({ isFetching: true }),
);
expect(reducer({ isFetching: false }, { type: ENTRY_REQUEST })).toEqual({ isFetching: true });
});
it('should set isFetching to false on entry success', () => {
expect(reducer(Map({ isFetching: true }), { type: ENTRY_SUCCESS })).toEqual(
Map({ isFetching: false }),
);
expect(reducer({ isFetching: true }, { type: ENTRY_SUCCESS })).toEqual({ isFetching: false });
});
it('should set isFetching to false on entry failure', () => {
expect(reducer(Map({ isFetching: true }), { type: ENTRY_FAILURE })).toEqual(
Map({ isFetching: false }),
);
expect(reducer({ isFetching: true }, { type: ENTRY_FAILURE })).toEqual({ isFetching: false });
});
it('should not change state on deploy preview request', () => {
const state = Map({ isFetching: false });
const state = { isFetching: false };
expect(reducer(state, { type: DEPLOY_PREVIEW_REQUEST })).toBe(state);
});
it('should not change state on deploy preview success', () => {
const state = Map({ isFetching: true });
const state = { isFetching: true };
expect(reducer(state, { type: DEPLOY_PREVIEW_SUCCESS })).toBe(state);
});
it('should not change state on deploy preview failure', () => {
const state = Map({ isFetching: true });
const state = { isFetching: true };
expect(reducer(state, { type: DEPLOY_PREVIEW_FAILURE })).toBe(state);
});
it('should set useOpenAuthoring to true on USE_OPEN_AUTHORING', () => {
expect(reducer(Map({ useOpenAuthoring: false }), { type: USE_OPEN_AUTHORING })).toEqual(
Map({ useOpenAuthoring: true }),
);
expect(reducer({ useOpenAuthoring: false }, { type: USE_OPEN_AUTHORING })).toEqual({
useOpenAuthoring: true,
});
});
});

View File

@ -1,5 +1,11 @@
import { Map } from 'immutable';
import { USE_OPEN_AUTHORING } from 'Actions/auth';
import { AnyAction } from 'redux';
import { produce } from 'immer';
import { USE_OPEN_AUTHORING } from '../actions/auth';
export type GlobalUI = {
isFetching: boolean;
useOpenAuthoring: boolean;
};
const LOADING_IGNORE_LIST = [
'DEPLOY_PREVIEW',
@ -8,26 +14,30 @@ const LOADING_IGNORE_LIST = [
'STATUS_FAILURE',
];
function ignoreWhenLoading(action) {
function ignoreWhenLoading(action: AnyAction) {
return LOADING_IGNORE_LIST.some(type => action.type.includes(type));
}
const defaultState: GlobalUI = {
isFetching: false,
useOpenAuthoring: false,
};
/**
* Reducer for some global UI state that we want to share between components
*/
function globalUI(state = Map({ isFetching: false, useOpenAuthoring: false }), action) {
const globalUI = produce((state: GlobalUI, action: AnyAction) => {
// Generic, global loading indicator
if (!ignoreWhenLoading(action) && action.type.includes('REQUEST')) {
return state.set('isFetching', true);
state.isFetching = true;
} else if (
!ignoreWhenLoading(action) &&
(action.type.includes('SUCCESS') || action.type.includes('FAILURE'))
) {
return state.set('isFetching', false);
state.isFetching = false;
} else if (action.type === USE_OPEN_AUTHORING) {
return state.set('useOpenAuthoring', true);
state.useOpenAuthoring = true;
}
return state;
}
}, defaultState);
export default globalUI;