import React, { useState, useEffect, useRef } from 'react'; import { View, Text, StyleSheet, SafeAreaView, Alert, TouchableOpacity, TouchableWithoutFeedback, Keyboard, BackHandler, Platform, KeyboardAvoidingView, } from 'react-native'; import { StatusBar } from 'expo-status-bar'; import { useFocusEffect } from '@react-navigation/native'; import { useAuth } from '../../contexts/AuthContext'; import Button from '../../components/Button'; import { CodeField, Cursor, useBlurOnFulfill, useClearByFocusCell, } from 'react-native-confirmation-code-field'; import { COLORS } from '../../constants/colors'; const CELL_COUNT = 6; const VerificationScreen = ({ navigation }) => { const [code, setCode] = useState(''); const [countdown, setCountdown] = useState(60); const [canResend, setCanResend] = useState(false); const { verify, isLoading, pendingVerification, clearPendingVerification } = useAuth(); const countdownRef = useRef(null); const ref = useBlurOnFulfill({ value: code, cellCount: CELL_COUNT }); const [props, getCellOnLayoutHandler] = useClearByFocusCell({ value: code, setValue: setCode, }); useEffect(() => { if (!pendingVerification) { // If no pending verification, go back to login navigation.replace('Login'); return; } startCountdown(); return () => { if (countdownRef.current) { clearInterval(countdownRef.current); } }; }, []); // Handle Android hardware back button useFocusEffect( React.useCallback(() => { const onBackPress = () => { handleGoBack(); return true; // Prevent default behavior }; if (Platform.OS === 'android') { const sub = BackHandler.addEventListener('hardwareBackPress', onBackPress); return () => sub.remove(); } }, [handleGoBack]) ); const startCountdown = () => { setCountdown(60); setCanResend(false); countdownRef.current = setInterval(() => { setCountdown((prev) => { if (prev <= 1) { setCanResend(true); if (countdownRef.current) { clearInterval(countdownRef.current); } return 0; } return prev - 1; }); }, 1000); }; const handleVerify = async (filledCode) => { if (!filledCode.trim()) { Alert.alert('Ýalňyşlyk', 'Tassyklama koduny giriziň'); return; } if (!/^\d{6}$/.test(filledCode.trim())) { Alert.alert('Ýalňyşlyk', 'Tassyklama kody 6 sanly bolmaly'); return; } const result = await verify(filledCode.trim()); if (result.success) { // Navigation will be handled by AuthContext } else { Alert.alert('Ýalňyşlyk', result.error); } }; const handleVerifyWrapper = () => handleVerify(code); const handleResendCode = async () => { if (!canResend) return; // Here you would call the API to resend the code // For now, just restart the countdown Alert.alert('Üstünlik', 'Täze tassyklama kody iberildi'); startCountdown(); }; const handleGoBack = React.useCallback(() => { clearPendingVerification(); // Check if we can go back, if not navigate to Login if (navigation.canGoBack()) { navigation.goBack(); } else { navigation.navigate('Login'); } }, [clearPendingVerification, navigation]); const formatPhoneNumber = (phone) => { if (!phone) return ''; const phoneStr = phone.toString(); return `+993 ${phoneStr.slice(0, 2)} ${phoneStr.slice(2, 4)}-${phoneStr.slice(4, 6)}-${phoneStr.slice(6)}`; }; if (!pendingVerification) { return null; // Will navigate away in useEffect } return ( ← Yza Tassyklama {formatPhoneNumber(pendingVerification.phone)} belgisine iberilen 6 sanly tassyklama koduny giriziň ( {symbol || (isFocused ? : null)} )} onSubmitEditing={handleVerifyWrapper} />