deliveryStatsActions.js 3.09 KB
'use strict';
import Request from '../../services/Request';
import DeliveryStatsService from '../../services/HomeService';

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,
} = require('../../constants/actionTypes').default;

export function chooseStartDate(startDay) {
    return {
        type: CHOOSE_DELIVERY_START_DATE,
        payload: startDay,
    }
}

export function chooseEndDate(endDay) {
    return {
        type: CHOOSE_DELIVERY_END_DATE,
        payload: endDay,
    }
}

export function newDatePicked(pickedDate, isStartDate) {
    return (dispatch, getState) => {
        if(isStartDate) {
            dispatch(chooseStartDate(pickedDate));
        } else {
            dispatch(chooseEndDate(pickedDate));
        }
        const {deliveryStats, home} = getState();
        let params = {
            page: deliveryStats.currentPage + 1,
            size: 20,
            beginTime: deliveryStats.startDay,
            endTime: deliveryStats.endDay,
            shopId: home.shopId,
        }
        dispatch(requestData(params));
    };

}

export function showDatePickerIOS(isShowDatePickerIOS, isStartDate) {
    return {
        type: SHOW_DELIVERY_DATEPICKER_IOS,
        isShowDatePickerIOS: isShowDatePickerIOS,
        isStartDate: isStartDate,
    }
}

// export function chooseDateIOS(selectedDate, isStartDate) {
//     return {
//         type: SELECT_DELIVERY_DATE_IOS,
//         selectedDate: selectedDate,
//         isStartDate: isStartDate,
//     }
// }

export function selectedDateIOS(selectedDate, isStartDate) {

    // return (dispatch, getState) => {
    //     dispatch(chooseDateIOS(selectedDate, isStartDate));
    //     const {deliveryStats, home} = getState();
    //     let params = {
    //         page: deliveryStats.currentPage + 1,
    //         size: 20,
    //         beginTime: deliveryStats.startDay,
    //         endTime: deliveryStats.endDay,
    //         shopId: home.shopId,
    //     }
    //     dispatch(requestData(params));
    // };
    return {
        type: SELECT_DELIVERY_DATE_IOS,
        selectedDate: selectedDate,
        isStartDate: isStartDate,
    }
}

export function request() {
    return {
        type: DELIVERY_STATS_REQUEST,
    }
}

export function success(json) {
    return {
        type: DELIVERY_STATS_SUCCESS,
        payload: json,
    }
}

export function failure(error) {
    return {
        type: DELIVERY_STATS_FAILURE,
        payload: error,
    }
}

export function requestData(params) {
    return (dispatch, getState) => {

        const {deliveryStats} = getState();

        if (deliveryStats.currentPage >= deliveryStats.pageCount || deliveryStats.isFetching) {//Last page reached....
            return;
        }

        dispatch(request());
        return new DeliveryStatsService()
        .deliveryStatsData(params)
        .then(json => {
            dispatch(success(json));
        })
        .catch(error => {
            dispatch(failure(error));
        })
    };
}