import { createReducer } from 'reduxsauce'; import { IService } from '../../utils/types'; import { AuthAction, authActionTypes, IGetSessionSuccessAction, IResetAuthStateAction, } from './actions'; export interface IAuthState { success: boolean; isNewUser: boolean | null; service: IService | null; } const INITIAL_STATE: IAuthState = { success: false, isNewUser: null, service: null, }; type Handler = (state: IAuthState, action: A) => IAuthState; const getSessionSuccess: Handler = (state, { isNewUser, service }) => ({ ...state, isNewUser, service, }); const verifyUserSessionSuccess = () => ({ ...INITIAL_STATE, success: true }); const resetAuthState: Handler = () => INITIAL_STATE; export const authReducer = createReducer(INITIAL_STATE, { [authActionTypes.GET_SESSION_SUCCESS]: getSessionSuccess, [authActionTypes.VERIFY_USER_SESSION_SUCCESS]: verifyUserSessionSuccess, [authActionTypes.RESET_AUTH_STATE]: resetAuthState, });