import {
  FETCH_DATASETS_BY_DOI_FULFILLED,
  FETCH_DATASETS_BY_DOI_PENDING,
  FETCH_DATASETS_BY_INVESTIGATION_FULFILLED,
  FETCH_DATASETS_BY_INVESTIGATION_PENDING,
  FETCH_DATASETS_BY_INVESTIGATION_REJECTED,
  LOG_OUT,
} from '../constants/actionTypes';

const initialState = { data: [], fetching: false, fetched: false };

const datasets = (state = initialState, action) => {
  switch (action.type) {
    case FETCH_DATASETS_BY_INVESTIGATION_PENDING: {
      state = { ...state, data: [], fetched: false, fetching: true };
      break;
    }
    case FETCH_DATASETS_BY_INVESTIGATION_FULFILLED: {
      state = {
        ...state,
        data: action.payload.data,
        fetched: true,
        fetching: false,
      };
      break;
    }

    case FETCH_DATASETS_BY_INVESTIGATION_REJECTED: {
      state = { ...state, data: [], fetched: false, fetching: false };
      break;
    }

    case FETCH_DATASETS_BY_DOI_PENDING: {
      state = { ...state, data: [], fetched: false, fetching: true };
      break;
    }
    case FETCH_DATASETS_BY_DOI_FULFILLED: {
      state = {
        ...state,
        data: action.payload.data,
        fetched: true,
        fetching: false,
      };
      break;
    }

    case LOG_OUT: {
      state = { ...state, data: [], fetching: false, fetched: false };
      break;
    }

    default:
      break;
  }
  return state;
};

export default datasets;
