tylerzika
3/1/2017 - 5:40 PM

concat state

concat state

import { GET_TERRITORY_GEOGRAPHIES, DELETE_TERRITORY_GEOGRAPHY, PATCH_TERRITORY_GEOGRAPHY, POST_TERRITORY_GEOGRAPHY, POST_TERRITORY_GEOGRAPHY_MASS } from '../actions/territory-geographies';

const INITIAL_STATE = { all: [] };

export default function(state = INITIAL_STATE, action) {
  switch(action.type) {
    case GET_TERRITORY_GEOGRAPHIES:
      return { ...state, all: action.payload.data.records};
    case DELETE_TERRITORY_GEOGRAPHY:
      const territoryGeographyId = action.deleteId;
      return {...state, all: state.all.filter(territoryGeography => territoryGeography.Id !== territoryGeographyId)};
    case PATCH_TERRITORY_GEOGRAPHY:
      let updatedRecord = action.updatedRecord;
      return {
        ...state,
          all: state.all.map(geography => geography.Id === action.updatedRecordId ?
              { ...geography, tpslead__Country__c: updatedRecord.tpslead__Country__c,
                              tpslead__State__c: updatedRecord.tpslead__State__c,
                              tpslead__Zip_Start__c: updatedRecord.tpslead__Zip_Start__c,
                              tpslead__Zip_End__c: updatedRecord.tpslead__Zip_End__c
              } :
              geography
              )
            };
    case POST_TERRITORY_GEOGRAPHY:
     var newRecord = JSON.parse(action.payload.config.data);
     newRecord["Id"] = action.payload.data.id;
      return Object.assign({}, state, {
        all: [
          ...state.all,
            {
              Id: newRecord.Id,
              tpslead__Country__c: newRecord.tpslead__Country__c,
              tpslead__State__c: newRecord.tpslead__State__c,
              tpslead__Zip_Start__c: newRecord.tpslead__Zip_Start__c,
              tpslead__Zip_End__c: newRecord.tpslead__Zip_End__c
            }
        ]
      })
    case POST_TERRITORY_GEOGRAPHY_MASS:
    console.log(action.payload);
    let newGeographies = action.payload
    return Object.assign({}, state, {
      all: [
        ...state.all.concat(newGeographies)
      ]
    })

    default:
      return state;
  }
}