RED-3800: Functional composite route guard, skeleton fixes

This commit is contained in:
Adina Țeudan 2023-09-21 18:30:13 +03:00
parent 2f2ee530b1
commit 890cf417a1
2 changed files with 25 additions and 6 deletions

View File

@ -1,6 +1,6 @@
import { ActivatedRouteSnapshot, CanActivate, RouterStateSnapshot, UrlTree } from '@angular/router';
import { Injectable, InjectionToken, Injector } from '@angular/core';
import { firstValueFrom, from, of } from 'rxjs';
import { ActivatedRouteSnapshot, CanActivate, CanActivateFn, RouterStateSnapshot, UrlTree } from '@angular/router';
import { inject, Injectable, InjectionToken, Injector, runInInjectionContext } from '@angular/core';
import { concatMap, firstValueFrom, from, last, of, takeWhile } from 'rxjs';
import { LoadingService } from '../loading';
import { SkeletonService } from './skeleton.service';
@ -62,3 +62,22 @@ export class CompositeRouteGuard implements CanActivate {
}
}
}
export type AsyncGuard = (route: ActivatedRouteSnapshot, state: RouterStateSnapshot) => Promise<boolean>;
export function orderedAsyncGuards(guards: Array<AsyncGuard>): CanActivateFn {
return (route, state) => {
const injector = inject(Injector);
return from(guards).pipe(
// For each guard, fire canActivate and wait for it
// to complete.
concatMap(guard => runInInjectionContext(injector, () => guard(route, state))),
// Don't execute the next guard if the current guard's
// result is not true.
takeWhile(value => value === true, /* inclusive */ true),
// Return the last guard's result.
last(),
);
};
}

View File

@ -1,6 +1,6 @@
import { ChangeDetectionStrategy, Component, HostBinding, inject, Input, TemplateRef } from '@angular/core';
import { SkeletonService } from '../../services';
import { getCurrentUser } from '../../users';
import { IqserUserService } from '../../users';
import { AsyncPipe, NgForOf, NgIf, NgTemplateOutlet } from '@angular/common';
import { tap } from 'rxjs/operators';
@ -16,10 +16,10 @@ export class SkeletonComponent {
@Input() templates!: Record<string, TemplateRef<unknown>>;
@HostBinding('style.display') display = 'none';
readonly #currentUser = getCurrentUser();
readonly iqserUserService = inject(IqserUserService);
readonly type$ = inject(SkeletonService).type$.pipe(
tap(type => {
this.display = type && this.#currentUser ? 'block' : 'none';
this.display = type && this.iqserUserService.currentUser ? 'block' : 'none';
}),
);
}