import { Injectable , Inject , } from '@angular/core' ; import { Actions , Effect , ofType , } from '@ngrx/effects' ; import { of , Observable , } from 'rxjs' ; import { switchMap , map , catchError , } from 'rxjs/operators' ; import { DaffError } from '@daffodil/core' ; import { ErrorTransformer } from '@daffodil/core/state' ; import { DaffProduct , DAFF_PRODUCT_ERROR_MATCHER , } from '@daffodil/product' ; import { DaffProductDriver , DaffProductServiceInterface , } from '@daffodil/product/driver' ; import { DaffProductPageActionTypes , DaffProductPageLoad , DaffProductPageLoadFailure , DaffProductPageLoadSuccess , DaffProductPageLoadByUrl , } from '../actions/product-page.actions' ; param action$ param driver @ Injectable ( ) export class DaffProductPageEffects < T extends DaffProduct > { constructor ( private actions$ , @ Inject ( DaffProductDriver ) private driver , @ Inject ( DAFF_PRODUCT_ERROR_MATCHER ) private errorMatcher ) { } returns @ Effect ( ) load$ : Observable < any > = this . actions$ . pipe ( ofType ( DaffProductPageActionTypes . ProductPageLoadAction ) , switchMap ( ( action ) => this . driver . get ( action . payload ) . pipe ( map ( ( resp ) => new DaffProductPageLoadSuccess ( resp ) ) , catchError ( ( error ) => of ( new DaffProductPageLoadFailure ( this . errorMatcher ( error ) ) ) ) ) ) ) ; @ Effect ( ) loadByUrl$ : Observable < any > = this . actions$ . pipe ( ofType ( DaffProductPageActionTypes . ProductPageLoadByUrlAction ) , switchMap ( ( action ) => this . driver . getByUrl ( action . payload ) . pipe ( map ( ( resp ) => new DaffProductPageLoadSuccess ( resp ) ) , catchError ( ( error ) => of ( new DaffProductPageLoadFailure ( this . errorMatcher ( error ) ) ) ) ) ) ) ; }