"use client"; import { Queries } from "@/api/queries"; import { useState, FormEvent } from "react"; import { useRouter } from "next/navigation"; import { useLotteryAuth } from "@/store/useLotteryAuth"; const LotteryAuthForm = () => { const [phone, setPhone] = useState(""); const [code, setCode] = useState(""); const [error, setError] = useState(null); const [isLoading, setIsLoading] = useState(false); const router = useRouter(); const setAuth = useLotteryAuth((state) => state.setAuth); const validatePhone = (value: string) => { const phoneRegex = /^993\d{8}$/; const isValid = phoneRegex.test(value); return isValid; }; const validateCode = (value: string) => { const codeRegex = /^.+-\d{10}$/; // Any characters before "-", exactly 10 digits after const isValid = codeRegex.test(value); return isValid; }; const handleSubmit = async (e: FormEvent) => { e.preventDefault(); setError(null); if (!validatePhone(phone)) { setError("Telefon belgisi nädogry"); return; } if (!validateCode(code)) { setError("Açar nädogry"); return; } setIsLoading(true); try { const response = await Queries.authenticateLottery(phone, code); if (response.errorMessage) { setError(response.errorMessage); } else { localStorage.setItem("lotteryPhone", phone); localStorage.setItem("lotteryCode", code); setAuth(response, phone, code); router.replace("/lottery"); } } catch (err) { console.error("Authentication error:", err); setError("Telefon belgisi ýa-da açar nädogry"); } finally { setIsLoading(false); } }; const handlePhoneChange = (e: React.ChangeEvent) => { const value = e.target.value.replace(/\\D/g, ""); if (value.length <= 11) { setPhone(value); } }; const handleCodeChange = (e: React.ChangeEvent) => { const value = e.target.value; setCode(value); // if (value.length <= 12) { // } }; return (

Giriş

{error && (

{error}

)}
); }; export default LotteryAuthForm;