Browse Source

UI: Added new selector userReady and change order updated auth state

pull/10623/head
Vladyslav_Prykhodko 2 years ago
parent
commit
fe493335fb
  1. 14
      ui-ngx/src/app/app.component.ts
  2. 6
      ui-ngx/src/app/core/auth/auth.selectors.ts
  3. 4
      ui-ngx/src/app/core/auth/auth.service.ts

14
ui-ngx/src/app/app.component.ts

@ -21,14 +21,13 @@ import { Component, OnInit } from '@angular/core';
import { environment as env } from '@env/environment'; import { environment as env } from '@env/environment';
import { TranslateService } from '@ngx-translate/core'; import { TranslateService } from '@ngx-translate/core';
import { select, Store } from '@ngrx/store'; import { Store } from '@ngrx/store';
import { AppState } from '@core/core.state'; import { AppState } from '@core/core.state';
import { LocalStorageService } from '@core/local-storage/local-storage.service'; import { LocalStorageService } from '@core/local-storage/local-storage.service';
import { DomSanitizer } from '@angular/platform-browser'; import { DomSanitizer } from '@angular/platform-browser';
import { MatIconRegistry } from '@angular/material/icon'; import { MatIconRegistry } from '@angular/material/icon';
import { combineLatest } from 'rxjs'; import { getCurrentAuthState, selectUserReady } from '@core/auth/auth.selectors';
import { getCurrentAuthState, selectIsAuthenticated, selectIsUserLoaded } from '@core/auth/auth.selectors'; import { filter, skip, tap } from 'rxjs/operators';
import { debounceTime, filter, map, skip, tap } from 'rxjs/operators';
import { AuthService } from '@core/auth/auth.service'; import { AuthService } from '@core/auth/auth.service';
import { svgIcons, svgIconsUrl } from '@shared/models/icon.models'; import { svgIcons, svgIconsUrl } from '@shared/models/icon.models';
import { ActionSettingsChangeLanguage } from '@core/settings/settings.actions'; import { ActionSettingsChangeLanguage } from '@core/settings/settings.actions';
@ -89,12 +88,7 @@ export class AppComponent implements OnInit {
} }
setupAuth() { setupAuth() {
combineLatest([ this.store.select(selectUserReady).pipe(
this.store.pipe(select(selectIsAuthenticated)),
this.store.pipe(select(selectIsUserLoaded))]
).pipe(
debounceTime(1),
map(results => ({isAuthenticated: results[0], isUserLoaded: results[1]})),
filter((data) => data.isUserLoaded), filter((data) => data.isUserLoaded),
tap((data) => { tap((data) => {
let userLang = getCurrentAuthState(this.store).userDetails?.additionalInfo?.lang ?? null; let userLang = getCurrentAuthState(this.store).userDetails?.additionalInfo?.lang ?? null;

6
ui-ngx/src/app/core/auth/auth.selectors.ts

@ -42,6 +42,12 @@ export const selectIsUserLoaded = createSelector(
(state: AuthState) => state.isUserLoaded (state: AuthState) => state.isUserLoaded
); );
export const selectUserReady = createSelector(
selectIsAuthenticated,
selectIsUserLoaded,
(isAuthenticated, isUserLoaded) => ({isAuthenticated, isUserLoaded})
);
export const selectAuthUser = createSelector( export const selectAuthUser = createSelector(
selectAuthState, selectAuthState,
(state: AuthState) => state.authUser (state: AuthState) => state.authUser

4
ui-ngx/src/app/core/auth/auth.service.ts

@ -546,14 +546,14 @@ export class AuthService {
this.notifyUserLoaded(false); this.notifyUserLoaded(false);
this.loadUser(false).subscribe( this.loadUser(false).subscribe(
(authPayload) => { (authPayload) => {
this.notifyUserLoaded(true);
this.notifyAuthenticated(authPayload); this.notifyAuthenticated(authPayload);
this.notifyUserLoaded(true);
authenticatedSubject.next(true); authenticatedSubject.next(true);
authenticatedSubject.complete(); authenticatedSubject.complete();
}, },
() => { () => {
this.notifyUserLoaded(true);
this.notifyUnauthenticated(); this.notifyUnauthenticated();
this.notifyUserLoaded(true);
authenticatedSubject.next(false); authenticatedSubject.next(false);
authenticatedSubject.complete(); authenticatedSubject.complete();
} }

Loading…
Cancel
Save