'use client'; import { useRouter } from '@/shared/config/i18n/navigation'; import formatPhone from '@/shared/lib/formatPhone'; import { Input } from '@/shared/ui/input'; import { ArrowRight, Check, Lock, Phone } from 'lucide-react'; import { useEffect, useRef, useState } from 'react'; type Step = 'phone' | 'otp'; const Login = () => { const [step, setStep] = useState('phone'); const [phoneNumber, setPhoneNumber] = useState('+998'); const [otp, setOtp] = useState(['', '', '', '', '', '']); const [isLoading, setIsLoading] = useState(false); const [error, setError] = useState(''); const [countdown, setCountdown] = useState(60); const [canResend, setCanResend] = useState(false); const router = useRouter(); const otpInputs = useRef>([]); /* Countdown */ useEffect(() => { if (step === 'otp' && countdown > 0) { const timer = setTimeout(() => setCountdown((c) => c - 1), 1000); return () => clearTimeout(timer); } if (countdown === 0) { setCanResend(true); } }, [countdown, step]); /* Phone submit */ const handlePhoneSubmit = (): void => { setError(''); if (phoneNumber.length < 9) { setError("Telefon raqamni to'liq kiriting"); return; } setIsLoading(true); setTimeout(() => { setIsLoading(false); setStep('otp'); setCountdown(60); setCanResend(false); }, 1500); }; /* OTP change */ const handleOtpChange = (index: number, value: string): void => { if (value && !/^\d$/.test(value)) return; const newOtp = [...otp]; newOtp[index] = value; setOtp(newOtp); if (value && index < 5) { otpInputs.current[index + 1]?.focus(); } if (newOtp.every((d) => d !== '') && index === 5) { handleOtpSubmit(newOtp); } }; /* OTP keydown */ const handleOtpKeyDown = ( index: number, e: React.KeyboardEvent, ): void => { if (e.key === 'Backspace' && !otp[index] && index > 0) { otpInputs.current[index - 1]?.focus(); } }; /* OTP paste */ const handleOtpPaste = (e: React.ClipboardEvent): void => { e.preventDefault(); const pasted = e.clipboardData.getData('text').slice(0, 6); if (!/^\d+$/.test(pasted)) return; const newOtp = pasted.split(''); setOtp([...newOtp, ...Array(6 - newOtp.length).fill('')]); const lastIndex = Math.min(newOtp.length - 1, 5); otpInputs.current[lastIndex]?.focus(); if (pasted.length === 6) { setTimeout(() => handleOtpSubmit(newOtp), 100); } }; const handleOtpSubmit = (otpArray: string[] = otp): void => { setError(''); const otpCode = otpArray.join(''); if (otpCode.length < 6) { setError("Kodni to'liq kiriting"); return; } setIsLoading(true); setTimeout(() => { setIsLoading(false); if (otpCode === '123456') { localStorage.setItem('user', 'true'); router.push('/'); } else { setError("Noto'g'ri kod. Qayta urinib ko'ring."); setOtp(['', '', '', '', '', '']); otpInputs.current[0]?.focus(); } }, 1500); }; /* Resend */ const handleResendOtp = (): void => { if (!canResend) return; setIsLoading(true); setTimeout(() => { setIsLoading(false); setCountdown(60); setCanResend(false); setOtp(['', '', '', '', '', '']); otpInputs.current[0]?.focus(); alert('Yangi kod yuborildi!'); }, 1000); }; const handleChangeNumber = (): void => { setStep('phone'); setPhoneNumber(''); setOtp(['', '', '', '', '', '']); setError(''); setCountdown(60); setCanResend(false); }; return (
{/* Header */}
{step === 'phone' ? ( ) : ( )}

{step === 'phone' ? 'Xush kelibsiz!' : 'Kodni tasdiqlang'}

{step === 'phone' ? 'Telefon raqamingizni kiriting' : `${phoneNumber} raqamiga yuborilgan kodni kiriting`}

{/* Form */}
{step === 'phone' ? ( // Phone Number Step
{ const value = e.target.value.replace(/\D/g, ''); setPhoneNumber(value); setError(''); }} placeholder="+998 90 123-45-67" maxLength={17} className="w-full pl-12 pr-4 py-4 h-12 border-2 border-gray-300 rounded-xl focus:outline-none focus:border-blue-500 transition text-lg" />
{error &&

{error}

}

Davom etish orqali siz bizning{' '} Foydalanish shartlari {' '} va{' '} Maxfiylik siyosati ga rozilik bildirasiz

) : ( // OTP Step
{otp.map((digit, index) => ( { otpInputs.current[index] = el; }} type="text" inputMode="numeric" maxLength={1} value={digit} onChange={(e) => handleOtpChange(index, e.target.value)} onKeyDown={(e) => handleOtpKeyDown(index, e)} className="w-12 h-14 text-center text-2xl font-bold border-2 border-gray-300 rounded-lg focus:outline-none focus:border-blue-500 transition" /> ))}
{error && (
{error}
)} {/* Resend OTP */}
{canResend ? ( ) : (

Kodni qayta yuborish ({countdown}s)

)}
{/* Change Number */} {/* Demo info */}

Demo uchun: {`Kod sifatida "123456" kiriting`}

)}
); }; export default Login;