turkmentv_front/components/lottery/auth/LotteryAuthForm.tsx

117 lines
3.6 KiB
TypeScript
Raw Normal View History

2024-12-29 18:02:23 +00:00
"use client";
2024-12-23 19:13:36 +00:00
2024-12-29 18:02:23 +00:00
import { Queries } from "@/api/queries";
import { useState, FormEvent } from "react";
import { useRouter } from "next/navigation";
import { useLotteryAuth } from "@/store/useLotteryAuth";
2024-12-23 19:13:36 +00:00
const LotteryAuthForm = () => {
2024-12-29 18:02:23 +00:00
const [phone, setPhone] = useState("");
const [code, setCode] = useState("");
2024-12-23 19:13:36 +00:00
const [error, setError] = useState<string | null>(null);
const [isLoading, setIsLoading] = useState(false);
const router = useRouter();
const setAuth = useLotteryAuth((state) => state.setAuth);
const validatePhone = (value: string) => {
const phoneRegex = /^99363\d{6}$/;
return phoneRegex.test(value);
};
const validateCode = (value: string) => {
const codeRegex = /^\d-\d{10}$/;
return codeRegex.test(value);
};
const handleSubmit = async (e: FormEvent) => {
e.preventDefault();
setError(null);
if (!validatePhone(phone)) {
2024-12-29 18:02:23 +00:00
setError("Telefon belgisi nädogry formatda");
2024-12-23 19:13:36 +00:00
return;
}
if (!validateCode(code)) {
2024-12-29 18:02:23 +00:00
setError("Kod nädogry formatda");
2024-12-23 19:13:36 +00:00
return;
}
setIsLoading(true);
try {
const response = await Queries.authenticateLottery(phone, code);
2024-12-27 15:23:39 +00:00
setAuth(response, phone, code);
2024-12-29 18:02:23 +00:00
router.replace("/lottery");
2024-12-23 19:13:36 +00:00
} catch (err) {
2024-12-29 18:02:23 +00:00
console.error("Authentication error:", err);
setError("Telefon belgisi ýa-da kod nädogry");
2024-12-23 19:13:36 +00:00
} finally {
setIsLoading(false);
}
};
const handlePhoneChange = (e: React.ChangeEvent<HTMLInputElement>) => {
2024-12-29 18:02:23 +00:00
const value = e.target.value.replace(/\D/g, ""); // Remove non-digits
2024-12-23 19:13:36 +00:00
if (value.length <= 11) {
// Limit to 11 digits (99363 + 6 digits)
setPhone(value);
}
};
const handleCodeChange = (e: React.ChangeEvent<HTMLInputElement>) => {
const value = e.target.value;
if (value.length <= 12) {
// Limit to 12 characters (X-XXXXXXXXXX)
setCode(value);
}
};
return (
<form
onSubmit={handleSubmit}
2024-12-29 18:02:23 +00:00
className="bg-lightSurfaceContainer rounded-[24px] md:p-[40px] sm:p-[24px] p-[16px] w-[530px] flex flex-col gap-[24px]"
>
<h1 className="md:text-display3 sm:text-[32px] text-[24px] font-[500] md:leading-display3">
Lotereýa giriş
</h1>
2024-12-23 19:13:36 +00:00
<div className="flex flex-col gap-[16px]">
<div className="flex flex-col gap-[8px]">
<input
type="tel"
value={phone}
onChange={handlePhoneChange}
className="px-[16px] py-[12px] bg-lightPrimaryContainer rounded-[12px] outline-none text-lightOnSurfaceVariant text-textSmall leading-textSmall"
placeholder="99363XXXXXX"
required
/>
</div>
<div className="flex flex-col gap-[8px]">
<input
type="text"
value={code}
onChange={handleCodeChange}
className="px-[16px] py-[12px] bg-lightPrimaryContainer rounded-[12px] outline-none text-lightOnSurfaceVariant text-textSmall leading-textSmall"
2024-12-29 18:02:23 +00:00
placeholder="5-0105639808"
2024-12-23 19:13:36 +00:00
required
/>
</div>
2024-12-29 18:02:23 +00:00
{error && (
<p className="text-lightError text-textSmall leading-textSmall">
{error}
</p>
)}
2024-12-23 19:13:36 +00:00
</div>
<button
type="submit"
disabled={isLoading || !phone || !code}
2024-12-29 18:02:23 +00:00
className="sm:text-textLarge sm:leading-textLarge text-[16px] leading-[24px] sm:py-[12px] py-[8px] w-full flex justify-center items-center rounded-[12px] bg-lightPrimary font-medium text-lightOnPrimary disabled:opacity-50"
>
{isLoading ? "Ýüklenilýär..." : "Giriş"}
2024-12-23 19:13:36 +00:00
</button>
</form>
);
};
export default LotteryAuthForm;