vipLevelActions.js 2.63 KB
'use strict';

import ReactNative from 'react-native';
import {Platform} from 'react-native';
import VIPLevelService from '../../services/VIPLevelService';
import {Record, List, Map} from 'immutable';

const {
	SET_NICKNAME,

	FETCH_VIP_INFO_REQUEST,
	FETCH_VIP_INFO_SUCCESS,
	FETCH_VIP_INFO_FAILURE,

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

export function fetchVIPInfoRequest() {
	return{
		type: FETCH_VIP_INFO_REQUEST,
	}
}

export function fetchVIPInfoSuccess(object) {
	return{
		type: FETCH_VIP_INFO_SUCCESS,
		payload: object,
	}
}

export function fetchVIPInfoFailure(error) {
	return{
		type: FETCH_VIP_INFO_FAILURE,
		payload: error,
	}
}

export function setNickname(nickname) {
	return{
		type: SET_NICKNAME,
		payload: nickname,
	}
}

export function fetchVIPInfo() {
	return(dispatch, getState) => {
		let {app, vipLevel} = getState();
		let fetchVIP = (uid) => {
			dispatch(fetchVIPInfoRequest());
			return new VIPLevelService(app.host).getVIPInfo(uid)
			.then(json => {
				let vipShowPageInfo = processVIPInfo(json);
				dispatch(fetchVIPInfoSuccess({
					vipInfo: json,
					upgradeNeedCost: vipShowPageInfo.upgradeNeedCost,
					vipNextLevelProgress: vipShowPageInfo.vipNextLevelProgress,
				}));
			})
			.catch(error => {
				dispatch(fetchVIPInfoFailure(error));
			});
		}
		ReactNative.NativeModules.YH_CommonHelper.uid()
		.then(uid => {
			fetchVIP(uid);
		})
		.catch(error => {
			fetchVIP(0);
		});
	}
}

function processVIPInfo(json) {
	let upgradeNeedCost = 0.0;
	let nextNeedCost = 0.0;
	if (!json.upgrade_need_cost || json.upgrade_need_cost =='') {
		upgradeNeedCost = 0.0;
	} else {
		upgradeNeedCost = parseFloat(json.upgrade_need_cost);
	}
	if (!json.next_need_cost || json.next_need_cost =='') {
		nextNeedCost = 0.0;
	} else {
		nextNeedCost = parseFloat(json.next_need_cost);
	}
	let currentLevelCost = 0.0;
	if (nextNeedCost - upgradeNeedCost >= 0) {
		currentLevelCost = nextNeedCost - upgradeNeedCost;
	}
	let vipNextLevelProgress = 1.0;
	if (nextNeedCost == 0.0) {
		vipNextLevelProgress = 1.0;
	}else{
		vipNextLevelProgress = currentLevelCost/nextNeedCost;
	}
	return({upgradeNeedCost,vipNextLevelProgress})
}

export function onPressPrivilegeCell(cellInfo,rowID) {
	return(dispatch, getState) => {
		let {app, vipLevel} = getState();
		let {vipInfo} =vipLevel;
		ReactNative.NativeModules.YH_PersonalInfoHelper.gotoVIPPrivilegeDetailPage({'id':cellInfo.get('id'),'current_vip_level':vipInfo.get('current_vip_level'),'title':cellInfo.get('title'),'rowID':rowID});

	}
}

export function onPressAllVIPPrivilegeCell() {
	return(dispatch, getState) => {
		ReactNative.NativeModules.YH_PersonalInfoHelper.gotoAllVIPPrivilegePage();

	}
}