import {
  DATASET_LIST_RECEIVED,
  DATASET_DETAIL_RECEIVED,
  DATASET_LAYER_RECEIVED,
  TOGGLE_LAYER_STATUS,
  SET_LAYER_STATUS,
  DATASET_LAYER_FETCH_ERROR
} from '../constants';

const initialState = {
  list: [],
  details: {},
  layers: {}
};

export default function (state = initialState, action) {
  switch (action.type) {
    case DATASET_LIST_RECEIVED: {
      return Object.assign({}, state, { list: action.payload.data });
    }
    case DATASET_DETAIL_RECEIVED: {
      const obj = {};
      obj[action.payload.data.dataset_id] = action.payload.data;
      return Object.assign({}, state, { details: obj });
    }
    case DATASET_LAYER_RECEIVED: {
      const obj = {};
      obj[action.payload.data.id] = action.payload.data;
      return Object.assign({}, state, { layers: obj });
    }
    case DATASET_LAYER_FETCH_ERROR: {
      const list = state.list.slice(0);
      for (let i = 0, length = list.length; i < length; i++) {
        if (list[i].id === action.payload.id) {
          list[i].active = false;
          break;
        }
      }
      return Object.assign({}, state, { list });
    }
    case TOGGLE_LAYER_STATUS: {
      const list = state.list.slice(0);
      for (let i = 0, length = list.length; i < length; i++) {
        if (list[i].id === action.payload) {
          list[i].active = !list[i].active;
          break;
        }
      }
      return Object.assign({}, state, { list });
    }
    case SET_LAYER_STATUS: {
      const list = state.list.slice(0);
      for (let i = 0, length = list.length; i < length; i++) {
        if (list[i].id === action.payload.id) {
          list[i].active = action.payload.status;
          break;
        }
      }
      return Object.assign({}, state, { list });
    }
    default:
      return state;
  }
}

@@ -482,32 +482,70 @@
   const 
-obj =
+details = Object.assign(%7B%7D, state.details,
  %7B%7D
+)
 ;%0A      
 obj%5Bacti
@@ -528,35 +528,39 @@
 ils, %7B%7D);%0A      
-obj
+details
 %5Baction.payload.
@@ -646,21 +646,16 @@
  details
-: obj
  %7D);%0A   
@@ -708,24 +708,60 @@
 nst 
-obj =
+layers = Object.assign(%7B%7D, state.layers,
  %7B%7D
+)
 ;%0A      
 obj%5B
@@ -756,19 +756,22 @@
 ;%0A      
-obj
+layers
 %5Baction.
@@ -860,13 +860,8 @@
 yers
-: obj
  %7D);

Fix layers reducer