import * as React from 'react';
import {
ScrollView,
TouchableOpacity,
View,
Text,
TextInput,
Alert,
Platform
} 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 MobileValidation from '../../components/api/mobile.js';
import CustomSafeArea from '../../components/safeArea.component.js';
const CustomInput = (props) => {
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 == 1 ? 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.PureComponent {
constructor(props) {
super(props)
this.cardnumber = ""
}
state = {
loading: false,
card: null,
datepicker: false,
focus: false,
activeInput: null,
agree: false,
errors: null,
processed: false,
fname: null,
lname: null,
email: null,
birthdate: "",
number: null,
valid: false,
validEmail: true,
validPhone: true,
}
componentDidMount() {
this.setState({ card: this.props.route?.params || {} })
}
componentWillUnmount() {
}
setCardNumber = () => {
this.setState({ valid: this.cardnumber.length == 15 ? true : false })
}
onAgree = () => {
this.setState({ agree: this.state.agree == false ? true : false, valid: !this.state.agree })
}
validateEmail = () => {
return this.state.includes("@")
}
validateNumber = () => {
return this.state.number != null ? this.state.number.length > 11 : false
}
validatemobile = async () => {
let num = this.state.number.substr(1, this.state.number.length-1)
let validation = await MobileValidation(num)
if(!this.state.processed && validation.status == 0){
Platform.OS == 'ios' ? setTimeout(() => {
Alert.alert("Error", validation.message)
}, 300) : Alert.alert("Error", validation.message)
this.setState({ errors:{mobile: [validation.message]} })
} else {
this.activateCard(success => {
this.processActivatedCard(success)
}, error => {
console.log(error)
})
}
}
activateCard = 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,
pin: this.state.card.pin,
card_number: this.state.card.card_number,
lastname: this.state.lname,
mobile: Theme.formatter.PMBL(this.state.number.substr(1, this.state.number.length-1) || "")
}
await REQUEST("activateCard", "post", {}, {}, body, async (res) => {
successCallback(res)
}, function(error){
console.log(error)
errorCallback(error)
})
}
stripString = (regex, regexReplace ,string, replaceString) => {
let validMobileNumber = regex.test(string);
if(!validMobileNumber) {
return null
}
return string.replace(regexReplace, replaceString)
}
processActivatedCard = (success) => {
console.log(success, this.props)
if(success.status == 1){
this.setState({ errors: null, processed: true, loading: false })
this.props.navigation.navigate("LoginSendOTP", {
lcard_uuid: success.data.lcard_uuid,
mobile_number: this.state.number.substr(1, this.state.number.length-1),
card_number: this.state.card.card_number,
transactionType: this.props.route.params?.transactionType
})
} else if(success.status == 0 && Object.keys(success.data).length == 0) {
Platform.OS == 'ios' ? setTimeout(() => {
}, 300) :
this.setState({ errors: null })
}else{
setTimeout(() => {
}, 300)
this.setState({ errors: success.data })
}
}
onSubmit = () => {
if(this.state.valid){
this.validatemobile()
}
}
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") : birthdate != null ? 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({ datepicker: true, activeInput: 4 })
}}
style={{}}>
{this.state.birthdate || "Birthday"}
{this.state.errors && this.state.errors.birthdate ?
{this.state.errors?.birthdate[0]}
: null}
{
this.setState({ activeInput: 5, focus: true, errors: null })
if(this.state.number == null) this.setState({ number: "+63" })
}}
onChangeText={(val) => {
this.setState({ number: val })
}}
/>
this.state.fname && this.state.lname && this.state.email && this.state.birthdate && this.state.number ? this.onAgree() : null} style={{flex: 0, paddingTop: 15}}>
{!this.state.agree ? : }
I agree to
this.props.navigation.navigate("TermsConditions", {screen: 'back'})}>
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);