couponActions.js 5.06 KB
'use strict';

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

const {
	SET_FLITER,

	UNUSE_COUPON_LIST_REQUEST,
	UNUSE_COUPON_LIST_SUCCESS,
	UNUSE_COUPON_LIST_FAILURE,

	USED_COUPON_LIST_REQUEST,
	USED_COUPON_LIST_SUCCESS,
	USED_COUPON_LIST_FAILURE,

	SHOW_USED_COUPON_DETAIL,
	SHOW_UNUSED_COUPON_DETAIL,

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

export function setFliter(fliter) {
    return {
        type: SET_FLITER,
		payload: fliter,
    };
}

export function resetData() {
    return {
        type: RESET_DATA,
    };
}

export function unuseCouponListRequest() {
    return {
        type: UNUSE_COUPON_LIST_REQUEST,
    };
}

export function unuseCouponListSuccess(json) {
    return {
        type: UNUSE_COUPON_LIST_SUCCESS,
        payload: json
    }
}

export function unuseCouponListFailure(error) {
    return {
        type: UNUSE_COUPON_LIST_FAILURE,
        payload: error
    }
}

export function getUnuseCouponsList() {
	return (dispatch, getState) => {

		let {app, coupon} = getState();
		let couponDic = coupon?coupon.toJS():null;
		let couponLists = couponDic.unuseCoupon;
		let status = 0;
		if (couponLists.isFetching || couponLists.endReached || couponLists.error) {
			return;
		}

		let limit = couponLists.pageSize;
		let page = couponLists.currentPage + 1;

		let fetchCouponListList = (uid) => {
			dispatch(unuseCouponListRequest());
			return new CouponService(app.host).getCouponsLists(uid,status,page,limit)
			.then(json => {
				let stateData = coupon?coupon.toJS():'';
				let payload = parseCouponList(stateData,status,json);
				dispatch(unuseCouponListSuccess(payload.unuseCoupon));
			})
			.catch(error => {
				dispatch(unuseCouponListFailure(error));
			});
		}

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


export function usedCouponListRequest() {
    return {
        type: USED_COUPON_LIST_REQUEST,
    };
}

export function usedCouponListSuccess(json) {
    return {
        type: USED_COUPON_LIST_SUCCESS,
        payload: json
    }
}

export function usedCouponListFailure(error) {
    return {
        type: USED_COUPON_LIST_FAILURE,
        payload: error
    }
}

export function showUsedCouponDetail(list) {
	return {
		type: SHOW_USED_COUPON_DETAIL,
		payload: list
	}
}

export function showUnusedCouponDetail(list) {
	return {
		type: SHOW_UNUSED_COUPON_DETAIL,
		payload: list
	}
}

export function showCouponDetail(fliter,index,isShow) {

	return (dispatch, getState) => {

		let {app, coupon} = getState();
		let couponDic = coupon?coupon.toJS():null;
		let couponLists = couponDic.usedCoupon.couponList;
		if (fliter == 0) {
			couponLists = couponDic.unuseCoupon.couponList;
		}

		let items = couponLists&&couponLists.length>index?couponLists[index]:null;

		if (items) {
			items.showDetail = isShow;
		}

		if (fliter == 0) {
			dispatch(showUnusedCouponDetail(couponLists));
		}else if (fliter == 1) {
			dispatch(showUsedCouponDetail(couponLists));
		}
	};
}

export function getUsedCouponsList() {
	return (dispatch, getState) => {

		let {app, coupon} = getState();
		let couponDic = coupon?coupon.toJS():null;
		let couponLists = couponDic.usedCoupon;
		let status = 1;

		if (couponLists.isFetching || couponLists.endReached || couponLists.error) {
			return;
		}

		let limit = couponLists.pageSize;
		let page = couponLists.currentPage + 1;

		let fetchCouponListList = (uid) => {
			dispatch(usedCouponListRequest());
			return new CouponService(app.host).getCouponsLists(uid,status,page,limit)
			.then(json => {
				let stateData = coupon?coupon.toJS():'';
				let payload = parseCouponList(stateData,status,json);
				dispatch(usedCouponListSuccess(payload.usedCoupon));
			})
			.catch(error => {
				dispatch(usedCouponListFailure(error));
			});
		}

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


function parseCouponList (coupon,status,data) {
	let couponList = data?data.couponList:[];
	let total = data?data.total:0;
	let unuseCoupon = coupon.unuseCoupon;
	let usedCoupon = coupon.usedCoupon;

	if (status == 0) {
		unuseCoupon.currentPage = unuseCoupon.currentPage+1;
		unuseCoupon.total = total;
		let num = unuseCoupon.currentPage * unuseCoupon.pageSize;
		unuseCoupon.endReached = num>=total;

		if (coupon.unuseCoupon.currentPage > 1) {
			let oldList = unuseCoupon.couponList;
			let list = [...oldList, ...data.couponList];
			unuseCoupon.couponList = list;
		}else {
			unuseCoupon.couponList = data.couponList?data.couponList:[];
		}
	}else if (status == 1) {
		usedCoupon.currentPage = usedCoupon.currentPage+1;
		usedCoupon.total = total;
		let num = usedCoupon.currentPage * usedCoupon.pageSize;
		usedCoupon.endReached = num>=total;
		if (coupon.usedCoupon.currentPage > 1) {
			let oldList = usedCoupon.couponList;
			let list = [...oldList, ...data.couponList];
			usedCoupon.couponList = list;
		}else {
			usedCoupon.couponList = data.couponList?data.couponList:[];
		}
	}
	return {
		unuseCoupon,
		usedCoupon,
	}
}