import * as React from 'react'; import { ScrollView, TouchableOpacity, View, Text, TextInput, Alert, Platform } from 'react-native'; import { Container } from 'native-base'; 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 == 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: 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 == 15 ? true : false }) } onAgree = () => { this.setState({ agree: this.state.agree == false ? true : false, valid: !this.state.agree }) } validateEmail = () => { return this.state.email.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) console.log("MOBILE VALIDATION", validation) 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.setState({ loading: true }) this.activateCard(success => { this.processActivateCard(success) }, error => { this.setState({ loading: false }) }) } } 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){ errorCallback(error) }) } onSubmit = () => { if(this.state.valid){ this.validatemobile() } } processActivateCard = (success) => { if(success.status == 1){ this.setState({ errors: null, processed: true, loading: false }) success.data.mobile_number = this.state.number.substr(1, this.state.number.length-1) success.data.target = "enroll" // success.data.callback = async (token) => { // this.setState({ loading: true }) // const { callback } = this.props.route.params // await DB.settoken(token, async () => { // this.fetchProfile(success => { // if(success.status == 1){ // DB.addaccount(success, function(){ // if(callback) callback("success") // this.props.navigation.navigate("Account") // }, function(error){ // console.log("Error saving profile", error) // }) // } // this.setState({ loading: false }) // }, error => { // this.setState({ loading: false }) // console.log(error) // }) // }, function(e){ // console.log("DEVICE INFO SAVING FAILED!", e) // }) // } // success.data.catcher = (res) => { // if(res.status == 0){ // setTimeout(() => { // this.setState({ mobile: [res.message] }) // console.log(res) // }, 300) // } // } this.setState({ loading: true }) console.log(this.props) this.requestOTP(success.data.lcard_uuid, success.data.mobile_number, onSuccess => { this.setState({ loading: false }) success.data.sessiondata = { data: this.state.card, callback: this.props.route.params?.callback, type: this.props.route.params?.type } this.props.navigation.navigate("AccountEnrollOtp", success.data) }, onError => { this.setState({ loading: false }) }) } else if(success.status == 0 && Object.keys(success.data).length == 0) { Platform.OS == 'ios' ? setTimeout(() => { Alert.alert("Error", success.message) }, 300) : Alert.alert("Error", success.message) this.setState({ errors: null, loading: false }) } else { this.setState({ errors: success.data, loading: false }) } } requestOTP = async (lcard_uuid, mobile_number, successCallback, errorCallback) => { let params = `lcard_uuid=${lcard_uuid}&mobile=${Theme.formatter.PMBL(mobile_number)}&is_resend=false`; await REQUEST("requestOTP", "get", {}, params, {}, function(res){ successCallback(res) }, function(error){ errorCallback(error) } ) } 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);