From b0a3fa5c5c49914e3ec8666a04ee1806431e7f26 Mon Sep 17 00:00:00 2001 From: Mike Ryan Date: Sat, 28 Mar 2020 20:14:39 -0500 Subject: [PATCH] 13-auth-state --- src/app/shared/state/auth.reducer.ts | 36 +++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/src/app/shared/state/auth.reducer.ts b/src/app/shared/state/auth.reducer.ts index 95195a8..e999aef 100644 --- a/src/app/shared/state/auth.reducer.ts +++ b/src/app/shared/state/auth.reducer.ts @@ -1,3 +1,37 @@ import { UserModel } from "../models"; -import { createReducer, on } from "@ngrx/store"; +import { createReducer, on, Action } from "@ngrx/store"; import { AuthApiActions, AuthUserActions } from "src/app/auth/actions"; + +export interface State { + gettingStatus: boolean; + user: null | UserModel; + error: null | string; +} + +const initialState: State = { + gettingStatus: true, + user: null, + error: null +}; + +export const authReducer = createReducer( + initialState, + on(AuthUserActions.logout, (state, action) => { + return { + gettingStatus: false, + user: null, + error: null + }; + }), + on(AuthUserActions.login, (state, action) => { + return { + gettingStatus: true, + user: null, + error: null + }; + }) +); + +export function reducer(state: State | undefined, action: Action) { + return authReducer(state, action); +}