File

guards/dynamic-hazard-compare.guard.ts

Description

Guard to allow dynamic compare application launch.

Will not be able to launch if there are no models to compare.

Index

Properties
Methods
Accessors

Methods

canActivate
canActivate()

Returns true if there are comparable models.

Returns : Observable | Promise | boolean | UrlTree

Properties

errorMessage
Default value : `No comparable models available. <br> Application cannot be launch.`

Error message

Private hazardService
Default value : inject(HazardService)
nshmpHazWs
Default value : environment.webServices.nshmpHazWs

nshmp-haz-ws web config

Private nshmpService
Default value : inject(NshmpService)
Private router
Default value : inject(Router)
serviceEndpoint
Default value : this.nshmpHazWs.services.curveServices.hazard

Hazard endpoint

Private spinnerService
Default value : inject(SpinnerService)

Accessors

hasComparableModels$
gethasComparableModels$()

Check to see if there are any comparable models.

Returns : Observable<boolean>
import {inject, Injectable} from '@angular/core';
import {Router, UrlTree} from '@angular/router';
import {HazardService} from '@ghsc/nshmp-lib-ng/hazard';
import {NshmpService} from '@ghsc/nshmp-lib-ng/nshmp';
import {SpinnerService} from '@ghsc/nshmp-template';
import {environment} from 'projects/nshmp-apps/src/environments/environment';
import {catchError, map, Observable, of} from 'rxjs';

/**
 * Guard to allow dynamic compare application launch.
 *
 * Will not be able to launch if there are no models to compare.
 */
@Injectable({
  providedIn: 'root',
})
export class DynamicHazardCompareGuard {
  private router = inject(Router);
  private hazardService = inject(HazardService);
  private spinnerService = inject(SpinnerService);
  private nshmpService = inject(NshmpService);

  /** Error message */
  errorMessage = `No comparable models available. <br>
    Application cannot be launch.`;
  /** nshmp-haz-ws web config */
  nshmpHazWs = environment.webServices.nshmpHazWs;
  /** Hazard endpoint */
  serviceEndpoint = this.nshmpHazWs.services.curveServices.hazard;

  /**
   * Returns true if there are comparable models.
   */
  canActivate(): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
    const spinnerRef = this.spinnerService.show('Checking for comparable models');

    return this.hasComparableModels$.pipe(
      map(hasComparableModels => {
        spinnerRef.close();

        if (!hasComparableModels) {
          this.nshmpService.throwError$(new Error(this.errorMessage));

          this.router
            .navigate([''])
            .then(() => this.router.navigate(['dev']))
            .catch((error: Error) => this.nshmpService.throwError$(error));
        }

        return hasComparableModels;
      }),
    );
  }

  /**
   * Check to see if there are any comparable models.
   */
  private get hasComparableModels$(): Observable<boolean> {
    return this.hazardService
      .dynamicNshms$(
        `${this.nshmpHazWs.url}${this.nshmpHazWs.services.nshms}`,
        this.serviceEndpoint,
      )
      .pipe(
        map(dynamicNshms => {
          const hasComparableModels = dynamicNshms.nshmServices.some(nshmService =>
            dynamicNshms.nshmServices.find(
              service =>
                service.project === nshmService.project && service.model !== nshmService.model,
            ),
          );

          return hasComparableModels;
        }),
        catchError(() => {
          return of(false);
        }),
      );
  }
}

results matching ""

    No results matching ""