import { combineReducers } from 'redux';
import { getSuccessActionType } from '../utils';
import { getErrorReducer, getStatusReducer } from './utils/generators';
import { FETCH_CARD, FETCH_NEXT_CARD } from '../constants/card';
import { UPDATE_STUDENT_CARD_RATE } from '../constants/student-card';
import { CREATE_REVIEW } from '../constants/review';

function data(state = {}, action) {
  switch (action.type) {
    case getSuccessActionType(FETCH_CARD):
    case getSuccessActionType(FETCH_NEXT_CARD):
      return action.payload;

    case getSuccessActionType(CREATE_REVIEW):
      return { ...state, studentCard: action.payload.studentCard };
    case getSuccessActionType(UPDATE_STUDENT_CARD_RATE):
      return { ...state, studentCard: action.payload };
    default:
      return state;
  }
}

export default combineReducers({
  status: getStatusReducer([FETCH_CARD, FETCH_NEXT_CARD]),
  error: getErrorReducer([FETCH_CARD, FETCH_NEXT_CARD]),
  data
});
