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

import Immutable, {List, Record} from 'immutable';

const {

	SALE_STATS_REQUEST,
	SALE_STATS_SUCCESS,
	SALE_STATS_FAILURE,

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

const initialState = new InitialState;

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

    switch (action.type) {

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

		case SALE_STATS_SUCCESS: {
			let nextState =  state.set('isFetching', false)
				.set('error', null)
				.set('goodsAmount', action.payload.goodsAmount)
				.set('amountRise', action.payload.amountRise)
				.set('amountRisePercent', action.payload.amountRisePercent)
				.set('goodsCount', action.payload.goodsCount)
				.set('countRise', action.payload.countRise)
				.set('countRisePercent', action.payload.countRisePercent)
				.set('trendInSevenDays', Immutable.fromJS(action.payload.trendInSevenDays));
			return nextState;
		}

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

    }

    return state;
}