All files / src/app/store/middleware ui.middleware.ts

100% Statements 23/23
100% Branches 6/6
100% Functions 6/6
100% Lines 20/20
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 371x     1x     1x 1x     1x     3x 3x 3x     1x 3x 3x 3x 3x 1x   3x 1x   3x 1x   3x       1x  
import { Injectable } from '@angular/core';
import { Http, Response } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import { NgRedux, select } from '@angular-redux/store';
 
import { IAppState } from '../state/AppState';
import { UiActions } from '../../common/mw.common.module';
import { ToastrService } from '../../common/mw.common.module';
 
@Injectable()
export class UiMiddleware {
 
  constructor(
    private ngRedux: NgRedux<IAppState>,
    private uiActions: UiActions,
    private toastr: ToastrService,
  ) {}
 
  uiMiddlewareFactory() {
    const vm = this;
    return function uiMiddleware(store) {
      return (next) => (action) => {
        if (action.uiStartLoading) {
          vm.uiActions.incrementLoading(action.uiStartLoading);
        }
        if (action.uiEndLoading) {
          vm.uiActions.decrementLoading(action.uiEndLoading);
        }
        if (action.toastr) {
          vm.toastr.info(action.toastr);
        }
        return next(action);
      };
    };
  }
}