import * as React from 'react'; import { View, Text, TouchableOpacity, ScrollView, TextInput, StyleSheet, Alert, Dimensions, Platform } from 'react-native'; import { Icon, 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 Icons from '../../components/icons'; import CustomSafeArea from '../../components/safeArea.component'; class Setnewmpin extends React.Component { constructor(props) { super(props) } state = { loading: false, mpin: "", confirmMpin: "", valid: "", activeInput: -1, showPass1: false, showPass2: false } 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.", '\n' + "Failed to set mpin. Please try again.") }, 300) : Alert.alert("New Mpin.", '\n' + "Failed to set mpin. Please try again.") }) } else { this.setState({ loading: false }) Elements.nointernet2(this.props) } }) } else { Platform.OS == 'ios' ? setTimeout(() => { Alert.alert("Mpin.", '\n' + "Please ensure that your MPIN is matched") }, 300) : Alert.alert("Mpin.", '\n' + "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){ successCallback(data) }, function(error){ errorCallback(error) }, "MPIN", "Setup") }, (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}, ); } showContent = () => { 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={{ color: this.props.app_theme?.theme.dark ? Theme.colors.white : Theme.colors.black, fontSize: 17, flex: 1}} /> this.setState({ showPass1: !this.state.showPass1 })}> 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={{ color: this.props.app_theme?.theme.dark ? Theme.colors.white : Theme.colors.black, fontSize: 17, flex: 1}} /> this.setState({ showPass2: !this.state.showPass2 })}> {(this.state.confirmMpin.length > 0 && !this.checkValidMpin()) && ( Please ensure that your MPIN is matched )} ) } render() { return( this.props.navigation.reset({index: 0, routes: [{name: "Mpin"}]})} navigation={this.props.navigation} /> { Theme.platform === "android" ? this.showContent() : {this.showContent()} } this.setAppMpin()} disabled={!this.checkValidMpin()} style={{padding: 20, paddingTop: 15, width: Theme.screen.w - 60, paddingBottom: 15, borderRadius: 10, backgroundColor: this.state.valid ? Theme.colors.primary : this.props.app_theme?.theme.dark ? this.props.app_theme?.theme.colors.border : Theme.colors.primary + "15", alignItems: 'center'}}> 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({ })