All files / src/app/services/config config.actions.ts

100% Statements 37/37
100% Branches 2/2
100% Functions 10/10
100% Lines 33/33
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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 1111x     1x 1x         1x   1x 1x 1x 1x 1x               24x 24x   24x     1x   24x                 24x                                             5x 5x           1x       1x 2x     1x 2x 2x           1x 2x               4x 4x 4x 1x   3x 7x         3x     1x  
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { Action } from 'redux';
import { NgRedux, select, } from '@angular-redux/store';
import { ObjectShapeComparer } from '../../common/object-shape-comparer/object-shape-comparer';
 
import { IAppState } from '../../store/state/AppState';
 
@Injectable()
export class ConfigActions {
 
  static INITIALIZE_CONFIG_REQUEST = 'INITIALIZE_CONFIG_REQUEST';
  static INITIALIZE_CONFIG_SUCCESS = 'INITIALIZE_CONFIG_SUCCESS';
  static INITIALIZE_CONFIG_FAILED  = 'INITIALIZE_CONFIG_FAILED';
  static INITIALIZE_CONFIG_TEMPLATE_ERROR  = 'INITIALIZE_CONFIG_TEMPLATE_ERROR';
  static ACTIONS = [ConfigActions.INITIALIZE_CONFIG_REQUEST,
                    ConfigActions.INITIALIZE_CONFIG_SUCCESS,
                    ConfigActions.INITIALIZE_CONFIG_FAILED];
 
  configTemplate;
  pageConfigTemplate;
 
  constructor(
    private ngRedux: NgRedux<IAppState>,
    private objectShapeComparer: ObjectShapeComparer,
  ) {
    this.initTemplates();
  }
 
  initTemplates() {
 
    this.pageConfigTemplate = {
      pageTitle: '',
      pageDescription: '',
      listTitle: '',
      listWidth: 0,
      badgeUnits: '',
      resultsZoom: ''
    };
 
    this.configTemplate = {
      baseDataUrl: '',
      validationsConfig: {
        filePrefixes: [],
        numDataPointsForSparkline: 0,
        numInitialFilesToDisplay: 0,
        page: this.pageConfigTemplate
      },
      referentialsConfig: {
        filePrefixes: [],
        daysToInitialize: 0,
        daysToDisplay: 0,
        page: this.pageConfigTemplate
      },
      clinicsConfig: {
        filePrefixes: [],
        numDataPointsForSparkline: 0,
        numInitialFilesToDisplay: 0,
        page: this.pageConfigTemplate
      }
    };
  }
 
  createInitializeConfigRequest() {
    return {
      type: ConfigActions.INITIALIZE_CONFIG_REQUEST,
      httpRequest: {
        url: 'data/migration-workbench.config.json',
        successAction: this.createInitializeConfigSuccess,
        failedAction: this.createInitializeConfigFailed,
        validateResponse: (data) => this.checkTemplate(data)
      }
    };
  }
  initializeConfigRequest() {
    this.ngRedux.dispatch( this.createInitializeConfigRequest() );
  }
 
  createInitializeConfigSuccess(data) {
    const payload = data;
    return {
      type: ConfigActions.INITIALIZE_CONFIG_SUCCESS,
      payload
    };
  }
 
  createInitializeConfigFailed(error) {
    return {
      type: ConfigActions.INITIALIZE_CONFIG_FAILED,
      payload: {
        error
      }
    };
  }
 
  checkTemplate(data): boolean {
    const results = this.objectShapeComparer.compare(this.configTemplate, data);
    if (results.length === 0) {
      return true;
    }
    results.forEach(result => {
      this.ngRedux.dispatch({
        type: ConfigActions.INITIALIZE_CONFIG_TEMPLATE_ERROR,
        data: result,
      });
    });
    return false;
  }
 
}