import * as React from 'react'; import { View, Text, TouchableOpacity, ScrollView, Alert, Dimensions } from 'react-native'; import { connect } from "react-redux"; import { saveUserInfo } from "../../redux/actions/AppUserInfoActions"; import Crypto from '../../components/crypto.js'; import Elements from '../../components/elements.js'; import NetInfo from "../../components/netstatus"; import CustomOTPInput from '../../components/otpinput'; import REQUEST from '../../components/api/'; import Theme from '../../components/theme.style.js'; import DB from '../../components/storage/'; import CustomSafeArea from '../../components/safeArea.component'; class Setmpin extends React.Component { constructor(props) { super(props) } state = { loading: false, mpin: "", confirmMpin: "", valid: "" } componentDidMount() { DB.set('set_mpin', 'true', success => {}, error => {}) console.log(this.props) } componentWillUnmount() { } checkValidMpin = () => { if(this.state.mpin.length < 4 || this.state.mpin == null) return false return this.state.mpin == this.state.confirmMpin } setAppMpin = () => { if(this.checkValidMpin()) { NetInfo.netstatus(async isConnected => { if(isConnected) { this.setState({ loading: true }) this.requestSetMpin(success => { DB.set('set_mpin', 'false', onSuccess => {}, onError => {}) this.getUserInfo(onSuccess => { this.setState({ loading: false }) if(this.props.route.params == undefined) { this.props.navigation.reset({ index: 0, routes: [{name: 'Mpin'}], }) } else { if( this.props.route.params.type != undefined && this.props.route.params.type == "addcard" ) { this.props.navigation.reset({ index: 0, routes: [{name: 'Mpin'}], }) } else { this.props.navigation.navigate("OnBoarding", { token: this.props.route.params?.token }) } } }, onError => { this.setState({ loading: false }) }) }, error => { Platform.OS == 'ios' ? setTimeout(() => { Alert.alert("Mpin.", "Failed to set mpin. Please try again.") }, 300) : Alert.alert("Mpin.", "Failed to set mpin. Please try again.") }) } else { this.setState({ loading: false }) Elements.nointernet2() } }) } else { Platform.OS == 'ios' ? setTimeout(() => { Alert.alert("Mpin.", "Please ensure that your MPIN is matched") }, 300) : Alert.alert("Mpin.", "Please ensure that your MPIN is matched") } } requestSetMpin = async (successCallback, errorCallback) => { const SESSION = await DB.session() const { mpin } = this.state const lcard_uuid = this.props.route.params?.sessiondata?.lcard_uuid || SESSION.user.lcard_uuid this.getCurrentUserInfo(async (onSuccess) => { let hash = onSuccess.data.customer_number.substring(0, 8) let ciphermpin = Crypto.encrypt(mpin, hash) const params = { lcard_uuid: lcard_uuid, mpin: ciphermpin } await REQUEST("setup_mpin", "post", {}, {}, params, function(data){ console.log(data) successCallback(data) }, function(error){ console.log(error) errorCallback(error) }) }, (onError) => { errorCallback(onError) }) } getCurrentUserInfo = async (successCallback, errorCallback) => { const SESSION = await DB.session() this.props.saveUserInfo({ token: SESSION.token, card_number: SESSION.user.card_number }).then(data => { if(data.status == 1) { successCallback(data) } else { errorCallback({}) } }) .catch(error => errorCallback(error)) } getUserInfo = async (successCallback, errorCallback) => { const SESSION = await DB.session() this.props.saveUserInfo({ token: SESSION.token, card_number: SESSION.user.card_number }).then(data => { if(data.status == 1) { DB.updateProfile(data, res => { this.setState({ loading: false }) successCallback(data) console.log("USER PROFILE SAVED ON ON BOARDING! ") }, error => { this.setState({ loading: false }) errorCallback(error) console.log("Error saving profile", error) }) } }) .catch(error => errorCallback(error)) } render() { return( Set your mobile PIN To keep your app secure, you will be asked for an MPIN to open your app. Enter 4-digit MPIN { this.setState({ mpin: v, valid: v.length == 4 ? true : false }) }}/> Confirm 4-digit MPIN { this.setState({ confirmMpin: v, valid: v.length == 4 ? true : false }) }}/> {(this.state.confirmMpin.length > 0 && !this.checkValidMpin()) && ( Please ensure that your MPIN is matched )} this.setAppMpin()}> Submit ) } } const mapStateToProps = (state) => { return { userinfo: state.appUserInfoReducer.userinfo, app_theme: state.appThemeReducer.theme } } const mapDispatchToProps = { saveUserInfo } export default connect(mapStateToProps, mapDispatchToProps)(Setmpin)