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
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 36 additions and 31 deletions

View File

@ -255,7 +255,7 @@ class App extends React.Component {
function mapStateToProps(state) { function mapStateToProps(state) {
const { auth, config, collections, globalUI, mediaLibrary } = state; const { auth, config, collections, globalUI, mediaLibrary } = state;
const user = auth.user; const user = auth.user;
const isFetching = globalUI.get('isFetching'); const isFetching = globalUI.isFetching;
const publishMode = config.publish_mode; const publishMode = config.publish_mode;
const useMediaLibrary = !mediaLibrary.get('externalLibrary'); const useMediaLibrary = !mediaLibrary.get('externalLibrary');
const showMediaButton = mediaLibrary.get('showMediaButton'); const showMediaButton = mediaLibrary.get('showMediaButton');

View File

@ -436,7 +436,7 @@ function mapStateToProps(state, ownProps) {
const hasChanged = entryDraft.get('hasChanged'); const hasChanged = entryDraft.get('hasChanged');
const displayUrl = config.display_url; const displayUrl = config.display_url;
const hasWorkflow = config.publish_mode === EDITORIAL_WORKFLOW; const hasWorkflow = config.publish_mode === EDITORIAL_WORKFLOW;
const useOpenAuthoring = globalUI.get('useOpenAuthoring', false); const useOpenAuthoring = globalUI.useOpenAuthoring;
const isModification = entryDraft.getIn(['entry', 'isModification']); const isModification = entryDraft.getIn(['entry', 'isModification']);
const collectionEntriesLoaded = !!entries.getIn(['pages', collectionName]); const collectionEntriesLoaded = !!entries.getIn(['pages', collectionName]);
const unPublishedEntry = selectUnpublishedEntry(state, collectionName, slug); const unPublishedEntry = selectUnpublishedEntry(state, collectionName, slug);

View File

@ -138,7 +138,7 @@ class Workflow extends Component {
function mapStateToProps(state) { function mapStateToProps(state) {
const { collections, config, globalUI } = state; const { collections, config, globalUI } = state;
const isEditorialWorkflow = config.publish_mode === EDITORIAL_WORKFLOW; const isEditorialWorkflow = config.publish_mode === EDITORIAL_WORKFLOW;
const isOpenAuthoring = globalUI.get('useOpenAuthoring', false); const isOpenAuthoring = globalUI.useOpenAuthoring;
const returnObj = { collections, isEditorialWorkflow, isOpenAuthoring }; const returnObj = { collections, isEditorialWorkflow, isOpenAuthoring };
if (isEditorialWorkflow) { if (isEditorialWorkflow) {

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 { import {
DEPLOY_PREVIEW_REQUEST, DEPLOY_PREVIEW_REQUEST,
DEPLOY_PREVIEW_SUCCESS, DEPLOY_PREVIEW_SUCCESS,
DEPLOY_PREVIEW_FAILURE, DEPLOY_PREVIEW_FAILURE,
} from 'Actions/deploys'; } from '../../actions/deploys';
import { ENTRY_REQUEST, ENTRY_SUCCESS, ENTRY_FAILURE } from 'Actions/entries'; import { ENTRY_REQUEST, ENTRY_SUCCESS, ENTRY_FAILURE } from '../../actions/entries';
import reducer from '../globalUI'; import reducer from '../globalUI';
describe('globalUI', () => { describe('globalUI', () => {
it('should set isFetching to true on entry request', () => { it('should set isFetching to true on entry request', () => {
expect(reducer(Map({ isFetching: false }), { type: ENTRY_REQUEST })).toEqual( expect(reducer({ isFetching: false }, { type: ENTRY_REQUEST })).toEqual({ isFetching: true });
Map({ isFetching: true }),
);
}); });
it('should set isFetching to false on entry success', () => { it('should set isFetching to false on entry success', () => {
expect(reducer(Map({ isFetching: true }), { type: ENTRY_SUCCESS })).toEqual( expect(reducer({ isFetching: true }, { type: ENTRY_SUCCESS })).toEqual({ isFetching: false });
Map({ isFetching: false }),
);
}); });
it('should set isFetching to false on entry failure', () => { it('should set isFetching to false on entry failure', () => {
expect(reducer(Map({ isFetching: true }), { type: ENTRY_FAILURE })).toEqual( expect(reducer({ isFetching: true }, { type: ENTRY_FAILURE })).toEqual({ isFetching: false });
Map({ isFetching: false }),
);
}); });
it('should not change state on deploy preview request', () => { 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); expect(reducer(state, { type: DEPLOY_PREVIEW_REQUEST })).toBe(state);
}); });
it('should not change state on deploy preview success', () => { 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); expect(reducer(state, { type: DEPLOY_PREVIEW_SUCCESS })).toBe(state);
}); });
it('should not change state on deploy preview failure', () => { 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); expect(reducer(state, { type: DEPLOY_PREVIEW_FAILURE })).toBe(state);
}); });
it('should set useOpenAuthoring to true on USE_OPEN_AUTHORING', () => { it('should set useOpenAuthoring to true on USE_OPEN_AUTHORING', () => {
expect(reducer(Map({ useOpenAuthoring: false }), { type: USE_OPEN_AUTHORING })).toEqual( expect(reducer({ useOpenAuthoring: false }, { type: USE_OPEN_AUTHORING })).toEqual({
Map({ useOpenAuthoring: true }), useOpenAuthoring: true,
); });
}); });
}); });

View File

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

View File

@ -8,6 +8,7 @@ import { Status } from '../reducers/status';
import { Medias } from '../reducers/medias'; import { Medias } from '../reducers/medias';
import { Deploys } from '../reducers/deploys'; import { Deploys } from '../reducers/deploys';
import { Search } from '../reducers/search'; import { Search } from '../reducers/search';
import { GlobalUI } from '../reducers/globalUI';
export type CmsBackendType = export type CmsBackendType =
| 'azure' | 'azure'
@ -684,6 +685,7 @@ export interface State {
cursors: Cursors; cursors: Cursors;
collections: Collections; collections: Collections;
deploys: Deploys; deploys: Deploys;
globalUI: GlobalUI;
editorialWorkflow: EditorialWorkflow; editorialWorkflow: EditorialWorkflow;
entries: Entries; entries: Entries;
entryDraft: EntryDraft; entryDraft: EntryDraft;