

import {Reducer} from 'redux';

import {ConfigActionTypes, IConfigState} from './types';

export const initialConfigState: IConfigState = {
  data: {
    configuration: [],
    whitelist: []
  },
  errors: undefined,
  loading: false
};

export const configReducer: Reducer<IConfigState> = (state = initialConfigState, action) => {
  switch (action.type) {
    case ConfigActionTypes.FETCH_REQUEST:
      return {...state, loading: true};
    case ConfigActionTypes.FETCH_SUCCESS:
      return {...state, loading: false, data: action.payload.data, response: action.payload, errors: undefined};
    case ConfigActionTypes.FETCH_ERROR:
      return {...state, loading: false, errors: action.payload};
    default:
      return state;
  }
};
