All files / src/app/dashboard measure.actions.ts

100% Statements 29/29
100% Branches 0/0
100% Functions 11/11
100% Lines 26/26
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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 681x   1x           1x   1x 1x 1x 1x   6x     6x     1x 1x           1x 1x     1x 2x           1x 1x     1x 2x           1x 1x     1x 2x             1x 1x     1x  
import { Injectable,  } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { NgRedux } from '@angular-redux/store';
 
import { IAppState } from '../store/state/AppState';
import { IMeasure, IMeasureUpdate } from '../model/measure.model';
 
@Injectable()
export class MeasureActions {
 
  static INITIALIZE_MEASURES_REQUEST = 'INITIALIZE_MEASURES_REQUEST';
  static INITIALIZE_MEASURES_SUCCESS = 'INITIALIZE_MEASURES_SUCCESS';
  static INITIALIZE_MEASURES_FAILED = 'INITIALIZE_MEASURES_FAILED';
  static UPDATE_MEASURE = 'UPDATE_MEASURE';
 
  public errorMeasure = (error) => ({ id: 'err', title: `Error loading measures: ${error}`, icon: 'fa-exclamation-triangle' });
 
  constructor(
    private ngRedux: NgRedux<IAppState>,
  ) {}
 
  createInitializeMeasuresRequest() {
    return {
      type: MeasureActions.INITIALIZE_MEASURES_REQUEST,
      uiStartLoading: MeasureActions.INITIALIZE_MEASURES_REQUEST,
      excludeFromLog: true,
    };
  }
  initializeMeasuresRequest() {
    this.ngRedux.dispatch(this.createInitializeMeasuresRequest());
  }
 
  createInitializeMeasuresSuccess(measures: IMeasure[]) {
    return {
      type: MeasureActions.INITIALIZE_MEASURES_SUCCESS,
      uiEndLoading: MeasureActions.INITIALIZE_MEASURES_SUCCESS,
      payload: { measures }
    };
  }
  initializeMeasuresSuccess(measures: IMeasure[]) {
    this.ngRedux.dispatch(this.createInitializeMeasuresSuccess(measures));
  }
 
  createInitializeMeasuresFailed(error) {
    return {
      type: MeasureActions.INITIALIZE_MEASURES_FAILED,
      uiEndLoading: MeasureActions.INITIALIZE_MEASURES_FAILED,
      payload: { measures: [this.errorMeasure(error)] }
    };
  }
  initializeMeasuresFailed(error) {
    this.ngRedux.dispatch(this.createInitializeMeasuresFailed(error));
  }
 
  createUpdateMeasure(measureUpdate: IMeasureUpdate) {
    return {
      type: MeasureActions.UPDATE_MEASURE,
      payload: {
        measureUpdate,
      }
    };
  }
  updateMeasure(measureUpdate: IMeasureUpdate) {
    this.ngRedux.dispatch(this.createUpdateMeasure(measureUpdate));
  }
 
}