All files / src/app/store/reducers measure.reducer.ts

100% Statements 22/22
75% Branches 6/8
100% Functions 4/4
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 37 38 39 40 41      1x     1x   1x   4x 4x     2x   1x   1x         2x 2x       1x 1x 1x 1x 1x 1x 1x 1x 1x   1x    
import { Action, Reducer } from 'redux';
import { MeasureState } from '../state/measure.state';
import { IMeasure } from '../../model/measure.model';
import { MeasureActions } from '../../dashboard/measure.actions';
import { GenericReducerFactory } from './generic.reducer';
 
const initialState: IMeasure[] = [];
 
const errorMeasures = [{ 'id': 'err', 'title': 'Error loading measures', 'icon': 'fa-exclamation-triangle', 'link': '' }];
 
export const measureReducer: Reducer<IMeasure[]> = (Istate: IMeasure[] = initialState, action: Action) => {
  switch (action.type) {
    case MeasureActions.INITIALIZE_MEASURES_SUCCESS:
    case MeasureActions.INITIALIZE_MEASURES_FAILED:
      return initializeMeasures(state, action);
    case MeasureActions.UPDATE_MEASURE:
      return updateMeasure(state, action);
    default:
      return state;
  }
};
 
function initializeMeasures(state, action): IMeasure[] {
  action.type = '[MEASURES] ' + action.type;
  return action.payload.measures;
}
 
function updateMeasure(state, action): IMeasure[] {
  const newState = [...state];
  const index = newState.findIndex(m => m.id === action.payload.measureUpdate.id );
  Eif (index > -1) {
    const newMeasure = {...newState[index]};
    newMeasure.metric = action.payload.measureUpdate.metric;
    newMeasure.color = action.payload.measureUpdate.color;
    newMeasure.history = action.payload.measureUpdate.history;
    newState[index] = newMeasure;
    action.type = '[MEASURES] ' + action.type + ' / ' + newMeasure.id;
  }
  return newState;
}