/*
 *
 * Create reducer
 *
 */

import { fromJS } from 'immutable';
import * as types from './constants';

const initialState = fromJS({
  createTemplateInProgress: false,
  response: {},
});

function createReducer(state = initialState, action) {
  switch (action.type) {
    case types.DEFAULT_ACTION:
      return state;
    case types.CREATE_TEMPLATE_REQUEST:
      return state
        .set('createTemplateInProgress', true)
        .set('createTemplateError', false)
        .set('createTemplateErrorMessage', fromJS(""));
    case types.CREATE_TEMPLATE_SUCCESS:
      return state.set('createTemplateInProgress', false)
        .set('response', action.data)
        .set('createTemplateError', (action.statusCode !== undefined && action.statusCode > 300))
        .set('createTemplateErrorMessage', fromJS(action.errorMsg));
    case types.CREATE_TEMPLATE_FAILURE:
      return state
        .set('createTemplateInProgress', false)
        .set('createTemplateError', true)
        .set('createTemplateErrorMessage', fromJS(action.errorMsg));
    case types.CLEAR_CREATE_RESPONSE_REQUEST:
      return state.set('response', {});
    default:
      return state;
  }
}

export default createReducer;
