import * as React from 'react';
import { useRef } from 'react';
import {
TouchableOpacity,
View,
Text,
TextInput,
Alert,
Platform,
ScrollView
} from 'react-native';
import { connect } from "react-redux";
import CustomHeader from '../../../components/header.js';
import Elements from '../../../components/elements.js';
import Theme from '../../../components/theme.style.js';
import Icon from '../../../components/icons.js';
import moment from 'moment';
import DB from '../../../components/storage/';
import REQUEST from '../../../components/api/';
import CustomSafeArea from '../../../components/safeArea.component.js';
import { KeyboardAvoidingView } from 'native-base';
const CustomInput = (props) => {
const _textInput = useRef(null);
const titlecolor = props.current == props.index && props.focus ? Theme.colors.accent : Theme.colors.darkGray
const bordercolor = props.error ? Theme.colors.primary : (props.focus && props.current == props.index ? Theme.colors.accent : 'gray')
const borderwidth = props.error ? 1.6 : (props.focus && props.current == props.index ? 1.5 : 1)
const style = {
container: {flexDirection: 'row', width: '80%', marginTop: props.top || 30, alignItems: 'center'},
title: {fontSize: 12, color: titlecolor, marginTop: -25, marginBottom: 15},
input: {width: '100%', fontSize: 16, padding: 0, borderBottomColor: bordercolor, borderBottomWidth: borderwidth, color: props.isDarkMode ? props.textColor : Theme.colors.black},
error: {fontSize: 12, color: Theme.colors.primary, marginTop: 5, marginBottom: 15}
}
return (
{props.current >= props.index ?
{props.title}
: null}
{props.error ? {props.errorMessage} : null }
)
}
class ApplyForm extends React.Component {
constructor(props) {
super(props)
this.cardnumber = ""
}
state = {
loading: false,
card: null,
datepicker: false,
focus: false,
activeInput: 0,
agree: false,
errors: null,
fname: null,
lname: null,
email: null,
birthdate: "",
number: null,
valid: false,
validEmail: true,
validPhone: true,
}
componentDidMount() {
this.init()
}
componentWillUnmount() {
}
setCardNumber = () => {
this.setState({ valid: this.state.cardnumber.length == 15 ? true : false })
}
onAgree = () => {
this.setState({ agree: this.state.agree == false ? true : false, valid: !this.state.agree })
}
// validateEmail = () => {
// return this.state.email.includes("@")
// }
validateEmail = () => {
let regEx = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/
return regEx.test(this.state.email)
}
validateNumber = () => {
return this.state.number != null ? this.state.number.length > 11 : false
}
init = async () => {
this.setState({ card: this.props.route?.params || {} })
await DB.remove("is_guest")
}
//registerAndValidate
onRegisterAndValidate = async (successCallback, errorCallback) => {
//this.setState({ loading: true })
let uuid = await DB.get("deviceUUID")
let body = {
birthdate: Theme.formatter.DT4API(this.state.birthdate || ""),
cardtype_uuid: this.state.card.card_uuid,
deviceUUID: uuid,
email: this.state.email,
firstname: this.state.fname,
id_number: this.state.card.id_number,
lastname: this.state.lname,
mobile: Theme.formatter.PMBL(this.state.number.substr(1, this.state.number.length-1) || "")
}
await REQUEST("registerAndValidate", "post", {}, {}, body, async (signup) => {
console.log("APPLY RESULT", signup.data)
if(signup.status == 1){
successCallback(signup.data,body);
}else{
errorCallback(signup);
}
}, function(error){
console.log(signup)
errorCallback(error)
})
}
onRegisterAndValidate = async (successCallback, errorCallback) => {
//this.setState({ loading: true })
let uuid = await DB.get("deviceUUID")
let body = {
birthdate: Theme.formatter.DT4API(this.state.birthdate || ""),
cardtype_uuid: this.state.card.card_uuid,
deviceUUID: uuid,
email: this.state.email,
firstname: this.state.fname,
id_number: this.state.card.id_number,
lastname: this.state.lname,
mobile: Theme.formatter.PMBL(this.state.number.substr(1, this.state.number.length-1) || "")
}
await REQUEST("registerAndValidate", "post", {}, {}, body, async (signup) => {
console.log("APPLY RESULT", signup.data)
if(signup.status == 1){
successCallback(signup.data,body);
}else{
errorCallback(signup);
}
}, function(error){
console.log(signup)
errorCallback(error)
})
}
onValidateAndActivate = async (successCallback, errorCallback) => {
this.setState({ loading: true })
let uuid = await DB.get("deviceUUID")
let body = {
birthdate: Theme.formatter.DT4API(this.state.birthdate || ""),
cardtype_uuid: this.state.card.card_uuid,
deviceUUID: uuid,
email: this.state.email,
firstname: this.state.fname,
id_number: this.state.card.id_number,
lastname: this.state.lname,
mobile: Theme.formatter.PMBL(this.state.number.substr(1, this.state.number.length-1) || "")
}
await REQUEST("submitRegistration", "post", {}, {}, body, async (signup) => {
console.log("APPLY RESULT", signup.data)
if(signup.status == 1){
await REQUEST("user_profile", "get", {
Authorization: "Bearer " + signup.data.token,
card_number: ''
}, {}, {}, function(act){
console.log(act)
successCallback(act, signup, body)
}, (err) => {
console.log(err)
errorCallback(err)
});
} else if(signup.status == 0 && Object.keys(signup.data).length == 0) {
console.log(signup)
errorCallback(signup)
} else {
console.log(signup)
errorCallback(signup)
}
}, function(error){
console.log(signup)
errorCallback(error)
})
}
onApplyCardSubmit = async (successCallback, errorCallback) => {
this.setState({ loading: true })
let uuid = await DB.get("deviceUUID")
let body = {
birthdate: Theme.formatter.DT4API(this.state.birthdate || ""),
cardtype_uuid: this.state.card.card_uuid,
deviceUUID: uuid,
email: this.state.email,
firstname: this.state.fname,
id_number: this.state.card.id_number,
lastname: this.state.lname,
mobile: Theme.formatter.PMBL(this.state.number.substr(1, this.state.number.length-1) || "")
}
await REQUEST("apply", "post", {}, {}, body, async (signup) => {
console.log("APPLY RESULT", signup.data)
if(signup.status == 1){
await REQUEST("user_profile", "get", {
Authorization: "Bearer " + signup.data.token,
card_number: ''
}, {}, {}, function(act){
console.log(act)
successCallback(act, signup, body)
}, (err) => {
console.log(err)
errorCallback(err)
});
} else if(signup.status == 0 && Object.keys(signup.data).length == 0) {
console.log(signup)
errorCallback(signup)
} else {
console.log(signup)
errorCallback(signup)
}
}, function(error){
console.log(signup)
errorCallback(error)
})
}
requestMobileOtp = async (mobilenum, successCallback, errorCallback) => {
await REQUEST("mobileOTP", "get", {}, `mobile=${mobilenum}`, {}, function(otp){
successCallback(otp)
}, function(error){
errorCallback(error)
})
}
openActivityForResult = async (data) => {
data.onValid = (result) => {
this.setState({ loading: true })
if(result) {
this.onValidateAndActivate(async (success, info, body) => {
const sessionData = {
token: info.data.token,
session: {
birthdate: body.birthdate,
card_number: success.data.card_number,
lcard_uuid: success.data.lcard_uuid,
mobile_number: body.mobile
}
}
console.log(success)
await DB.login(sessionData, () => {
this.setState({ loading: false })
let data = {
sessiondata: sessionData.session,
token: info.data.token,
transactionType: "ApplyCard"
}
if(success.data.mpin == undefined || success.data.mpin == "") {
this.props.navigation.navigate("Setmpin", data)
} else {
this.props.navigation.navigate("OnBoarding", { token: info.data.token })
}
}, (err) => {
this.setState({ loading: false })
})
}, error => {
if(error.status == 0 && Object.keys(error.data).length == 0) {
console.log(error)
if(error.message.includes("Mobile number")){
this.setState({ loading: false, errors: {mobile: [error.message]} })
}else{
Platform.OS == 'ios' ? setTimeout(() => {
Alert.alert("Error", error.message)
}, 300) :
Alert.alert("Error", error.message)
this.setState({ loading: false, errors: null })
}
} else if(Object.keys(error.data).length > 0) {
console.log(error)
Platform.OS == 'ios' ? setTimeout(() => {
Alert.alert("Error", error.data)
}, 300) :
Alert.alert("Error", error.data)
this.setState({ loading: false, errors: error.data })
} else {
console.log(error)
Platform.OS == 'ios' ? setTimeout(() => {
Alert.alert("Error", error.message)
}, 300) :
Alert.alert("Error", error.message)
this.setState({ loading: false, errors: error })
}
});
}
}
this.props.navigation.navigate("ApplyOTP", data)
}
onSubmit = () => {
if(this.state.valid){
let errs = {}
let bdayElapsed = moment(this.state.birthdate).fromNow(true).split(" ")
if(!this.state.fname) errs = Object.assign({"firstname": ["The firstname field is required."]}, errs)
if(!this.state.lname) errs = Object.assign({"lastname": ["The lastname field is required."]}, errs)
if(!this.state.birthdate) errs = Object.assign({"birthdate": ["The birthdate field is required."]}, errs)
if(bdayElapsed[1] != 'years' || bdayElapsed[1] == 'years' && bdayElapsed[0] < 18) errs = Object.assign({"birthdate": ["You must be at least 18 years old to join the Loyalty Program"]}, errs)
if(!this.state.number) errs = Object.assign({"mobile": ["The mobile field is required."]}, errs)
if(!this.state.email) errs = Object.assign({"email": ["The email field is required."]}, errs)
if(this.state.number.length < 11) errs = Object.assign({"mobile": ["Please enter your 10-digit Mobile Phone Number"]}, errs)
if(!this.validateEmail()) errs = Object.assign({"email": ["The email address is invalid"]}, errs)
this.setState({ errors: errs })
if(Object.keys(errs).length > 0) {
return false
} else {
let mobilenum = this.state.number.substr(3, this.state.number.length-1)
this.onRegisterAndValidate(async (data, body) => {
this.setState({ loading: false })
this.openActivityForResult({
trans_id: data.trans_id,
mobile_number: "63" + mobilenum,
birthday:Theme.formatter.DT4API(this.state.birthdate || ""),
mobile:mobilenum
})
}, error => {
Platform.OS == 'ios' ? setTimeout(() => {
Alert.alert("Error", error.message)
}, 300) :
Alert.alert("Error", error.message)
this.setState({ loading: false, errors:{ mobile: [success.message] }})
})
}
}
}
onChangeTextValueFirstname = (text) => {
if (/^[a-zA-Z ]+$/.test(text) || text === "") {
this.setState({ fname: text })
}
}
onChangeTextValueLastname = (text) => {
if (/^[a-zA-Z ]+$/.test(text) || text === "") {
this.setState({ lname: text })
}
}
render() {
return (
{
this.setState({ birthdate: selectedDate ? moment(selectedDate).format("DD MMM YYYY") : this.state.birthdate != null ? this.state.birthdate : null, datepicker: false, agree: false, valid: false })
}}
onCancel={() => this.setState({ datepicker: false })}
/>
this.props.navigation.goBack()} navigation={this.props.navigation} />
Enter Your Details
Fill out the remaining forms and you're good to go!
{
this.setState({ activeInput: 1, focus: true })
}}
onChangeText={this.onChangeTextValueFirstname}
/>
{
this.setState({ activeInput: 2, focus: true })
}}
onChangeText={this.onChangeTextValueLastname}
/>
{
this.setState({ activeInput: 3, focus: true })
}}
onChangeText={(val) => {
this.setState({ email: val })
}}
/>
{this.state.activeInput >= 4 ? Birthday : null}
{
this.setState({ focus: true, activeInput: 4, datepicker: true })
if(this.state.errors && this.state.errors.birthdate) delete this.state.errors.birthdate
}}>
{this.state.birthdate || "Birthday"}
{this.state.errors && this.state.errors.birthdate ?
{this.state.errors?.birthdate[0]}
: null}
{
this.setState({ activeInput: 5, focus: true })
if(this.state.number == null) this.setState({ number: '+63' })
}}
onChangeText={(val) => {
this.setState({ number: val, errors: null })
if(this.state.agree){
if(val.length < 13) this.setState({ valid: false })
else if(val.length == 13) this.setState({ valid: true })
}
}}
/>
this.state.fname && this.state.lname && this.state.birthdate && this.state.email && this.state.number ? this.onAgree() : null} style={{flex: 0, paddingTop: 15}}>
{!this.state.agree ? : }
I agree to
this.props.navigation.navigate("TermsConditions", {screen: 'ApplyForm'})}>
Unioil's Data Privacy Policy.
this.onSubmit()} disabled={!this.state.valid} 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"}}>
Submit
);
}
}
const mapStateToProps = (state) => {
return {
app_theme: state.appThemeReducer.theme
}
}
export default connect(mapStateToProps, null)(ApplyForm);