deliveryStatsReducer.js 2.7 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,
    SHOW_DELIVERY_DATEPICKER_IOS,
    SELECT_DELIVERY_DATE_IOS,

    SWITCH_SHOP,
    LOGOUT,
} = 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 {additionInfo, list} = action.payload;
            let data = [...origin, ...(list || [])];
            let nextState = state.set('isFetching', false)
                .set('sum', additionInfo.allNumber || '0')
                .set('totalAmount', additionInfo.allAmount || '0.00')
                .set('currentPage', action.payload.page)
                .set('pageCount', action.payload.totalPage)
                .set('jsonData', Immutable.fromJS(data));
            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)
                .set('isShowDatePickerIOS', false)
                .set('currentPage', 0)
                .set('pageCount', 1);
            return nextState;
        }
        case CHOOSE_DELIVERY_END_DATE: {
            let nextState = state.set('endDay', action.payload)
                .set('isShowDatePickerIOS', false)
                .set('currentPage', 0)
                .set('pageCount', 1);
            return nextState;
        }
        case SHOW_DELIVERY_DATEPICKER_IOS: {
            let nextState = state.set('isShowDatePickerIOS', action.isShowDatePickerIOS)
                .set('isStartDate', action.isStartDate);
            return nextState;
        }
        case SELECT_DELIVERY_DATE_IOS: {
            let nextState = action.isStartDate ? state.set('startDay', action.selectedDate) : state.set('endDay', action.selectedDate);
            return nextState;
        }

        case SWITCH_SHOP:
        case LOGOUT:
            return initialState;
    }

    return state;
}