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

const {

	HOME_OVERVIEW_REQUEST,
    HOME_OVERVIEW_SUCCESS,
    HOME_OVERVIEW_FAILURE,

	HOME_BRABDRANK_REQUEST,
    HOME_BRABDRANK_SUCCESS,
    HOME_BRABDRANK_FAILURE,

    GO_TO_SALE_STATS,
    GO_TO_REFOUND_STATS,
    GO_TO_STOCK_STATS,
    GO_TO_DELIVERY_STATS,
    GO_TO_REQUEST_STATS,
    GO_TO_ACCOUNT_SETTLEMENT,

    SETUP_DEFAULT_SHOP,
	SWITCH_SHOP,
	LOGOUT,

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

const initialState = new InitialState;

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

    switch (action.type) {

		case SETUP_DEFAULT_SHOP: {
			let nextState =  state.set('shopId', action.payload.shopId)
				.set('shopName', action.payload.shopName);
			return nextState;
		}

		case SWITCH_SHOP:
			let nextState =  initialState.set('shopId', state.shopId)
				.set('shopName', state.shopName);
			return nextState;

        case HOME_OVERVIEW_REQUEST:
		case HOME_BRABDRANK_REQUEST: {
			let nextState =  state.set('isFetching', true)
				.set('error', null);
			return nextState;
		}

		case HOME_OVERVIEW_SUCCESS: {
			let nextState =  state.set('isFetching', false)
				.set('error', null)
				.setIn(['overview', 'goodsCount'], action.payload.buyNumbers || 0)
				.setIn(['overview', 'goodsAmount'], action.payload.orderAmount || 0);
			return nextState;
		}

        case HOME_BRABDRANK_SUCCESS: {
			let rankChange = action.payload.rankChange || 0;
			let rise = Math.abs(rankChange) == rankChange ? true : false;
			let rankHasTrend = action.payload.rankLevelFlag;
			let nextState =  state.set('isFetching', false)
				.set('error', null)
				.setIn(['overview', 'rank'], action.payload.rankNow || 0)
				.setIn(['overview', 'rankHasTrend'], rankHasTrend)
				.setIn(['overview', 'rise'], rise)
				.setIn(['overview', 'riseCount'], Math.abs(rankChange))
			return nextState;
		}

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

      	case GO_TO_SALE_STATS:
        case GO_TO_REFOUND_STATS:
        case GO_TO_STOCK_STATS:
        case GO_TO_DELIVERY_STATS:
        case GO_TO_REQUEST_STATS:
        case GO_TO_ACCOUNT_SETTLEMENT:
        	return state;

		case LOGOUT:
			return initialState;
    }

    return state;
}