stockStatsReducer.js 1.63 KB
'use strict';

import StockStatsInitialState from './stockStatsInitialState'
import Immutable, {List, Record} from 'immutable';

const {
    STOCK_STATS_REQUEST,
    STOCK_STATS_SUCCESS,
    STOCK_STATS_FAILURE,
    STOCK_STATS_CLEAN,
    SWITCH_SHOP,
    LOGOUT,
} = require('../../constants/actionTypes').default;

const initialState = new StockStatsInitialState;

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

    switch (action.type) {
        case STOCK_STATS_REQUEST: {
            let nextState = state.set('isFetching', true).set('error', null);
            return nextState;
        }
        case STOCK_STATS_SUCCESS: {
            const {jsonData} = state;
            let origin = jsonData.toJS();
            let {additionInfo, list} = action.payload;
            let data = [...origin, ...(list || [])];
            let nextState = state.set('isFetching',false)
                .set('sum', additionInfo.storageTotalNum || '0')
                .set('totalAmount', additionInfo.storageTotalMoney || '0.00')
                .set('currentPage', action.payload.page)
                .set('pageCount', action.payload.totalPage)
                .set('jsonData', Immutable.fromJS(data));
            return nextState;
        }
        case STOCK_STATS_FAILURE: {
            let nextState = state.set('isFetching',false)
                .set('error', action.playload);
            return nextState;
        }
        case STOCK_STATS_CLEAN:
        case SWITCH_SHOP:
        case LOGOUT:
            return initialState;
    }

    return state;
}