import { Injectable } from '@angular/core'; import { Actions, Effect, ofType } from '@ngrx/effects'; import { Observable } from 'rxjs/Observable'; import { Action } from '@ngrx/store'; import { switchMap } from 'rxjs/operators/switchMap'; import { map } from 'rxjs/operators/map'; import { ProductionEditor, Statistics } from '../models/header.model'; import { catchError } from 'rxjs/operators/catchError'; import { of } from 'rxjs/observable/of'; import { FetchStatistics, FetchStatisticsFailed, StatisticsActionTypes, StatisticsFetched } from '../actions/statistics.actions'; import { StatisticsService } from '../services/statistics.service'; @Injectable() export class StatisticsEffect { @Effect() public statisticsFetching: Observable; constructor(private actions$: Actions, private statisticsService: StatisticsService) { this.initEffects(); } private initEffects() { this.statisticsFetching = this.actions$.pipe( ofType(StatisticsActionTypes.FetchStatistics), map(action => action.payload), switchMap((productionEditors: ProductionEditor[]) => this.statisticsService .getStatistics(productionEditors) .pipe( map((statistics: Statistics) => new StatisticsFetched(statistics)), catchError(error => of(new FetchStatisticsFailed(error))) ) ) ); } }