purchaseCodeActions.js 2.91 KB
'use strict';

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

const {

	GET_MINE_LIMIT_PURCHASE_LIST_REQUEST,
	GET_MINE_LIMIT_PURCHASE_LIST_SUCCESS,
	GET_MINE_LIMIT_PURCHASE_LIST_FAILURE,

	DELETE_MINE_LIMIT_PURCHASE_REQUEST,
	DELETE_MINE_LIMIT_PURCHASE_SUCCESS,
	DELETE_MINE_LIMIT_PURCHASE_FAILURE,

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

export function MineLimitPurchaseListRequest() {
    return {
        type: GET_MINE_LIMIT_PURCHASE_LIST_REQUEST,
    };
}

export function MineLimitPurchaseListSuccess(json) {
    return {
        type: GET_MINE_LIMIT_PURCHASE_LIST_SUCCESS,
        payload: json
    }
}

export function MineLimitPurchaseListFailure(error) {
    return {
        type: GET_MINE_LIMIT_PURCHASE_LIST_FAILURE,
        payload: error
    }
}

export function getMineLimitPurchaseList() {
	return (dispatch, getState) => {
		let {app, purchaseCode} = getState();

		let fetchMineLimitPurchaseList = (uid) => {
			dispatch(MineLimitPurchaseListRequest());
			return new PurchaseCodeService(app.host).getMineLimitPurchaseList(uid)
			.then(json => {
				dispatch(MineLimitPurchaseListSuccess(json));
			})
			.catch(error => {
				dispatch(MineLimitPurchaseListFailure(error));
			});
		}

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

export function DeleteMinePurchaseCodeLimitCodeRequest() {
    return {
        type: DELETE_MINE_LIMIT_PURCHASE_REQUEST,
    };
}

export function DeleteMinePurchaseCodeLimitCodeSuccess(json) {
    return {
        type: DELETE_MINE_LIMIT_PURCHASE_SUCCESS,
        payload: json
    }
}

export function DeleteMinePurchaseCodeLimitCodeFailure(error) {
    return {
        type: DELETE_MINE_LIMIT_PURCHASE_FAILURE,
        payload: error
    }
}

export function deleteMinePurchaseCodeLimitCode(data) {
	return (dispatch, getState) => {
		let {app, purchaseCode} = getState();
		let limitCode = data.limitCode;
		let fetchDeleteMinePurchaseCodeLimitCode = (uid) => {
			let res = parseMinePurchaseCodeLimitCode(data,purchaseCode?purchaseCode.toJS():'')
			dispatch(DeleteMinePurchaseCodeLimitCodeSuccess(res));
			return new PurchaseCodeService(app.host).deleteMinePurchaseCodeLimitCode(uid,limitCode)
			.then(json => {
			})
			.catch(error => {
			});
		}

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

function parseMinePurchaseCodeLimitCode (data,purchaseCode) {
	let invalidLimitCodeProducts = purchaseCode?purchaseCode.invalidLimitCodeProducts:[];
	let  index = -1;
	for (var i = 0; i < invalidLimitCodeProducts.length; i++) {
		let item = invalidLimitCodeProducts[i];
		if (item.limitCode == data.limitCode) {
			index = i;
		}
	}
	if (index != -1) {
		invalidLimitCodeProducts.splice( index, 1 );
	}
	return invalidLimitCodeProducts;
}