homeActions.js 3.31 KB
/**
 * # guideActions.js
 *
 * App user guide
 *
 */
'use strict';

import {Actions} from 'react-native-router-flux';
import CONFIG from '../../constants/config';
import HomeService from '../../services/HomeService';

const {

	HOME_OVERVIEW_REQUEST,
    HOME_OVERVIEW_SUCCESS,
    HOME_OVERVIEW_FAILURE,

	HOME_BRABDRANK_REQUEST,
    HOME_BRABDRANK_SUCCESS,
    HOME_BRABDRANK_FAILURE,

    GO_TO_SALE_STATS,
    GO_TO_REFOUND_STATS,
    GO_TO_STOCK_STATS,
    GO_TO_DELIVERY_STATS,
    GO_TO_REQUEST_STATS,
    GO_TO_ACCOUNT_SETTLEMENT,

    SETUP_DEFAULT_SHOP,
	SWITCH_SHOP,

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

export function overviewRequest() {
    return {
        type: HOME_OVERVIEW_REQUEST,
    };
}

export function overviewSuccess(json) {
    return {
        type: HOME_OVERVIEW_SUCCESS,
        payload: json
    };
}

export function overviewFailure(error) {
    return {
        type: HOME_OVERVIEW_FAILURE,
        payload: error
    };
}

export function brandRankRequest() {
    return {
        type: HOME_BRABDRANK_REQUEST,
    };
}

export function brandRankSuccess(json) {
    return {
        type: HOME_BRABDRANK_SUCCESS,
        payload: json
    };
}

export function brandRankFailure(error) {
    return {
        type: HOME_BRABDRANK_FAILURE,
        payload: error
    };
}

export function overview(shopId) {

	return dispatch => {
		dispatch(overviewRequest());
        return new HomeService().overview(shopId)
            .then(json => {
                dispatch(overviewSuccess(json));
            })
            .catch(error => {
                dispatch(overviewFailure(error));
            });
	};
}

export function brandRank(shopId) {

	return dispatch => {
		dispatch(brandRankRequest());
        return new HomeService().brandRank(shopId)
            .then(json => {
                dispatch(brandRankSuccess(json));
            })
            .catch(error => {
                dispatch(brandRankFailure(error));
            });
	};
}

export function goToStatsPage(type) {

    switch (type) {
        case CONFIG.statsKey.saleStats:
            Actions.SaleStats();
            return {
                type: GO_TO_SALE_STATS,
            };
        case CONFIG.statsKey.refoundStats:
            Actions.RefoundStats();
            return {
                type: GO_TO_REFOUND_STATS,
            };
        case CONFIG.statsKey.stockStats:
            Actions.StockStats();
            return {
                type: GO_TO_STOCK_STATS,
            };
        case CONFIG.statsKey.deliveryStats:
            Actions.DeliveryStats();
            return {
                type: GO_TO_DELIVERY_STATS,
            };
        case CONFIG.statsKey.requestStats:
            Actions.RequestStats();
            return {
                type: GO_TO_REQUEST_STATS,
            };
        case CONFIG.statsKey.accountSettlement:
            Actions.AccountSettlement();
            return {
                type: GO_TO_ACCOUNT_SETTLEMENT,
            };
    }
}

export function switchShop() {
	return {
		type: SWITCH_SHOP,
	};
}

export function defaultShop(shopId, shopName) {
	return {
		type: SETUP_DEFAULT_SHOP,
		payload: {
			shopId,
			shopName
		}
	};
}

export function setupDefaultShop(shopId, shopName) {

	Actions.Home();

	return dispatch => {
		dispatch(defaultShop(shopId, shopName));
		dispatch(switchShop());
	};
}