cardDetailActions.js 4.19 KB
'use strict';

import ReactNative from 'react-native';
import {DeviceEventEmitter} from 'react-native';
import InstallmentService from '../../services/InstallmentService';

const {

SET_TIP_MESSAGE,
SET_ERROR,
SET_CARD_ID_NO,

GET_CARD_DETAIL_REQUEST,
GET_CARD_DETAIL_FAILURE,
GET_CARD_DETAIL_SUCCESS,

UNBIND_CARD_SUCCESS,
TOGGLE_CARD_SUCCESS,


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


export function setError(error) {
    return {
        type: SET_ERROR,
        payload: error,
    }
}

export function setCardIdNo(cardIdNo){
     return {
        type: SET_CARD_ID_NO,
        payload: cardIdNo,
    }
}

export function getCardDetailRequest(){
     return {
        type: GET_CARD_DETAIL_REQUEST,
    }
}

export function getCardDetailFailure(error){
     return {
        type: GET_CARD_DETAIL_FAILURE,
        payload: error,
    }
}

export function getCardDetailSuccess(list){
     return {
        type: GET_CARD_DETAIL_SUCCESS,
        payload: list,
    }
}


export function getBankDetail(cardIdNo) { 
    return (dispatch, getState) => {
        let {app} = getState();
        
        let queryBankDetail = (uid) => {
            dispatch(getCardDetailRequest());
            return new InstallmentService(app.host).getBankCardDetail(uid, cardIdNo)
            .then(json => {
                dispatch(getCardDetailSuccess(json));
            })
            .catch(error => {
                dispatch(getCardDetailFailure(error));
                dispatch(setTipMessage(error.message || '暂未获取到数据'));
                dispatch(setError(error));
            });
        };

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

			});
		});
    };
}

export function setTipMessage(message){
     return {
        type: SET_TIP_MESSAGE,
        payload: message,
    }
}


export function unbindBankCard(cardIdNo) { 
    return (dispatch, getState) => {
        let {app} = getState();
        
        let unbindCard = (uid) => {
            return new InstallmentService(app.host).unbindCard(uid, cardIdNo)
            .then(json => {
                dispatch(unbindCardSuccess());
                dispatch(backToMyCardList());


            })
            .catch(error => {
                dispatch(unbindCardSuccess());
                dispatch(backToMyCardList());

                dispatch(setTipMessage(error.message || '暂未获取到数据'));
            });
        };

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

            });
        });
    };
}

export function unbindCardSuccess(){
     return {
        type: UNBIND_CARD_SUCCESS,
        payload: true,
    }
}

export function backToMyCardList() {
    return (dispatch, getState) => {
        ReactNative.NativeModules.YH_CommonHelper.goBack();
        DeviceEventEmitter.emit('BankCardChangeEvent');
    }
}


export function toggleBankCard(cardIdNo) { 
    return (dispatch, getState) => {
        let {app} = getState();
        
        let toggleCard = (uid) => {
            return new InstallmentService(app.host).toggleCard(uid, cardIdNo)
            .then(json => {
                DeviceEventEmitter.emit('BankCardChangeEvent');
                dispatch(toggleCardSuccess());
            })
            .catch(error => {
                dispatch(setTipMessage(error.message || '暂未获取到数据'));
            });
        };

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

            });
        });
    };
}

export function toggleCardSuccess(){
     return {
        type: TOGGLE_CARD_SUCCESS,
        payload: true,
    }
}