sectionReducer.js 1.53 KB
/**
 * # guideReducer.js
 *
 * The reducer for all the actions from the various log states
 */
'use strict';
/**
 * ## Imports
 *
 * InitialState
 */
import InitialState from './sectionInitialState';

const {

	HOME_BANNER_REQUEST,
    HOME_BANNER_SUCCESS,
    HOME_BANNER_FAILURE,

    HOME_NOTICE_REQUEST,
    HOME_NOTICE_SUCCESS,
    HOME_NOTICE_FAILURE,

    HOME_SECTION_REQUEST,
    HOME_SECTION_SUCCESS,
    HOME_SECTION_FAILURE,

	HOME_RECOMMENDATION_REQUEST,
    HOME_RECOMMENDATION_SUCCESS,
    HOME_RECOMMENDATION_FAILURE,

} = require('../../constants/actionTypes').default;

const initialState = new InitialState;

/**
 * ## guideReducer function
 * @param {Object} state - initialState
 * @param {Object} action - type and payload
 */
export default function sectionReducer(state = initialState, action) {
    if (!(state instanceof InitialState)) return initialState.merge(state);

    switch (action.type) {

		case HOME_BANNER_REQUEST:
		case HOME_NOTICE_REQUEST:
		case HOME_SECTION_REQUEST:
        case HOME_RECOMMENDATION_REQUEST: {
			let nextState =  state.set('isFetching', true)
				.set('error', null);
			return nextState;
		}

		case HOME_RECOMMENDATION_SUCCESS: {
			let nextState =  state.set('isFetching', false)
				.set('error', null)
				.set('recommendation', action.payload);
			return nextState;
		}

		case HOME_BANNER_FAILURE:
		case HOME_NOTICE_FAILURE:
		case HOME_SECTION_FAILURE:
		case HOME_RECOMMENDATION_FAILURE:
	    	return state.set('isFetching', false)
	      		.set('error', action.payload);

    }

    return state;
}