addCouponActions.js 1.25 KB
'use strict';

import ReactNative from 'react-native';
import AddCouponService from '../../services/AddCouponService';

const {
	ADD_COUPON_REQUEST,
	ADD_COUPON_SUCCESS,
	ADD_COUPON_FAILURE,

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

export function resetReceiveCouponResult() {
	return {
		type: RESETRECEIVECOUPONRES,
	}
}

export function addCouponRequest() {
    return {
        type: ADD_COUPON_REQUEST,
    };
}

export function addCouponSuccess(json) {
    return {
        type: ADD_COUPON_SUCCESS,
        payload: json
    }
}

export function addCouponFailure(error) {
    return {
        type: ADD_COUPON_FAILURE,
        payload: error
    }
}

export function addCoupon(coupon_code) {
	return (dispatch, getState) => {
		let {app, addCoupon} = getState();

		let fetchAddCoupon = (uid) => {
			dispatch(addCouponRequest());
			return new AddCouponService(app.host).addCoupon(uid,coupon_code)
			.then(json => {
				dispatch(addCouponSuccess(json));
				ReactNative.NativeModules.YH_MyAssetsViewHelper.needUpdateCoupon();
			})
			.catch(error => {
				dispatch(addCouponFailure(error));
			});
		}

		ReactNative.NativeModules.YH_CommonHelper.uid()
		.then(uid => {
			fetchAddCoupon(uid);
		})
		.catch(error => {
		});
	};
}