import { Injectable } from '@angular/core'; import { Actions, Effect, ofType } from '@ngrx/effects'; import { BomActionTypes, LoadBomsSuccess, DeleteBomSuccess, DeleteBom, Error, NoOp } from '@arrow/bom/store'; import { UserService2, BomService, DialogService } from '@arrow/bom/core'; import { of } from 'rxjs'; import { mergeMap, map, catchError, switchMap } from 'rxjs/operators'; @Injectable() export class BomEffects { constructor( private actions$: Actions, private us2: UserService2, private bs: BomService, private ds: DialogService ) {} @Effect() loadBoms$ = this.actions$.pipe( ofType(BomActionTypes.LoadBoms), mergeMap(() => this.us2.getBoms().pipe( map((data: any) => new LoadBomsSuccess({ boms: data })), catchError((response: any) => of(new Error({ error: response.error_description })) ) ) ) ); @Effect() deleteBom$ = this.actions$.pipe( ofType(BomActionTypes.DeleteBom), mergeMap((action: DeleteBom) => this.ds .confirm('Delete this BOM?', 'Are you sure? This cannot be undone.', { style: 'error' }) .pipe( switchMap((response: boolean) => response ? this.bs.deleteBom(action.payload.id) : of(null) ), map(response => response ? new DeleteBomSuccess({ id: action.payload.id }) : new NoOp() ), catchError((response: any) => of(new Error({ error: response.error_description })) ) ) ) ); @Effect({ dispatch: false }) bomError$ = this.actions$.pipe( ofType(BomActionTypes.Error), mergeMap((action: Error) => this.ds.alert('Error', action.payload.error || 'Unexpected Error') ) ); }