turkmentv_front/components/lottery/auth/LotteryAuthForm.tsx

138 lines
4.3 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}$/;
2025-01-10 12:31:34 +00:00
const isValid = phoneRegex.test(value);
console.log('Phone Input:', value);
console.log('Regex Check Result:', isValid);
return isValid;
2024-12-23 19:13:36 +00:00
};
const validateCode = (value: string) => {
2025-01-16 13:18:23 +00:00
const codeRegex = /^.+-\d{10}$/; // Any characters before "-", exactly 10 digits after
2025-01-10 12:31:34 +00:00
const isValid = codeRegex.test(value);
console.log('Code Input:', value);
console.log('Regex Check Result:', isValid);
return isValid;
2024-12-23 19:13:36 +00:00
};
const handleSubmit = async (e: FormEvent) => {
e.preventDefault();
setError(null);
2025-01-10 12:31:34 +00:00
console.log('Submitting Phone:', phone);
2024-12-23 19:13:36 +00:00
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);
2025-01-10 12:31:34 +00:00
if (response.errorMessage) {
2025-01-08 13:48:04 +00:00
setError('Telefon belgisi ýa-da açar nädogry');
} else {
2025-01-10 12:31:34 +00:00
localStorage.setItem('lotteryPhone', phone);
localStorage.setItem('lotteryCode', code);
setAuth(response, phone, code);
2025-01-08 13:48:04 +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-10 12:31:34 +00:00
const value = e.target.value.replace(/\\D/g, '');
2024-12-23 19:13:36 +00:00
if (value.length <= 11) {
setPhone(value);
}
};
const handleCodeChange = (e: React.ChangeEvent<HTMLInputElement>) => {
const value = e.target.value;
2025-01-16 13:18:23 +00:00
setCode(value);
// if (value.length <= 12) {
// }
2024-12-23 19:13:36 +00:00
};
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;