homeReducer.js 2.43 KB
/**
 * # guideReducer.js
 *
 * The reducer for all the actions from the various log states
 */
'use strict';
/**
 * ## Imports
 *
 * InitialState
 */
import InitialState from './homeInitialState';
import Immutable, {Map} from 'immutable';

const {

	HOME_BNS_REQUEST,
    HOME_BNS_SUCCESS,
    HOME_BNS_FAILURE,

	HOME_RECOMMENDATION_REQUEST,
    HOME_RECOMMENDATION_SUCCESS,
    HOME_RECOMMENDATION_FAILURE,

	SYNC_USER_REQUEST,
    SYNC_USER_SUCCESS,
    SYNC_USER_FAILURE,

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

const initialState = new InitialState;

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

    switch (action.type) {

        case HOME_RECOMMENDATION_REQUEST: {
			let nextState =  state.set('isFetching', true)
				.set('error', null)
				.set('ptr', action.payload);
			return nextState;
		}

		case HOME_BNS_SUCCESS: {
			let {banner, notice, section} = action.payload;
			let nextState =  state.set('section', Immutable.fromJS(section))
				.setIn(['banner', 'duration'], banner.duration)
				.setIn(['banner', 'list'], Immutable.fromJS(banner.list))
				.setIn(['notice', 'duration'], notice.duration)
				.setIn(['notice', 'open'], notice.open)
				.setIn(['notice', 'list'], Immutable.fromJS(notice.list));
			return nextState;
		}
		case HOME_RECOMMENDATION_SUCCESS: {
			let {lastedTime, list, endReached} = action.payload;
			let nextState =  state.set('isFetching', false)
				.set('error', null)
				.setIn(['recommendation', 'lastedTime'], lastedTime)
				.setIn(['recommendation', 'endReached'], endReached)
				.setIn(['recommendation', 'list'], Immutable.fromJS(list));
			return nextState;
		}

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

		case SYNC_USER_REQUEST: {
			let nextState =  state.setIn(['sync', 'isFetching'], true)
				.setIn(['sync', 'error'], null);
			return nextState;
		}

		case SYNC_USER_SUCCESS: {
			let nextState =  state.setIn(['sync', 'isFetching'], false)
				.setIn(['sync', 'error'], null)
				.setIn(['sync', 'ssouid'], action.payload);
			return nextState;
		}

		case SYNC_USER_FAILURE:
	    	return state.setIn(['sync', 'isFetching'], false)
				.setIn(['sync', 'error'], action.payload);

    }

    return state;
}