diff --git a/packages/netlify-cms-core/src/actions/status.ts b/packages/netlify-cms-core/src/actions/status.ts index 72294d28..b39faee8 100644 --- a/packages/netlify-cms-core/src/actions/status.ts +++ b/packages/netlify-cms-core/src/actions/status.ts @@ -13,7 +13,7 @@ export const STATUS_FAILURE = 'STATUS_FAILURE'; export function statusRequest() { return { type: STATUS_REQUEST, - }; + } as const; } export function statusSuccess(status: { @@ -23,21 +23,21 @@ export function statusSuccess(status: { return { type: STATUS_SUCCESS, payload: { status }, - }; + } as const; } export function statusFailure(error: Error) { return { type: STATUS_FAILURE, payload: { error }, - }; + } as const; } export function checkBackendStatus() { return async (dispatch: ThunkDispatch, getState: () => State) => { try { const state = getState(); - if (state.status.get('isFetching') === true) { + if (state.status.get('isFetching')) { return; } @@ -90,3 +90,7 @@ export function checkBackendStatus() { } }; } + +export type StatusAction = ReturnType< + typeof statusRequest | typeof statusSuccess | typeof statusFailure +>; diff --git a/packages/netlify-cms-core/src/backend.ts b/packages/netlify-cms-core/src/backend.ts index 69c02de0..f36713f0 100644 --- a/packages/netlify-cms-core/src/backend.ts +++ b/packages/netlify-cms-core/src/backend.ts @@ -334,11 +334,11 @@ export class Backend { auth: { status: boolean }; api: { status: boolean; statusPage: string }; } = { - auth: { status: false }, - api: { status: false, statusPage: '' }, + auth: { status: true }, + api: { status: true, statusPage: '' }, }; for (let i = 1; i <= attempts; i++) { - status = await this.implementation!.status(); + status = await this.implementation.status(); // return on first success if (Object.values(status).every(s => s.status === true)) { return status; diff --git a/packages/netlify-cms-core/src/reducers/auth.ts b/packages/netlify-cms-core/src/reducers/auth.ts index c2a755c9..ab66b37c 100644 --- a/packages/netlify-cms-core/src/reducers/auth.ts +++ b/packages/netlify-cms-core/src/reducers/auth.ts @@ -20,9 +20,9 @@ export const defaultState = fromJS({ isFetching: false, user: undefined, error: undefined, -}); +}) as Auth; -const auth = (state = defaultState as Auth, action: AuthAction) => { +const auth = (state = defaultState, action: AuthAction) => { switch (action.type) { case AUTH_REQUEST: return state.set('isFetching', true); diff --git a/packages/netlify-cms-core/src/reducers/status.ts b/packages/netlify-cms-core/src/reducers/status.ts index a1ff1d3b..5b14305a 100644 --- a/packages/netlify-cms-core/src/reducers/status.ts +++ b/packages/netlify-cms-core/src/reducers/status.ts @@ -1,16 +1,26 @@ -import { Map, fromJS } from 'immutable'; -import { AnyAction } from 'redux'; -import { STATUS_REQUEST, STATUS_SUCCESS, STATUS_FAILURE } from '../actions/status'; -import { Status } from '../types/redux'; +import { fromJS } from 'immutable'; +import { STATUS_REQUEST, STATUS_SUCCESS, STATUS_FAILURE, StatusAction } from '../actions/status'; +import { StaticallyTypedRecord } from '../types/immutable'; -interface StatusAction extends AnyAction { - payload: { - status: { auth: { status: boolean }; api: { status: boolean; statusPage: string } }; - error?: Error; - }; -} +export type Status = StaticallyTypedRecord<{ + isFetching: boolean; + status: StaticallyTypedRecord<{ + auth: StaticallyTypedRecord<{ status: boolean }>; + api: StaticallyTypedRecord<{ status: boolean; statusPage: string }>; + }>; + error: Error | undefined; +}>; -const status = (state = Map(), action: StatusAction) => { +const defaultState = fromJS({ + isFetching: false, + status: { + auth: { status: true }, + api: { status: true, statusPage: '' }, + }, + error: undefined, +}) as Status; + +const status = (state = defaultState, action: StatusAction) => { switch (action.type) { case STATUS_REQUEST: return state.set('isFetching', true); @@ -30,7 +40,7 @@ const status = (state = Map(), action: StatusAction) => { }; export const selectStatus = (status: Status) => { - return status.get('status')?.toJS() || {}; + return status.get('status').toJS(); }; export default status; diff --git a/packages/netlify-cms-core/src/types/immutable.ts b/packages/netlify-cms-core/src/types/immutable.ts index 2d1325ac..7549879b 100644 --- a/packages/netlify-cms-core/src/types/immutable.ts +++ b/packages/netlify-cms-core/src/types/immutable.ts @@ -35,4 +35,5 @@ export interface StaticallyTypedRecord { mapFunc: (value: T[K]) => V, ): StaticallyTypedRecord<{ [key: string]: V }>; keySeq(): { toArray: () => K[] }; + withMutations(mutator: (mutable: StaticallyTypedRecord) => unknown): StaticallyTypedRecord; } diff --git a/packages/netlify-cms-core/src/types/redux.ts b/packages/netlify-cms-core/src/types/redux.ts index 2eb5b174..5bc8f3c6 100644 --- a/packages/netlify-cms-core/src/types/redux.ts +++ b/packages/netlify-cms-core/src/types/redux.ts @@ -4,6 +4,7 @@ import { Map, List, OrderedMap, Set } from 'immutable'; import AssetProxy from '../valueObjects/AssetProxy'; import { MediaFile as BackendMediaFile } from '../backend'; import { Auth } from '../reducers/auth'; +import { Status } from '../reducers/status'; export type SlugConfig = StaticallyTypedRecord<{ encoding: string; @@ -290,11 +291,6 @@ export type Search = StaticallyTypedRecord<{ export type Cursors = StaticallyTypedRecord<{}>; -export type Status = StaticallyTypedRecord<{ - isFetching: boolean; - status: StaticallyTypedRecord<{ auth: boolean }>; -}>; - export interface State { auth: Auth; config: Config;