From deecb3c1ccfb906f6e51a98a34e75decf498fc6a Mon Sep 17 00:00:00 2001 From: Nicoleta Panaghiu Date: Fri, 18 Aug 2023 16:56:17 +0300 Subject: [PATCH] RED-7242: Fixed refresh error on trash screen. --- apps/red-ui/src/app/app-routing.module.ts | 2 +- apps/red-ui/src/app/guards/trash.guard.ts | 24 ++++++++++++++++--- .../services/system-preferences.service.ts | 6 +++++ apps/red-ui/src/app/utils/main.resolver.ts | 6 ++++- 4 files changed, 33 insertions(+), 5 deletions(-) diff --git a/apps/red-ui/src/app/app-routing.module.ts b/apps/red-ui/src/app/app-routing.module.ts index 64ff6c3d7..3ea027e14 100644 --- a/apps/red-ui/src/app/app-routing.module.ts +++ b/apps/red-ui/src/app/app-routing.module.ts @@ -160,7 +160,7 @@ const mainRoutes: IqserRoutes = [ { path: 'trash', loadChildren: () => import('./modules/trash/trash.module').then(m => m.TrashModule), - canActivate: [CompositeRouteGuard, IqserPermissionsGuard, loadActiveDossiersGuard()], + canActivate: [CompositeRouteGuard, IqserPermissionsGuard], data: { routeGuards: [IqserAuthGuard, RedRoleGuard, TrashGuard], permissions: { diff --git a/apps/red-ui/src/app/guards/trash.guard.ts b/apps/red-ui/src/app/guards/trash.guard.ts index 542a4ede1..013090f93 100644 --- a/apps/red-ui/src/app/guards/trash.guard.ts +++ b/apps/red-ui/src/app/guards/trash.guard.ts @@ -1,14 +1,32 @@ -import { Injectable } from '@angular/core'; -import { CanActivate } from '@angular/router'; +import { inject, Injectable } from '@angular/core'; +import { CanActivate, CanActivateFn } from '@angular/router'; import { firstValueFrom } from 'rxjs'; import { TrashService } from '@services/entity-services/trash.service'; +import { SystemPreferencesService } from '@services/system-preferences.service'; +import { ActiveDossiersService } from '@services/dossiers/active-dossiers.service'; +import { ACTIVE_DOSSIERS_SERVICE } from '../tokens'; @Injectable({ providedIn: 'root' }) export class TrashGuard implements CanActivate { - constructor(private readonly _trashService: TrashService) {} + constructor( + private readonly _trashService: TrashService, + private readonly _systemPreferences: SystemPreferencesService, + private readonly _activeDossierService: ActiveDossiersService, + ) {} async canActivate(): Promise { + await this._systemPreferences.loadPreferencesIfNeeded(); + await firstValueFrom(this._activeDossierService.loadAll()); await firstValueFrom(this._trashService.loadAll()); return true; } } + +export function trashGuard(): CanActivateFn { + return async () => { + await inject(SystemPreferencesService).loadPreferencesIfNeeded(); + await firstValueFrom(inject(ACTIVE_DOSSIERS_SERVICE).loadAll()); + await firstValueFrom(inject(TrashService).loadAll()); + return true; + }; +} diff --git a/apps/red-ui/src/app/services/system-preferences.service.ts b/apps/red-ui/src/app/services/system-preferences.service.ts index d4a6a90ca..23832588e 100644 --- a/apps/red-ui/src/app/services/system-preferences.service.ts +++ b/apps/red-ui/src/app/services/system-preferences.service.ts @@ -18,6 +18,12 @@ export class SystemPreferencesService extends GenericService return this.values; } + async loadPreferencesIfNeeded() { + if (!this.values) { + await this.loadPreferences(); + } + } + async update(value: SystemPreferences) { await firstValueFrom(this._post(value)); return await this.loadPreferences(); diff --git a/apps/red-ui/src/app/utils/main.resolver.ts b/apps/red-ui/src/app/utils/main.resolver.ts index aadfd031a..00e6cf491 100644 --- a/apps/red-ui/src/app/utils/main.resolver.ts +++ b/apps/red-ui/src/app/utils/main.resolver.ts @@ -40,7 +40,11 @@ export const mainResolver: ResolveFn = async () => { const generalConfig$ = inject(GeneralSettingsService).getGeneralConfigurations(); const updatedDisplayName$ = generalConfig$.pipe(tap(config => configService.updateDisplayName(config.displayName))); - await Promise.all([systemPreferencesService.loadPreferences(), userPreferenceService.reload(), firstValueFrom(updatedDisplayName$)]); + await Promise.all([ + systemPreferencesService.loadPreferencesIfNeeded(), + userPreferenceService.reload(), + firstValueFrom(updatedDisplayName$), + ]); const lastDossierTemplate = userPreferenceService.getLastDossierTemplate();