import * as React from 'react'; import { View, Text, TouchableOpacity, ScrollView, TextInput, StyleSheet, Alert, Dimensions, Platform } from 'react-native'; import { KeyboardAvoidingView } from 'native-base'; import { connect } from "react-redux"; import { saveUserInfo } from "../../redux/actions/AppUserInfoActions"; import Crypto from '../../components/crypto.js'; import CustomHeader from '../../components/header.js'; import Elements from '../../components/elements.js'; import NetInfo from "../../components/netstatus"; import REQUEST from '../../components/api/'; import Theme from '../../components/theme.style.js'; import DB from '../../components/storage'; import Icon from './../../components/icons.js'; import CustomSafeArea from '../../components/safeArea.component'; class Setnewmpin extends React.Component { constructor(props) { super(props) } state = { loading: false, mpin: "", confirmMpin: "", valid: "", activeInput: -1 } componentDidMount() { } 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 => { this.getUserInfo(onSuccess => { this.setState({ loading: false }) this.props.navigation.navigate('SuccessUpdateMpin') }, onError => { this.setState({ loading: false }) }) }, error => { this.setState({ loading: false }) Platform.OS == 'ios' ? setTimeout(() => { Alert.alert("New Mpin.", "Failed to set mpin. Please try again.") }, 300) : Alert.alert("New Mpin.", "Failed to set mpin. Please try again.") }) } else { this.setState({ loading: false }) Elements.nointernet2(this.props) } }) } 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 { mpin } = this.state this.getCurrentUserInfo(async (onSuccess) => { let hash = onSuccess.data.customer_number.substring(0, 8) let ciphermpin = Crypto.encrypt(mpin, hash) const params = { lcard_uuid: this.props.route.params.sessiondata.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) }, error => { this.setState({ loading: false }) errorCallback(error) }) } }) .catch(error => errorCallback(error)) } onInputFocus = (x) => { this.setState({ activeInput: x }) } onBackToPrevious = () => { Alert.alert( '', 'Are you sure you want to cancel?', [ { textStyle: 'cancel', text: 'Cancel' }, { text: 'Ok', onPress: async () => { this.props.navigation.navigate('Mpin') }, }, ], {cancelable: true}, ); } render() { return( Set new MPIN In order to protect your account, make sure your password: - Is not weak and easy to guess. - Do not use MPIN such as 1234. - Do not use your birthdate. New MPIN this.onInputFocus(1)} onBlur={() => { this.setState({ activeInput: -1 }) }} onChangeText={(v) => { let value = v.includes(".") ? v.replace(".", "") : v this.setState({ mpin: value, valid: value.length == 4 ? true : false }) }} style={{marginTop: 20, marginLeft: 35, width: '75%', fontSize: 17 ,borderBottomColor: this.state.activeInput == 1 ? Theme.colors.accent : "gray", borderBottomWidth: this.state.activeInput == 1 ? 1.5 : 1, paddingBottom: 5, color: this.props.app_theme?.theme.dark ? Theme.colors.white : Theme.colors.black }} /> Verify MPIN this.onInputFocus(2)} onBlur={() => { this.setState({ activeInput: -1 }) }} onChangeText={(v) => { let value = v.includes(".") ? v.replace(".", "") : v this.setState({ confirmMpin: value, valid: value.length == 4 ? true : false }) }} style={{marginTop: 20, marginLeft: 35, width: '75%', fontSize: 17, borderBottomColor: this.state.activeInput == 2 ? Theme.colors.accent : "gray", borderBottomWidth: this.state.activeInput == 2 ? 1.5 : 1, paddingBottom: 5, color: this.props.app_theme?.theme.dark ? Theme.colors.white : Theme.colors.black }} /> {(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)(Setnewmpin) export const styles = StyleSheet.create({ })