import React from 'react'; import {OnboardingStackParams} from '../../routes'; import {RouteProp} from '@react-navigation/native'; import {StackNavigationProp} from '@react-navigation/stack'; import { Background, RegistrationWizard, SubmitButton, ArrowButton, LoadingIndicator, } from '../../components'; import {VERIFY_OTP_ENDPOINT, SEND_OTP_ENDPOINT} from '../../constants'; import {Text} from 'react-native-animatable'; import { CodeField, Cursor, useBlurOnFulfill, useClearByFocusCell, } from 'react-native-confirmation-code-field'; import { StyleSheet, View, TouchableOpacity, KeyboardAvoidingView, Alert, Platform, } from 'react-native'; import {trackPromise} from 'react-promise-tracker'; type VerificationScreenRouteProp = RouteProp< OnboardingStackParams, 'Verification' >; type VerificationScreenNavigationProp = StackNavigationProp< OnboardingStackParams, 'Verification' >; interface VerificationProps { route: VerificationScreenRouteProp; navigation: VerificationScreenNavigationProp; } const Verification: React.FC = ({route, navigation}) => { const [value, setValue] = React.useState(''); const ref = useBlurOnFulfill({value, cellCount: 6}); const [valueProps, getCellOnLayoutHandler] = useClearByFocusCell({ value, setValue, }); const {phone} = route.params; /** * Sends the verify_otp request upon tapping the Verify button. * If successful, it navigates to the Profile page. */ const handleVerification = async () => { try { let verifyOtpResponse = await fetch(VERIFY_OTP_ENDPOINT, { method: 'POST', body: JSON.stringify({ phone_number: '+1' + phone, otp: value, }), }); let statusCode = verifyOtpResponse.status; if (statusCode === 200) { navigation.navigate('RegistrationTwo', { phone: phone, }); } else { Alert.alert( 'Invalid verification code 🤔', 'Try again. Tap the resend code button if you need a new code.', ); } } catch (error) { Alert.alert( 'Verifiation failed 😓', 'Please double-check your network connection and retry.', ); return { name: 'Verification error', description: error, }; } }; /** * Sends the send_otp request so to provide a new verification code upon tapping the Resend Code button. */ const handleResend = async () => { try { let sendOtpResponse = await trackPromise( fetch(SEND_OTP_ENDPOINT, { method: 'POST', body: JSON.stringify({ phone: phone, }), }), ); let sendOtpStatusCode = sendOtpResponse.status; if (sendOtpStatusCode === 200) { setValue(''); Alert.alert( 'New verification code sent!', 'Check your phone messages for your code.', ); } else { Alert.alert('Something went wrong!'); } } catch (error) { console.log(error); } }; const Footer = () => ( navigation.navigate('RegistrationOne')} /> ); return ( Enter 6 digit code We sent a 6 digit verification code to the phone number you provided. ( {symbol || (isFocused ? : null)} )} /> Resend Code