xsnippet logotype

xsnippet

#363723, Untitled [ Text only ]

by Guest
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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<Action>;

  constructor(private actions$: Actions, private statisticsService: StatisticsService) {
    this.initEffects();
  }

  private initEffects() {
    this.statisticsFetching = this.actions$.pipe(
      ofType<FetchStatistics>(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)))
          )
      )
    );
  }
}