import React, { useState, useEffect } from 'react'; import { View, Text, ScrollView, TouchableOpacity, Alert } from 'react-native'; import { OtpInput, NumericKeypad, PrimaryButton } from '@components'; import { useAppTheme } from '@theme'; import { useNavigation } from '@react-navigation/native'; import { NativeStackNavigationProp } from '@react-navigation/native-stack'; import { AuthStackParamList } from '@navigation/navigationTypes'; import { RouteNames } from '@utils/constants'; import { getStyles } from './OtpScreen.styles'; export const OtpScreen: React.FC<{ route: any }> = ({ route }) => { const { colors } = useAppTheme(); const styles = getStyles(colors); const navigation = useNavigation>(); const phone = route?.params?.phone || '+91 98765 43210'; const [otp, setOtp] = useState(''); const [timer, setTimer] = useState(30); const [isLoading, setIsLoading] = useState(false); const [error, setError] = useState(false); useEffect(() => { if (timer <= 0) return; const interval = setInterval(() => { setTimer((prev) => prev - 1); }, 1000); return () => clearInterval(interval); }, [timer]); const handleKeyPress = (digit: string) => { if (otp.length < 6) { setError(false); setOtp((prev) => prev + digit); } }; const handleBackspace = () => { setOtp((prev) => prev.slice(0, -1)); }; const handleVerify = () => { if (otp.length < 6) { setError(true); Alert.alert('Invalid OTP', 'Please enter a 6-digit verification code.'); return; } setIsLoading(true); setTimeout(() => { setIsLoading(false); if (otp === '123456' || otp.length === 6) { navigation.navigate(RouteNames.SetLocation); } else { setError(true); Alert.alert('Verification Failed', 'The code you entered is incorrect. Try 123456.'); } }, 1200); }; const handleResend = () => { if (timer > 0) return; setOtp(''); setTimer(30); Alert.alert('OTP Resent', 'A new verification code has been sent to your mobile number.'); }; return ( Verify Phone Enter the 6-digit OTP code sent to{' '} {phone} {timer > 0 ? ( Resend code in 00:{timer < 10 ? `0${timer}` : timer} ) : ( Resend Code )} ); }; export default OtpScreen;