'use client'; import { zodResolver } from '@hookform/resolvers/zod'; import { type RouteProp, useNavigation, useRoute, } from '@react-navigation/native'; import type { NativeStackNavigationProp } from '@react-navigation/native-stack'; import { useMutation, useQuery } from '@tanstack/react-query'; import { authApi } from 'api/auth'; import { registerPayload } from 'api/auth/type'; import { Branch, branchApi } from 'api/branch'; import AppText from 'components/AppText'; import formatPhone from 'helpers/formatPhone'; import { useEffect, useRef, useState } from 'react'; import { Controller, useForm } from 'react-hook-form'; import { useTranslation } from 'react-i18next'; import { ActivityIndicator, Animated, ImageBackground, Keyboard, KeyboardAvoidingView, ScrollView, TextInput, TouchableOpacity, View, } from 'react-native'; import { SafeAreaView } from 'react-native-safe-area-context'; import Logo from 'screens/../../assets/bootsplash/logo_512.png'; import { FirstStepFormType, FirstStepSchema, } from 'screens/auth/registeration/lib/form'; import LanguageSelector from 'screens/auth/select-language/SelectLang'; import ArrowDown from 'svg/ArrowDown'; import ArrowLeft from 'svg/ArrowLeft'; import ArrowUp from 'svg/ArrowUp'; import Check from 'svg/Check'; import { RootStackParamList } from 'types/types'; import { useUserStore } from '../lib/userstore'; import { RegisterStyle } from './styled'; type LoginScreenNavigationProp = NativeStackNavigationProp< RootStackParamList, 'Login' >; const recommended = [ { label: 'Tanishim orqali', value: 'FRIEND' }, { label: 'Telegram orqali', value: 'TELEGRAM' }, { label: 'Instagram orqali', value: 'INSTAGRAM' }, { label: 'Facebook orqali', value: 'FACEBOOK' }, ]; const FirstStep = ({ onNext }: { onNext: () => void }) => { const { t } = useTranslation(); const [filialDropdownVisible, setFilialDropdownVisible] = useState(false); const [error, setError] = useState(); const { setUser } = useUserStore(state => state); const { data: branchList } = useQuery({ queryKey: ['branchList'], queryFn: branchApi.branchList, }); const { mutate, isPending } = useMutation({ mutationFn: (payload: registerPayload) => authApi.register(payload), onSuccess: res => { onNext(); }, onError: err => { setError('Xatolik yuz berdi'); }, }); const [recommendedDropdownVisible, setRecommendedDropdownVisible] = useState(false); const [termsAccepted, setTermsAccepted] = useState(false); const [checkboxAnimation] = useState(new Animated.Value(0)); const navigation = useNavigation(); const [rawPhone, setRawPhone] = useState('+998'); const route = useRoute>(); const { control, handleSubmit, setValue, formState: { errors }, } = useForm({ resolver: zodResolver(FirstStepSchema), defaultValues: { firstName: '', address: '', lastName: '', recommend: '', }, }); // 🔑 Input ref'lar const firstNameRef = useRef(null); const lastNameRef = useRef(null); const phoneRef = useRef(null); const addressRef = useRef(null); const onSubmit = (data: FirstStepFormType) => { setUser({ firstName: data.firstName, lastName: data.lastName, phoneNumber: data.phoneNumber, }); mutate({ firstName: data.firstName, lastName: data.lastName, phoneNumber: data.phoneNumber, recommend: data.recommend, branchId: data.branchId, address: data.address, fcmToken: '', deviceId: '', deviceType: '', deviceName: '', }); }; useEffect(() => { if (route.params?.termsAccepted) { setTermsAccepted(true); Animated.spring(checkboxAnimation, { toValue: 1, useNativeDriver: false, tension: 100, friction: 8, }).start(); } }, [route.params]); const navigateToTerms = () => { navigation.navigate('TermsAndConditions'); setTermsAccepted(true); Animated.spring(checkboxAnimation, { toValue: 1, useNativeDriver: false, tension: 100, friction: 8, }).start(); }; const toggleCheckbox = () => { if (!termsAccepted) { navigateToTerms(); } else { setTermsAccepted(false); Animated.spring(checkboxAnimation, { toValue: 0, useNativeDriver: false, tension: 100, friction: 8, }).start(); } }; return ( navigation.navigate('select-auth')}> {t("Ro'yxatdan o'tish")} {/* Ism */} ( {t('Ism')} lastNameRef.current?.focus()} /> {errors.firstName && ( {t(errors.firstName.message || '')} )} )} /> {/* Familiya */} ( {t('Familiya')} phoneRef.current?.focus()} /> {errors.lastName && ( {t(errors.lastName.message || '')} )} )} /> {/* Telefon raqami */} { const formatted = formatPhone(rawPhone); return ( {t('Telefon raqami')} { const digits = text.replace(/\D/g, '').slice(0, 12); const full = digits.startsWith('998') ? digits : `998${digits}`; setRawPhone(full); onChange(full); }} style={RegisterStyle.input} placeholderTextColor="#D8DADC" maxLength={17} returnKeyType="next" onSubmitEditing={ () => setFilialDropdownVisible(true) // ❗ Branch select ochiladi } /> {errors.phoneNumber && ( {t(errors.phoneNumber.message || '')} )} ); }} /> {/* Filial (dropdown) */} ( {t('Filial')} { setFilialDropdownVisible(prev => !prev); Keyboard.dismiss(); }} > {branchList?.find(e => e.id === value)?.name || t('Filialni tanlang...')} {filialDropdownVisible ? ( ) : ( )} {filialDropdownVisible && ( {branchList && branchList.map((item: Branch) => ( { setValue('branchId', item.id); setFilialDropdownVisible(false); // keyingi inputga focus setTimeout( () => addressRef.current?.focus(), 200, ); }} > {item.name} ))} )} {errors.branchId && ( {t(errors.branchId.message || '')} )} )} /> {/* Manzil */} ( {t('Manzilingizni kiriting')} setRecommendedDropdownVisible(true) // ❗ recommend select ochiladi } /> {errors.address && ( {t(errors.address.message || '')} )} )} /> {/* Recommend (dropdown) */} ( {t('Bizni qaerdan topdingiz?')} setRecommendedDropdownVisible(prev => !prev) } > {t( recommended.find(e => e.value === value)?.label || 'Bizni kim tavsiya qildi...', )} {recommendedDropdownVisible ? ( ) : ( )} {recommendedDropdownVisible && ( {recommended.map((item, index) => ( { setValue('recommend', item.value); setRecommendedDropdownVisible(false); }} > {t(item.label)} ))} )} {errors.recommend && ( {t(errors.recommend.message || '')} )} {error && ( {t(error)} )} )} /> {/* Terms */} {termsAccepted && ( )} {t('Foydalanish shartlari')} {t('bilan tanishib chiqdim!')} {isPending ? ( ) : ( {t('Davom etish')} )} ); }; export default FirstStep;