EnterPasswordSmartOTP.js 4.04 KB
Newer Older
1 2
import React, {useState, useEffect} from 'react';
import EnterPasswordSmartOTPView from './EnterPasswordSmartOTPView';
Giang Tran committed
3
import {getOTPApiSmartOTP, verifyOTPApiSmart} from '../../apis/Functions/users';
Nguyễn Thị Thúy committed
4 5 6 7 8
import {showAlert, TYPE} from '../../components/DropdownAlert';
import I18n from '../../helper/i18/i18n';
import {connect} from 'react-redux';
import {hideLoading, showLoading} from '../../actions/loadingAction';
import {OTP_TYPE} from '../../Config/constants';
9
import {useNavigation} from '@react-navigation/native';
10
import {RESET_SMART_OTP, CHANGESMARTOTP} from '../../routers/ScreenNames';
11 12

const EnterPasswordSmartOTP = (props) => {
13
    const navigation = useNavigation();
Nguyễn Thị Thúy committed
14 15 16 17
    const [firstNumber, setFirstNumber] = useState(null);
    const [secondsNumber, setSecondsNumber] = useState(null);
    const [thirdNumber, setThirdNumber] = useState(null);
    const [fourNumber, setFourNumber] = useState(null);
18
    useEffect(() => {
Nguyễn Thị Thúy committed
19 20
        if (fourNumber) {
            onCheckPINSmartOTPPin();
21
        }
Nguyễn Thị Thúy committed
22
    }, [fourNumber]);
23
    const onNumberPress = (number) => {
Nguyễn Thị Thúy committed
24 25 26 27 28 29 30 31
        if (!firstNumber) {
            setFirstNumber(number.toString());
        } else if (!secondsNumber) {
            setSecondsNumber(number.toString());
        } else if (!thirdNumber) {
            setThirdNumber(number.toString());
        } else if (!fourNumber) {
            setFourNumber(number.toString());
32
        }
Nguyễn Thị Thúy committed
33
    };
34

Nguyễn Thị Thúy committed
35 36 37
    const onCheckPINSmartOTPPin = async () => {
        let pinCode = `${firstNumber}${secondsNumber}${thirdNumber}${fourNumber}`;
        props.showLoading();
38 39 40 41 42 43 44 45
        if (props.route.params.type == 'CHANGE_SMART_OTP') {
            const res = await verifyOTPApiSmart({
                platform: Platform.OS,
                otp_password: pinCode,
                type: 'VERIFY_OTP_PASSWORD',
            });
            if (res.data.code == 200) {
                navigation.navigate(CHANGESMARTOTP, {old_password: pinCode});
46
                clearPIN()
47 48
            } else {
                showAlert(TYPE.ERROR, I18n.t('Notification'), res.data.message);
49
                clearPIN()
50
            }
Nguyễn Thị Thúy committed
51
        } else {
52 53 54 55 56 57 58 59 60
            const res = await getOTPApiSmartOTP({
                platform: Platform.OS,
                otp_by: props.user.email,
                otp_password: pinCode,
                type:
                    props.route.params.type == 'DEPOSIT'
                        ? OTP_TYPE.CUSTOMER_REQUEST_DEPOSIT
                        : OTP_TYPE.REQUEST_WITHDRAW,
            });
61

62 63 64 65
            if (res.data.code == 200) {
                props.route.params.onGoToSmartOTP(pinCode, res.data.data.otp);
            } else {
                showAlert(TYPE.ERROR, I18n.t('Notification'), res.data.message);
66
                clearPIN()
67
            }
Nguyễn Thị Thúy committed
68
        }
69
        props.hideLoading();
Nguyễn Thị Thúy committed
70
    };
71 72 73 74 75 76
    const clearPIN = () => {
        setFirstNumber(null);
        setSecondsNumber(null);
        setThirdNumber(null);
        setFourNumber(null);
    }
77
    const onDeletePress = () => {
Nguyễn Thị Thúy committed
78 79 80 81 82 83 84 85
        if (fourNumber) {
            setFourNumber(null);
        } else if (thirdNumber) {
            setThirdNumber(null);
        } else if (secondsNumber) {
            setSecondsNumber(null);
        } else if (firstNumber) {
            setFirstNumber(null);
86
        }
Nguyễn Thị Thúy committed
87
    };
88
    const onReactivationSmartOTP = () => {
89
        navigation.navigate(RESET_SMART_OTP, {title: 'ReactivationSmartOTP'});
Nguyễn Thị Thúy committed
90
    };
91 92

    const onForgotPINCode = () => {
93
        navigation.navigate(RESET_SMART_OTP, {title: 'ForgotSmartOTP'});
Nguyễn Thị Thúy committed
94
    };
95 96 97 98 99 100 101 102 103 104 105 106
    return (
        <EnterPasswordSmartOTPView
            firstNumber={firstNumber}
            secondsNumber={secondsNumber}
            thirdNumber={thirdNumber}
            fourNumber={fourNumber}
            onNumberPress={onNumberPress}
            onDeletePress={onDeletePress}
            onForgotPINCode={onForgotPINCode}
            onReactivationSmartOTP={onReactivationSmartOTP}
        />
    );
107
};
Nguyễn Thị Thúy committed
108 109 110 111 112 113
const mapStateToProps = (state) => {
    return {
        user: state.userReducer,
    };
};

Giang Tran committed
114
export default connect(mapStateToProps, {showLoading, hideLoading})(
115
    EnterPasswordSmartOTP,
Giang Tran committed
116
);