import * as React from 'react';
import {
View,
Text,
TouchableOpacity,
ScrollView,
Alert,
Dimensions,
KeyboardAvoidingView,
Platform} from 'react-native';
import { connect } from "react-redux";
import { saveUserInfo } from "../../redux/actions/AppUserInfoActions";
import Icon from '../../components/icons.js';
import CustomHeader from '../../components/header.js';
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 => {})
}
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.reset({
index: 0,
routes: [{name: "OnBoarding", params: { token: this.props.route.params?.token }}],
});
}
}
}, onError => {
this.setState({ loading: false })
})
}, error => {
Platform.OS == 'ios' ? setTimeout(() => {
Alert.alert("Mpin.", '\n' + "Failed to set mpin. Please try again.")
}, 300) : Alert.alert("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 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){
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)
console.log("Error saving profile", error)
})
}
})
.catch(error => errorCallback(error))
}
showContent = () => {
return (
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
)}
)
}
render() {
return(
this.props.navigation.goBack()} 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"
backgroundColor: this.state.valid ? Theme.colors.primary : Theme.colors.primary + "15"
}}
>
Submit
)
}
}
const mapStateToProps = (state) => {
return {
userinfo: state.appUserInfoReducer.userinfo,
app_theme: state.appThemeReducer.theme
}
}
const mapDispatchToProps = {
saveUserInfo
}
export default connect(mapStateToProps, mapDispatchToProps)(Setmpin)