import {RouteProp} from '@react-navigation/native'; import {StackNavigationProp} from '@react-navigation/stack'; import React, {useRef, useState} from 'react'; import { Alert, KeyboardAvoidingView, Platform, StatusBar, StyleSheet, Text, TouchableOpacity, View, } from 'react-native'; import {trackPromise} from 'react-promise-tracker'; import { ArrowButton, Background, LoadingIndicator, RegistrationWizard, TaggInput, } from '../../../components'; import {phoneRegex, SEND_OTP_ENDPOINT} from '../../../constants'; import { ERROR_EMAIL_IN_USE, ERROR_SERVER_DOWN, } from '../../../constants/strings'; import {OnboardingStackParams} from '../../../routes'; import {BackgroundGradientType, VerificationScreenType} from '../../../types'; type RegistrationScreenOneRouteProp = RouteProp< OnboardingStackParams, 'RegistrationOne' >; type RegistrationScreenOneNavigationProp = StackNavigationProp< OnboardingStackParams, 'RegistrationOne' >; interface RegistrationOneProps { route: RegistrationScreenOneRouteProp; navigation: RegistrationScreenOneNavigationProp; } /** * Registration screen 1 for Email * @param navigation react-navigation navigation object */ const RegistrationOne: React.FC = ({navigation}) => { // // refs for changing focus const phoneRef = useRef(); // registration form state const [form, setForm] = useState({ phone_number: '', isValidPhone: false, attemptedSubmit: false, }); /* * Handles changes to the email field value and verifies the input by updating state and running a validation function. */ const handlePhoneUpdate = (phone_number: string) => { phone_number = phone_number.trim(); let isValidPhone: boolean = phoneRegex.test(phone_number); setForm({ ...form, phone_number, isValidPhone, }); }; /** * Handles a click on the "next" arrow button by navigating to Verification if email is filled correctly */ const goToVerification = async () => { if (!form.attemptedSubmit) { setForm({ ...form, attemptedSubmit: true, }); } try { if (form.isValidPhone) { let sendOtpResponse = await trackPromise( fetch(SEND_OTP_ENDPOINT, { method: 'POST', body: JSON.stringify({ phone_number: '+1' + form.phone_number, }), }), ); let otpStatusCode = sendOtpResponse.status; if (otpStatusCode === 200) { navigation.navigate('Verification', { id: form.phone_number, screenType: VerificationScreenType.Phone, }); } else if (otpStatusCode === 409) { Alert.alert(ERROR_EMAIL_IN_USE); } else { Alert.alert(ERROR_SERVER_DOWN); } } 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: 'Send OTP error', description: error, }; } }; const Footer = () => ( navigation.navigate('Login')} /> ); return ( ENTER PHONE NUMBER