refoundStatisticsReducer.js 2.43 KB
'use strict';

import InitialState from './refoundStatisticsInitialState';

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

const {
    REFOUND_STATS_REQUEST,
    REFOUND_STATS_SUCCESS,
    REFOUND_STATS_FAILURE,

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

const initialState = new InitialState;

export default function refoundStatisticsReducer(state = initialState, action) {
    if (!(state instanceof InitialState)) return initialState.merge(state);

    switch (action.type) {
        case REFOUND_STATS_REQUEST:{
            let nextState =  state.set('isFetching', true)
				.set('error', null)
			return nextState;
        }
            break;
        case REFOUND_STATS_SUCCESS: {
            let {list, additionInfo} = action.payload;
            let sixMonths = [];
            let trendInSixMonth = [];
            if (list) {
                for (var i = 0; i < list.length; i++) {
                    let item = action.payload.list[i];
                    sixMonths.push(moment(item.dateId, 'YYYYMM').format('MM月'));
                    trendInSixMonth.push(item.allNums);
                }
            }

            let amountRate = additionInfo.allAmountRate || 0;
            let countRate = additionInfo.allNumberRate || 0;
            let percentRate = additionInfo.returnRingRate || 0;

            let nextState =  state.set('isFetching', false)
                .set('error', null)
                .set('refoundSum', additionInfo.allAmount || 0)
                .set('sumRatio', Math.abs(amountRate))
                .set('sumRise', amountRate >= 0)
                .set('refoundCount', additionInfo.allNumber || 0)
                .set('countRatio', Math.abs(countRate))
                .set('countRise', countRate >= 0)
                .set('refoundPercent',additionInfo.returnAmountRate || 0)
                .set('percentRatio', Math.abs(percentRate))
                .set('percentRise', percentRate >= 0)
                .set('sixMonths', Immutable.fromJS(sixMonths))
                .set('trendInSixMonth', Immutable.fromJS(trendInSixMonth));
            return nextState;
        }
            break;
        case REFOUND_STATS_FAILURE: {
            return state.set('isFetching', false)
	      		.set('error', action.payload);
        }
            break;
        case SWITCH_SHOP:
        case LOGOUT:
            return initialState;
            break;
    }

    return state;
}