import { TranslationService } from '@alfresco/adf-core' ; import { Injectable } from '@angular/core' ; import { MatSnackBar } from '@angular/material/snack-bar' ; import { Actions , Effect , ofType } from '@ngrx/effects' ; import { Store } from '@ngrx/store' ; import { map } from 'rxjs/operators' ; import { AppStore } from '../states/app.state' ; import { SnackbarInfoAction , SnackbarActionTypes , SnackbarWarningAction , SnackbarErrorAction , SnackbarAction } from '../actions/snackbar.actions' ; @ Injectable ( ) export class SnackbarEffects { constructor ( private store , private actions$ , private snackBar , private translationService ) { } @ Effect ( { dispatch : false } ) infoEffect = this . actions$ . pipe ( ofType < SnackbarInfoAction > ( SnackbarActionTypes . Info ) , map ( ( action ) => { this . showSnackBar ( action , 'adf-info-snackbar' ) ; } ) ) ; @ Effect ( { dispatch : false } ) warningEffect = this . actions$ . pipe ( ofType < SnackbarWarningAction > ( SnackbarActionTypes . Warning ) , map ( ( action ) => { this . showSnackBar ( action , 'adf-warning-snackbar' ) ; } ) ) ; @ Effect ( { dispatch : false } ) errorEffect = this . actions$ . pipe ( ofType < SnackbarErrorAction > ( SnackbarActionTypes . Error ) , map ( ( action ) => { this . showSnackBar ( action , 'adf-error-snackbar' ) ; } ) ) ; private showSnackBar ( action , panelClass ) { const message = this . translate ( action . payload , action . params ) ; let actionName = null ; if ( action . userAction ) { actionName = this . translate ( action . userAction . title ) ; } const snackBarRef = this . snackBar . open ( message , actionName , { duration : action . duration || 4000 , panelClass : panelClass } ) ; if ( action . userAction ) { snackBarRef . onAction ( ) . subscribe ( ( ) => { this . store . dispatch ( action . userAction . action ) ; } ) ; } } private translate ( message , params ? ) { return this . translationService . instant ( message , params ) ; } }