From 428353d37938f6f1f39d3c08f784374b617e620b Mon Sep 17 00:00:00 2001 From: Willem Dantuma Date: Thu, 23 Jul 2020 09:42:44 +0200 Subject: [PATCH] Fix inituser flow --- .../common/src/fm/effects/app-common.effects.ts | 15 +++++---------- .../common/src/fm/reducers/app-common.reducer.ts | 5 ++++- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/projects/common/src/fm/effects/app-common.effects.ts b/projects/common/src/fm/effects/app-common.effects.ts index 10229c3..be45a69 100644 --- a/projects/common/src/fm/effects/app-common.effects.ts +++ b/projects/common/src/fm/effects/app-common.effects.ts @@ -3,8 +3,8 @@ import { Router } from '@angular/router'; import { OAuthService,UserInfo } from 'angular-oauth2-oidc'; import { Store, Action } from '@ngrx/store'; import { Effect, Actions,ofType } from '@ngrx/effects'; -import { Observable , defer , of,from } from 'rxjs'; -import { withLatestFrom,mergeMap,switchMap,map,catchError} from 'rxjs/operators'; +import { Observable , defer , of,from,zip } from 'rxjs'; +import { withLatestFrom,mergeMap,switchMap,map,catchError,first} from 'rxjs/operators'; import * as appCommonActions from '../actions/app-common.actions'; import * as appCommonReducers from '../reducers/app-common.reducer'; import { ItemService } from '../services/item.service'; @@ -50,16 +50,11 @@ export class AppCommonEffects { @Effect() initUser$: Observable = this.actions$.pipe( ofType(appCommonActions.INITUSER), - withLatestFrom(this.store$.select(appCommonReducers.selectGetInitialized)), - switchMap(([action, initialized]) => { - if(!initialized) { - return this.userService$.getCurrentUser().pipe( - withLatestFrom(from(this.oauthService$.loadUserProfile())), + first(), + switchMap((action) => { + return zip(this.userService$.getCurrentUser(),from(this.oauthService$.loadUserProfile())).pipe( switchMap(([user,userInfo]) => {return of(new appCommonActions.InitUserSuccess(user,userInfo as UserInfo))} ), catchError(error => of(new appCommonActions.Fail(error)))) - } else { - return []; - } } )); diff --git a/projects/common/src/fm/reducers/app-common.reducer.ts b/projects/common/src/fm/reducers/app-common.reducer.ts index cabd77d..19a5fa0 100644 --- a/projects/common/src/fm/reducers/app-common.reducer.ts +++ b/projects/common/src/fm/reducers/app-common.reducer.ts @@ -38,6 +38,9 @@ export const initialState: State = { export function reducer(state = initialState, action: appCommonActions.Actions ): State { switch (action.type) { + case appCommonActions.INITUSER: { + return tassign(state,{initialized: true}); + } case appCommonActions.INITUSERSUCCESS: { let a = action as appCommonActions.InitUserSuccess; let claims = {} @@ -50,7 +53,7 @@ export function reducer(state = initialState, action: appCommonActions.Actions ) name:a.userinfo["name"], claims:claims }; - return tassign(state, { user: user,initialized: true }); + return tassign(state, { user: user }); } case appCommonActions.INITROOTSUCCESS: { let a = action as appCommonActions.InitRootSuccess;