turkmentv_front/components/lottery/auth/LotteryAuthForm.tsx

116 lines
4.0 KiB
TypeScript
Raw Normal View History

2025-01-24 13:20:37 +00:00
"use client";
2024-12-23 19:13:36 +00:00
import { useState } from "react";
2025-01-24 13:20:37 +00:00
import { useRouter } from "next/navigation";
import { useForm } from "react-hook-form";
import z from "zod";
import { zodResolver } from "@hookform/resolvers/zod";
import { Form, FormField, FormItem, FormMessage } from "@/components/ui/form";
import { FormControl, FormLabel } from "@mui/material";
import { authenticateLottery } from "@/api";
const lotteryAuthSchema = z.object({
phoneNumber: z.string().regex(/^993\d{8}$/, {
message: "Dogry telefon belgisini girizin",
}),
key: z.string().regex(/^.+-\d{10}$/, {
message: "Dogry acar sozuni girizin",
}),
});
2024-12-23 19:13:36 +00:00
const LotteryAuthForm = () => {
const form = useForm<z.infer<typeof lotteryAuthSchema>>({
resolver: zodResolver(lotteryAuthSchema),
});
2024-12-23 19:13:36 +00:00
const [error, setError] = useState<string | null>(null);
const [isLoading, setIsLoading] = useState(false);
const router = useRouter();
async function onSubmit(values: z.infer<typeof lotteryAuthSchema>) {
const { phoneNumber, key } = values;
2024-12-23 19:13:36 +00:00
setIsLoading(true);
try {
const response = await authenticateLottery(phoneNumber, key);
2025-01-10 12:31:34 +00:00
if (response.errorMessage) {
2025-01-24 13:20:37 +00:00
setError(response.errorMessage);
2025-01-08 13:48:04 +00:00
} else {
2025-02-11 12:34:39 +00:00
document.cookie = `phoneNumber=${phoneNumber};path=/;max-age=3600;`;
document.cookie = `key=${key};path=/;max-age=3600;`;
router.replace("/cekilis");
2025-01-08 13:48:04 +00:00
}
2024-12-23 19:13:36 +00:00
} catch (err) {
2025-01-24 13:20:37 +00:00
setError("Telefon belgisi ýa-da açar nädogry");
2024-12-23 19:13:36 +00:00
} finally {
setIsLoading(false);
}
}
2024-12-23 19:13:36 +00:00
return (
<Form {...form}>
<form
onSubmit={form.handleSubmit(onSubmit)}
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">
Giriş
</h1>
<div className="flex flex-col gap-[16px]">
<FormField
control={form.control}
name="phoneNumber"
render={({ field }) => (
<FormItem className="flex flex-col gap-[8px]">
<FormLabel className="font-base-medium text-lightOnSurface">
Telefon
</FormLabel>
<FormControl>
<input
{...field}
className="px-[16px] py-[12px] bg-lightPrimaryContainer rounded-[12px] outline-none text-lightOnSurfaceVariant text-textSmall leading-textSmall"
placeholder="99363XXXXXX"
/>
</FormControl>
<FormMessage className="text-red-500" />
</FormItem>
)}
2024-12-23 19:13:36 +00:00
/>
<FormField
control={form.control}
name="key"
render={({ field }) => (
<FormItem className="flex flex-col gap-[8px]">
<FormLabel className="font-base-medium text-lightOnSurface">
Açar
</FormLabel>
<FormControl>
<input
{...field}
className="px-[16px] py-[12px] bg-lightPrimaryContainer rounded-[12px] outline-none text-lightOnSurfaceVariant text-textSmall leading-textSmall"
placeholder="C5-0105639808"
/>
</FormControl>
<FormMessage className="text-red-500" />
</FormItem>
)}
2024-12-23 19:13:36 +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"
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ş"}
</button>
</form>
</Form>
2024-12-23 19:13:36 +00:00
);
};
export default LotteryAuthForm;