decap-cms-core
Version:
Decap CMS core application, see decap-cms package for the main distribution.
47 lines (40 loc) • 972 B
text/typescript
import { produce } from 'immer';
import {
AUTH_REQUEST,
AUTH_SUCCESS,
AUTH_FAILURE,
AUTH_REQUEST_DONE,
LOGOUT,
} from '../actions/auth';
import type { User } from 'decap-cms-lib-util';
import type { AuthAction } from '../actions/auth';
export type Auth = {
isFetching: boolean;
user: User | undefined;
error: string | undefined;
};
export const defaultState: Auth = {
isFetching: false,
user: undefined,
error: undefined,
};
const auth = produce((state: Auth, action: AuthAction) => {
switch (action.type) {
case AUTH_REQUEST:
state.isFetching = true;
break;
case AUTH_SUCCESS:
state.user = action.payload;
break;
case AUTH_FAILURE:
state.error = action.payload && action.payload.toString();
break;
case AUTH_REQUEST_DONE:
state.isFetching = false;
break;
case LOGOUT:
state.user = undefined;
state.isFetching = false;
}
}, defaultState);
export default auth;