turkmentv_front/components/lottery/auth/LotteryAuthForm.tsx

120 lines
3.8 KiB
TypeScript
Raw Normal View History

2025-01-08 13:34:28 +00:00
'use client';
2024-12-23 19:13:36 +00:00
2025-01-08 13:34:28 +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 = () => {
2025-01-08 13:34:28 +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) => {
2025-01-08 13:34:28 +00:00
const phoneRegex = /^993\d{8}$/;
2024-12-23 19:13:36 +00:00
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)) {
2025-01-08 13:34:28 +00:00
setError('Telefon belgisi nädogry');
2024-12-23 19:13:36 +00:00
return;
}
if (!validateCode(code)) {
2025-01-08 13:34:28 +00:00
setError('Açar nädogry');
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);
2025-01-08 13:34:28 +00:00
router.replace('/lottery');
2024-12-23 19:13:36 +00:00
} catch (err) {
2025-01-08 13:34:28 +00:00
console.error('Authentication error:', err);
setError('Telefon belgisi ýa-da açar nädogry');
2024-12-23 19:13:36 +00:00
} finally {
setIsLoading(false);
}
};
const handlePhoneChange = (e: React.ChangeEvent<HTMLInputElement>) => {
2025-01-08 13:34:28 +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) {
2025-01-08 13:34:28 +00:00
// Limit to 11 digits (993 + 8 digits)
2024-12-23 19:13:36 +00:00
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}
2025-01-08 13:34:28 +00:00
className="bg-lightSurfaceContainer rounded-[24px] md:p-[40px] sm:p-[24px] p-[16px] w-[530px] flex flex-col gap-[24px]">
2024-12-29 18:02:23 +00:00
<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]">
2025-01-08 13:34:28 +00:00
<label htmlFor="phone" className="font-base-medium text-lightOnSurface cursor-pointer">
Telefon
</label>
2024-12-23 19:13:36 +00:00
<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
2025-01-08 13:34:28 +00:00
id="phone"
2024-12-23 19:13:36 +00:00
/>
</div>
<div className="flex flex-col gap-[8px]">
2025-01-08 13:34:28 +00:00
<label htmlFor="code" className="font-base-medium text-lightOnSurface cursor-pointer">
Açar
</label>
2024-12-23 19:13:36 +00:00
<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
2025-01-08 13:34:28 +00:00
id="code"
2024-12-23 19:13:36 +00:00
/>
</div>
2025-01-08 13:34:28 +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}
2025-01-08 13:34:28 +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;