import * as React from 'react';
import {
TouchableOpacity,
View,
Text,
KeyboardAvoidingView,
Alert,
Platform
} from 'react-native';
import { connect } from "react-redux";
import NetInfo from "../../../components/netstatus";
import CustomHeader from '../../../components/header.js';
import Elements from '../../../components/elements.js';
import Theme from '../../../components/theme.style.js';
import DB from '../../../components/storage/';
import REQUEST from '../../../components/api/';
import CustomOTPInput from '../../../components/otpinput';
import CustomSafeArea from '../../../components/safeArea.component';
class TermsConditions extends React.PureComponent {
constructor(props) {
super(props)
}
state = {
loading: false,
infodialog: false,
errordialog: false,
otp: "",
resend: false,
retries: 0,
valid: false,
trans_id: null,
session: null,
data: {},
}
componentDidMount() {
this.init()
this.activateResend()
}
componentWillUnmount() {
}
init = async () => {
const session = await DB.get("session")
this.setState({ data: this.props.route.params, trans_id: this.props.route.params.trans_id, session: session })
}
activateResend = () => {
setTimeout(() => {
this.setState({ resend: true })
}, 15000)
}
encyptNumber = (contact) => {
let replacement = "****"
let index = 6
return contact.substr(0, index) + replacement+ contact.substr(index + replacement.length)
}
getRetries = () => { return this.state.retries }
onRetry = async () => {
if(this.state.resend){
this.setState({ loading: true })
let mobilenum = this.state.data.mobile_number.substr(2, this.state.data.mobile_number.length-1)
await REQUEST("mobileOTP", "get", {}, `mobile=${mobilenum}`, {}, (otp) => {
if(otp.status == 1){
this.setState({ loading: false, trans_id: otp.data.trans_id })
Platform.OS == 'ios' ? setTimeout(() => {
Alert.alert("Requested.", '\n' + "New OTP code will be submitted on your mobile number.")
}, 300) : Alert.alert("Requested.", '\n' + "New OTP code will be submitted on your mobile number.")
} else {
this.setState({ loading: false })
}
}, (err) => {
Alert.alert("Information", `\n${err.message}`);
this.setState({ loading: false })
}, "OTP", "Request")
}
}
validateOTP = async (successCallback, errorCallback) => {
this.setState({ loading: true })
await REQUEST("validateMobileOTP", "get", {}, `trans_id=${this.state.trans_id}&otp=${this.state.otp}&mobile=${this.state.data.mobile}&birthday=${this.state.data.birthday}`, {}, function(res){
successCallback(res)
}, function(error){
errorCallback(error)
}, "OTP", "Validate");
}
onSubmit = async () => {
NetInfo.netstatus(isConnected => {
if(isConnected){
this.setState({ loading: true })
this.validateOTP(success => {
if(success.status == 1){
this.setState({ loading: false })
this.props.route.params.onValid(true)
}else{
this.setState({ loading: false })
Alert.alert("Information", '\n' + success.message)
this.setState({ resend: true })
}
}, err => {
Alert.alert("Information", `\n${err.message}`);
})
}else{
Elements.nointernet2(this.props)
}
})
}
showContent = () => {
return (
Enter Code
Enter the 4-digit verification code sent to
{Theme.formatter.ENCMBL(this.state.data.mobile_number || "+639000000000")}
{
this.setState({ otp: v, valid: v.length == 4 ? true : false })
}}
/>
this.onSubmit()} style={{backgroundColor: this.state.valid ? Theme.colors.primary : this.props.app_theme?.theme.dark ? this.props.app_theme?.theme.colors.border : Theme.colors.primary + "15", padding: 15, width: '80%', borderRadius: 10, elevation: this.state.valid ? 3 : 0}}>
Submit
this.onRetry()} style={{flex: 2}}>
Resend Code
)
}
render() {
return (
this.setState({ infodialog: false })}
onSubmit={(value) => this.setState({ infodialog: false })}
/>
this.setState({ errordialog: false })}
onSubmit={(value) => this.setState({ errordialog: false })}
/>
this.props.navigation.goBack()} navigation={this.props.navigation} />
{
Theme.platform === "android" ?
this.showContent()
:
{this.showContent()}
}
);
}
}
const mapStateToProps = (state) => {
return {
app_theme: state.appThemeReducer.theme
}
}
export default connect(mapStateToProps, null)(TermsConditions);