import { Injectable , Inject } from '@angular/core' ; import { Observable , of } from 'rxjs' ; import { catchError , switchMap } from 'rxjs/operators' ; import { ToastController } from '@ionic/angular' ; import { Action } from '@ngrx/store' ; import { Effect , Actions , ofType } from '@ngrx/effects' ; import * as Err from '../actions/err.actions' ; import { ToastOptions } from '@ionic/core' ; import { environment } from '@env/environment' ; @ Injectable ( ) export class ErrorsEffects { private _data : ToastOptions ; private _payload : any ; private _env : any = environment ; constructor ( private action$ , @ Inject ( ToastController ) private _toast ) { } @ Effect ( ) handleErrorAction$ : Observable < Action > = this . action$ . pipe ( ofType ( Err . ErrorActions . ERROR_SHARED ) , switchMap ( ( action ) => of ( new Err . ErrorDisplayAction ( action . payload ) ) ) , catchError ( err => of ( new Err . ErrorAction ( err ) ) ) ) ; @ Effect ( ) displayErrorAction$ = this . action$ . pipe ( ofType ( Err . ErrorActions . ERROR_DISPLAY ) , switchMap ( async ( action ) => { const { message = '' } = action . payload || { } ; if ( ! message ) { return action . payload ; } this . _payload = action . payload ; this . _data = { message : message . split ( '}' ) . reverse ( ) [ 0 ] || 'Error...' , position : 'bottom' , duration : 5000 , cssClass : [ 'danger' ] , showCloseButton : true , closeButtonText : 'Close' } ; const alert = await this . _toast . create ( this . _data ) ; return await alert . present ( ) . then ( _ => ( this . _env . envName === 'test' ) ? alert . dismiss ( ) : null ) ; } ) , switchMap ( ( payload ) => ( ! payload ) ? of ( new Err . ErrorDisplaySuccessAction ( this . _payload ) ) : of ( new Err . ErrorAction ( payload ) ) ) , catchError ( err => of ( Err . ErrorActions . ERROR_DISPLAY_FAILED ) ) ) ; }