guards/dynamic-hazard-compare.guard.ts
Guard to allow dynamic compare application launch.
Will not be able to launch if there are no models to compare.
Properties |
|
Methods |
Accessors |
canActivate |
canActivate()
|
Defined in guards/dynamic-hazard-compare.guard.ts:34
|
Returns true if there are comparable models.
Returns :
Observable | Promise | boolean | UrlTree
|
errorMessage |
Default value : `No comparable models available. <br>
Application cannot be launch.`
|
Defined in guards/dynamic-hazard-compare.guard.ts:24
|
Error message |
Private hazardService |
Default value : inject(HazardService)
|
Defined in guards/dynamic-hazard-compare.guard.ts:19
|
nshmpHazWs |
Default value : environment.webServices.nshmpHazWs
|
Defined in guards/dynamic-hazard-compare.guard.ts:27
|
nshmp-haz-ws web config |
Private nshmpService |
Default value : inject(NshmpService)
|
Defined in guards/dynamic-hazard-compare.guard.ts:21
|
Private router |
Default value : inject(Router)
|
Defined in guards/dynamic-hazard-compare.guard.ts:18
|
serviceEndpoint |
Default value : this.nshmpHazWs.services.curveServices.hazard
|
Defined in guards/dynamic-hazard-compare.guard.ts:29
|
Hazard endpoint |
Private spinnerService |
Default value : inject(SpinnerService)
|
Defined in guards/dynamic-hazard-compare.guard.ts:20
|
hasComparableModels$ |
gethasComparableModels$()
|
Defined in guards/dynamic-hazard-compare.guard.ts:58
|
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);
}),
);
}
}