deliveryStatsReducer.js 1.79 KB
'use strict';

import DeliveryStatsInitialState from './deliveryStatsInitialState'
import Immutable, {List, Record} from 'immutable';

const {
    DELIVERY_STATS_REQUEST,
    DELIVERY_STATS_SUCCESS,
    DELIVERY_STATS_FAILURE,
    CHOOSE_DELIVERY_START_DATE,
    CHOOSE_DELIVERY_END_DATE,
} = require('../../constants/actionTypes').default;

const initialState = new DeliveryStatsInitialState;

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

    switch (action.type) {
        case DELIVERY_STATS_REQUEST: {
            let nextState = state.set('isFetching', true).set('error', null);
            return nextState;
        }
        case DELIVERY_STATS_SUCCESS: {
            // const {jsonData} = state;
            // let origin = jsonData.toJS();
            let data = [...action.payload.list];
            // let newPageCount = action.payload.list ? state.pageCount + 1 : state.pageCount;
            let nextState = state.set('isFetching',false)
                .set('sum', action.payload.sum)
                .set('totalAmount', action.payload.totalAmount)
                .set('jsonData', List(data));
                // .set('pageCount', newPageCount)
            return nextState;
        }
        case DELIVERY_STATS_FAILURE: {
            let nextState = state.set('isFetching', false)
                .set('error', action.playload);
            return nextState;
        }
        case CHOOSE_DELIVERY_START_DATE: {
            let nextState = state.set('startDay', action.payload);
            return nextState;
        }
        case CHOOSE_DELIVERY_END_DATE: {
            let nextState = state.set('endDay', action.payload);
            return nextState;
        }
    }

    return state;
}