import { actionTypes } from 'constants/action-types';

const initialState = {
  componentsById: {}
};

export default function reducer(state = initialState, { type, payload }) {
  switch (type) {
    case actionTypes.COMPONENTS_BACKUP: {
      payload = Array.isArray(payload)
        ? payload.reduce((all, item) => {
            all[item.id] = item;
            return all;
          }, {})
        : payload;
      return {
        ...state,
        componentsById: {
          ...state.componentsById,
          ...payload
        }
      };
    }
    default:
      return state;
  }
}
