From 884d1d7190b7239abba9a6067491ad9e878c1107 Mon Sep 17 00:00:00 2001 From: Brandon Clayton <bclayton@usgs.gov> Date: Wed, 24 Jul 2024 16:30:09 -0600 Subject: [PATCH] remove ngrx --- .../dynamic-compare/state/app.effects.ts | 474 ------------------ 1 file changed, 474 deletions(-) delete mode 100644 projects/nshmp-apps/src/app/dev/hazard/dynamic-compare/state/app.effects.ts diff --git a/projects/nshmp-apps/src/app/dev/hazard/dynamic-compare/state/app.effects.ts b/projects/nshmp-apps/src/app/dev/hazard/dynamic-compare/state/app.effects.ts deleted file mode 100644 index 8f4c7f8f6..000000000 --- a/projects/nshmp-apps/src/app/dev/hazard/dynamic-compare/state/app.effects.ts +++ /dev/null @@ -1,474 +0,0 @@ -import {Injectable} from '@angular/core'; -import {ActivatedRoute, Router} from '@angular/router'; -import { - HazardFormControlIds, - HazardService, - hazardUtils, -} from '@ghsc/nshmp-lib-ng/hazard'; -import { - NshmpService, - ServiceCallInfo, - SpinnerService, -} from '@ghsc/nshmp-lib-ng/nshmp'; -import { - HazardCalcResponse, - HazardRequestMetadata, - HazardResponse, -} from '@ghsc/nshmp-utils-ts/libs/nshmp-haz/www/hazard-service'; -import {NshmMetadata} from '@ghsc/nshmp-utils-ts/libs/nshmp-haz/www/nshm-service'; -import {Imt, NehrpSiteClass} from '@ghsc/nshmp-utils-ts/libs/nshmp-lib/gmm'; -import {sourceTypeFromPascalCase} from '@ghsc/nshmp-utils-ts/libs/nshmp-lib/model'; -import {NshmId} from '@ghsc/nshmp-utils-ts/libs/nshmp-lib/nshm'; -import {Actions, createEffect, ofType} from '@ngrx/effects'; -import {concatLatestFrom} from '@ngrx/operators'; -import {Store} from '@ngrx/store'; -import {MarkAsDirtyAction, SetValueAction} from 'ngrx-forms'; -import {environment} from 'projects/nshmp-apps/src/environments/environment'; -import {devApps} from 'projects/nshmp-apps/src/shared/utils/applications.utils'; -import {catchError, exhaustMap, forkJoin, map, mergeMap, tap} from 'rxjs'; - -import {createPlots} from '../utils/response-handler.utils'; -import {appActions} from './app.actions'; -import {dynamicCompareAppFeature} from './app.reducer'; -import { - ControlForm, - controlPanelFormInitialState, - FORM_ID, - ServiceResponses, - Spectra, -} from './app.state'; - -/** - * URL query. - */ -interface Query { - imt: string; - latitude: string; - longitude: string; - maxDirection: string; - model: string; - modelCompare: string; - returnPeriod: string; - siteClass: string; - truncate: string; - vs30: string; -} - -/** - * NGRX effects for dynamic hazard compare application. - */ -@Injectable() -export class DynamicCompareAppEffects { - /** nshmp-haz-ws web config */ - nshmpHazWs = environment.webServices.nshmpHazWs; - /** Hazard endpoint */ - serviceEndpoint = this.nshmpHazWs.services.curveServices.hazard; - - /** - * Create the plot data from the service response. - */ - createPlots$ = createEffect(() => - this.actions$.pipe( - ofType(appActions.serviceResponses), - concatLatestFrom(() => - this.store.select(dynamicCompareAppFeature.selectDynamicCompareAppState) - ), - map(([, state]) => { - const plots = createPlots(state); - this.spinnerService.remove(); - return appActions.plots({plots}); - }), - catchError((error: Error) => this.nshmpService.throwError$(error)) - ) - ); - - /** - * Call dynamic hazard services. - */ - callServices$ = createEffect(() => - this.actions$.pipe( - ofType(appActions.callServices), - concatLatestFrom(() => - this.store.select(dynamicCompareAppFeature.selectDynamicCompareAppState) - ), - exhaustMap(([, state]) => { - this.spinnerService.show(SpinnerService.MESSAGE_SERVICE); - const form = state.controlPanelForm.value; - - const modelUrl = this.serviceCallUrl( - form.model, - form, - state.nshmServices - ); - - const modelCompareUrl = this.serviceCallUrl( - form.modelCompare, - form, - state.nshmServices - ); - - const serviceCallInfo: ServiceCallInfo = { - ...state.serviceCallInfo, - serviceCalls: [modelUrl, modelCompareUrl], - }; - - const calls = [modelUrl, modelCompareUrl].map(url => - this.nshmpService.callService$(url) - ); - - return forkJoin(calls).pipe( - mergeMap((hazardResponses: HazardCalcResponse[]) => { - if (hazardResponses.length !== 2) { - throw new Error('Does not contain two responses'); - } - - const modelA = hazardResponses[0]; - const modelB = hazardResponses[1]; - - const serviceResponses: ServiceResponses = { - modelA: { - hazardResponse: modelA, - model: form.model, - spectra: this.responseSpectra( - modelA.response.hazardCurves, - form - ), - }, - modelB: { - hazardResponse: modelB, - model: form.modelCompare, - spectra: this.responseSpectra( - modelB.response.hazardCurves, - form - ), - }, - }; - - return [ - appActions.serviceResponses({serviceResponses}), - appActions.serviceCallInfo({serviceCallInfo}), - ]; - }), - catchError((error: Error) => this.nshmpService.throwError$(error)) - ); - }), - catchError((error: Error) => this.nshmpService.throwError$(error)) - ) - ); - - /** - * Initialize the app by getting all the usages from the available dynamic services. - */ - init$ = createEffect(() => - this.actions$.pipe( - ofType(appActions.init), - concatLatestFrom(() => - this.store.select(dynamicCompareAppFeature.selectDynamicCompareAppState) - ), - exhaustMap(([, state]) => { - this.spinnerService.show(SpinnerService.MESSAGE_METADATA); - - return this.hazardService - .dynamicNshms$<HazardRequestMetadata>( - `${this.nshmpHazWs.url}${this.nshmpHazWs.services.nshms}`, - this.serviceEndpoint - ) - .pipe( - mergeMap(({models, nshmServices, usageResponses}) => { - this.spinnerService.remove(); - const serviceCallInfo: ServiceCallInfo = { - ...state.serviceCallInfo, - usage: nshmServices.map( - service => `${service.url}${this.serviceEndpoint}` - ), - }; - - return [ - appActions.nshmServices({nshmServices}), - appActions.usageResponses({usageResponses}), - appActions.serviceCallInfo({serviceCallInfo}), - appActions.availableModels({models}), - appActions.initialFormSet(), - ]; - }), - catchError((error: Error) => this.nshmpService.throwError$(error)) - ); - }), - catchError((error: Error) => this.nshmpService.throwError$(error)) - ) - ); - - /** - * Throw error if no comparable models are found. - */ - noComparableModel$ = createEffect( - () => - this.actions$.pipe( - ofType(SetValueAction.TYPE), - concatLatestFrom(() => - this.store.select(dynamicCompareAppFeature.selectControlPanelForm) - ), - tap(([action, {value}]) => { - const setValueAction = action as SetValueAction<unknown>; - - if ( - setValueAction.controlId === - `${FORM_ID}.${HazardFormControlIds.MODEL}` && - value.modelCompare === null - ) { - this.nshmpService.throwError$( - new Error( - 'No available models to compare.<br>Select a different starting model.' - ) - ); - } - }), - catchError((error: Error) => this.nshmpService.throwError$(error)) - ), - { - dispatch: false, - } - ); - - /** - * Check if form is valid from the url query parameters and call service. - */ - initialCallFromUrlQuery$ = createEffect(() => - this.actions$.pipe( - ofType(appActions.initialCallFromQuery), - concatLatestFrom(() => - this.store.select(dynamicCompareAppFeature.selectControlPanelForm) - ), - mergeMap(([, form]) => { - if (form.isValid) { - this.nshmpService.selectPlotControl(); - return [new MarkAsDirtyAction(form.id), appActions.callServices()]; - } else if (form.value !== controlPanelFormInitialState().value) { - return [new MarkAsDirtyAction(form.id)]; - } else { - return []; - } - }), - catchError((error: Error) => this.nshmpService.throwError$(error)) - ) - ); - - /** - * Set the form values. - */ - initialFormSet$ = createEffect(() => - this.actions$.pipe( - ofType(appActions.initialFormSet), - concatLatestFrom(() => - this.store.select(dynamicCompareAppFeature.selectControlPanelForm) - ), - mergeMap(([, form]) => { - const query = this.route.snapshot.queryParams as Query; - const defaultValues = controlPanelFormInitialState().value; - - const formValues: ControlForm = { - commonReturnPeriods: defaultValues.commonReturnPeriods, - imt: this.getImt(query.imt, defaultValues.imt), - latitude: query.latitude - ? Number.parseFloat(query.latitude) - : defaultValues.latitude, - longitude: query.longitude - ? Number.parseFloat(query.longitude) - : defaultValues.longitude, - maxDirection: - query.maxDirection !== undefined - ? (JSON.parse(query.maxDirection) as boolean) - : defaultValues.maxDirection, - model: this.getModel(query.model, defaultValues.model), - modelCompare: this.getModel( - query.modelCompare, - defaultValues.modelCompare - ), - returnPeriod: query.returnPeriod - ? Number.parseInt(query.returnPeriod, 10) - : defaultValues.returnPeriod, - siteClass: this.getSiteClass( - query.siteClass, - defaultValues.siteClass as NehrpSiteClass - ), - sourceType: null, - truncate: - query.truncate !== undefined - ? (JSON.parse(query.truncate) as boolean) - : defaultValues.truncate, - vs30: query.vs30 ? Number.parseFloat(query.vs30) : defaultValues.vs30, - }; - - const actions = hazardUtils.initialFormSetActions(form, formValues); - actions.push( - new SetValueAction( - form.controls.modelCompare.id, - formValues.modelCompare - ) - ); - actions.push(new SetValueAction(form.controls.imt.id, formValues.imt)); - actions.push( - new SetValueAction(form.controls.vs30.id, formValues.vs30) - ); - actions.push(appActions.initialCallFromQuery()); - return actions; - }), - catchError((error: Error) => this.nshmpService.throwError$(error)) - ) - ); - - /** - * Effect to set location form fields. - */ - setLocation$ = createEffect(() => - this.actions$.pipe( - ofType(appActions.setLocation), - concatLatestFrom(() => - this.store.select(dynamicCompareAppFeature.selectControlPanelForm) - ), - exhaustMap(([{location}, form]) => { - return [ - new SetValueAction(form.controls.latitude.id, location.latitude), - new MarkAsDirtyAction(form.controls.latitude.id), - new SetValueAction(form.controls.longitude.id, location.longitude), - new MarkAsDirtyAction(form.controls.longitude.id), - ]; - }), - catchError((error: Error) => this.nshmpService.throwError$(error)) - ) - ); - - /** - * Update the URL query for settings - */ - updateUrl$ = createEffect( - () => - this.actions$.pipe( - ofType(SetValueAction.TYPE), - concatLatestFrom(() => - this.store.select(dynamicCompareAppFeature.selectControlPanelForm) - ), - map(([action, {value}]) => { - if ((action as SetValueAction<unknown>).controlId.includes(FORM_ID)) { - const query: Query = { - imt: value.imt, - latitude: value.latitude?.toString(), - longitude: value.longitude?.toString(), - maxDirection: String(value.maxDirection), - model: value.model, - modelCompare: value.modelCompare, - returnPeriod: value.returnPeriod?.toString(), - siteClass: value.siteClass, - truncate: String(value.truncate), - vs30: value.vs30?.toString(), - }; - - this.router - .navigate([devApps().hazard.dynamicCompare.routerLink], { - queryParams: { - ...query, - }, - }) - .catch((error: Error) => this.nshmpService.throwError$(error)); - } - }), - catchError((error: Error) => this.nshmpService.throwError$(error)) - ), - {dispatch: false} - ); - - constructor( - private actions$: Actions, - private hazardService: HazardService, - private nshmpService: NshmpService, - private route: ActivatedRoute, - private router: Router, - private spinnerService: SpinnerService, - private store: Store - ) {} - - /** - * Returns the IMT to use. - * - * @param query The query value - * @param defaultImt The default IMT - */ - private getImt(query: string, defaultImt: Imt): Imt { - return ( - Object.values(Imt).find(imt => imt.toString() === query) ?? defaultImt - ); - } - - /** - * Returns the model to use. - * - * @param query The query model - * @param defaultModel The default model - */ - private getModel(query: string, defaultModel: NshmId): NshmId { - return ( - Object.values(NshmId).find(model => model.toString() === query) ?? - defaultModel - ); - } - - /** - * Returns the site class to use. - * - * @param query The query value - * @param defaultSiteClass The default site class - */ - private getSiteClass( - query: string, - defaultSiteClass: NehrpSiteClass - ): NehrpSiteClass { - return ( - Object.values(NehrpSiteClass).find( - siteClass => siteClass.toString() === query - ) ?? defaultSiteClass - ); - } - - /** - * Create the response spectra for a specific model and for each source type available. - * - * @param hazardResponses The hazard responses - * @param form The control form values - */ - private responseSpectra( - hazardResponses: HazardResponse[], - form: ControlForm - ): Spectra[] { - const spectra: Spectra[] = []; - const sourceTypes = [...hazardResponses] - .shift() - .data.map(data => data.component); - - sourceTypes.forEach(sourceType => { - form.sourceType = sourceType; - spectra.push({ - responseSpectra: hazardUtils.responseSpectra(hazardResponses, form), - sourceType: sourceTypeFromPascalCase(sourceType), - }); - }); - - return spectra; - } - - /** - * Returns the URL to call for a specific model. - * - * @param model The NSHM to call - * @param formValues The control panel form values - * @param nshmServices The NSHM service metadata - */ - private serviceCallUrl( - model: NshmId, - formValues: ControlForm, - nshmServices: NshmMetadata[] - ): string { - const {latitude, longitude, vs30} = formValues; - const service = nshmServices.find(service => service.model === model); - return `${service.url}${this.serviceEndpoint}/${longitude}/${latitude}/${vs30}`; - } -} -- GitLab