import React, {useState, useRef} from 'react'; import {RouteProp} from '@react-navigation/native'; import {StackNavigationProp} from '@react-navigation/stack'; import { View, Text, StyleSheet, StatusBar, Alert, Platform, TouchableOpacity, KeyboardAvoidingView, } from 'react-native'; import {OnboardingStackParams} from '../../routes'; import { ArrowButton, RegistrationWizard, TaggInput, Background, } from '../../components'; import {nameRegex, emailRegex} from '../../constants'; type RegistrationScreenOneRouteProp = RouteProp< OnboardingStackParams, 'RegistrationOne' >; type RegistrationScreenOneNavigationProp = StackNavigationProp< OnboardingStackParams, 'RegistrationOne' >; interface RegistrationOneProps { route: RegistrationScreenOneRouteProp; navigation: RegistrationScreenOneNavigationProp; } /** * Registration screen 1 for First Name, Last Name, and email * @param navigation react-navigation navigation object */ const RegistrationOne: React.FC = ({navigation}) => { // refs for changing focus const lnameRef = useRef(); const emailRef = useRef(); /** * Handles focus change to the next input field. * @param field key for field to move focus onto */ const handleFocusChange = (field: string): void => { switch (field) { case 'lname': const lnameField: any = lnameRef.current; lnameField.focus(); break; case 'email': const emailField: any = emailRef.current; emailField.focus(); break; default: return; } }; // registration form state const [form, setForm] = useState({ fname: '', lname: '', email: '', isValidFname: false, isValidLname: false, isValidEmail: false, attemptedSubmit: false, }); /* * Handles changes to the first name field value and verifies the input by updating state and running a validation function. */ const handleFnameUpdate = (fname: string) => { let isValidFname: boolean = nameRegex.test(fname); setForm({ ...form, fname, isValidFname, }); }; /* * Handles changes to the last name field value and verifies the input by updating state and running a validation function. */ const handleLnameUpdate = (lname: string) => { let isValidLname: boolean = nameRegex.test(lname); setForm({ ...form, lname, isValidLname, }); }; /* * Handles changes to the email field value and verifies the input by updating state and running a validation function. */ const handleEmailUpdate = (email: string) => { let isValidEmail: boolean = emailRegex.test(email); setForm({ ...form, email, isValidEmail, }); }; /** * Handles a click on the "next" arrow button by navigating to RegistrationTwo if First Name and Last Name are filled */ const goToRegisterTwo = async () => { if (!form.attemptedSubmit) { setForm({ ...form, attemptedSubmit: true, }); } try { if (form.isValidFname && form.isValidLname && form.isValidEmail) { navigation.navigate('RegistrationTwo', { firstName: form.fname, lastName: form.lname, email: form.email, }); } else { setForm({...form, attemptedSubmit: false}); setTimeout(() => setForm({...form, attemptedSubmit: true})); } } catch (error) { Alert.alert( 'Looks like our servers are down. 😓', "Try again in a couple minutes. We're sorry for the inconvenience.", ); return { name: 'Registration error', description: error, }; } }; const Footer = () => ( navigation.navigate('Login')} /> navigation.navigate('RegistrationTwo', { firstName: form.fname, lastName: form.lname, email: form.email, }) } /> ); return ( SIGN UP handleFocusChange('lname')} blurOnSubmit={false} valid={form.isValidFname} invalidWarning="Please enter a valid first name." attemptedSubmit={form.attemptedSubmit} width={280} /> handleFocusChange('email')} blurOnSubmit={false} ref={lnameRef} valid={form.isValidLname} invalidWarning="Please enter a valid last name." attemptedSubmit={form.attemptedSubmit} width={280} />