turkmentv_front/app/(main)/lottery/page.tsx

115 lines
4.0 KiB
TypeScript
Raw Normal View History

2025-01-29 13:08:55 +00:00
"use client";
2024-12-23 19:13:36 +00:00
2025-01-29 13:08:55 +00:00
import { useEffect, useState } from "react";
import { useLotteryAuth } from "@/store/useLotteryAuth";
import ProtectedRoute from "@/components/lottery/auth/ProtectedRoute";
import LotteryHeader from "@/components/lottery/LotteryHeader";
2024-12-23 19:32:36 +00:00
2025-01-29 13:08:55 +00:00
import LotteryWinnersSection from "@/components/lottery/LotteryWinnersSection";
import LotteryRulesSection from "@/components/lottery/rules/LotteryRulesSection";
import LotteryCountDown from "@/components/lottery/countDown/LotteryCountDown";
import { Queries } from "@/api/queries";
import Link from "next/link";
import { useRouter } from "next/navigation";
2025-01-29 13:45:17 +00:00
import Loader from "@/components/Loader";
2024-12-23 19:13:36 +00:00
2024-12-23 19:32:36 +00:00
const LotteryPage = () => {
2025-01-10 12:31:34 +00:00
const { lotteryData, setAuth } = useLotteryAuth();
2025-01-29 13:08:55 +00:00
const [status, setStatus] = useState<"not-started" | "started" | "ended">(
"not-started"
);
2025-01-29 13:45:17 +00:00
const [isLoading, setIsLoading] = useState(true);
2025-01-29 13:08:55 +00:00
const router = useRouter();
2024-12-29 18:02:23 +00:00
2025-01-10 12:31:34 +00:00
useEffect(() => {
2025-01-29 13:45:17 +00:00
const checkAuth = async () => {
// ✅ Check credentials from localStorage
const phone = localStorage.getItem("lotteryPhone");
const code = localStorage.getItem("lotteryCode");
if (phone && code) {
try {
// ✅ Authenticate using stored credentials
const response = await Queries.authenticateLottery(phone, code);
2025-01-10 12:31:34 +00:00
2025-01-29 13:08:55 +00:00
if (response.errorMessage) {
// If authentication fails, redirect to the auth page
2025-01-29 13:49:28 +00:00
console.log("redirecting form lottery/");
2025-01-29 13:08:55 +00:00
router.replace("/lottery/auth");
} else {
// ✅ Set the authenticated state
setAuth(response, phone, code);
2025-01-29 13:45:17 +00:00
setIsLoading(false);
2025-01-29 13:08:55 +00:00
}
2025-01-29 13:45:17 +00:00
} catch (err) {
2025-01-29 13:08:55 +00:00
console.error("Authentication failed:", err);
router.replace("/lottery/auth");
2025-01-29 13:45:17 +00:00
}
} else {
// Redirect to the auth page if no credentials are found
router.replace("/lottery/auth");
}
};
checkAuth();
}, [router, setAuth]);
2025-01-10 12:31:34 +00:00
2025-01-29 13:49:28 +00:00
if (isLoading && !lotteryData?.data) {
2025-01-29 13:45:17 +00:00
<div className="flex w-full h-[90vh] justify-center items-center">
<Loader />
</div>;
}
2025-01-29 13:27:26 +00:00
2024-12-23 19:13:36 +00:00
return (
<ProtectedRoute>
2025-01-29 13:49:28 +00:00
{lotteryData?.data && (
<div className="flex flex-col md:gap-[128px] gap-[80px] font-roboto md:pt-[64px] sm:pt-[48px] pt-[40px] ms:pb-[128px] pb-[80px] text-lightOnSurface">
{lotteryData && (
<div className="flex flex-col sm:gap-[64px] gap-[40px]">
<LotteryHeader
title={lotteryData.data.title}
description={lotteryData.data.description}
image={lotteryData.data.image}
smsCode={lotteryData.data.sms_code}
startDate={lotteryData.data.start_time}
/>
2024-12-23 19:13:36 +00:00
2025-01-29 13:49:28 +00:00
{status === "not-started" ? (
<div className="container">
<LotteryCountDown
lotteryStatus={status}
setLotteryStatus={setStatus}
endDate={lotteryData.data.end_time}
startDate={lotteryData.data.start_time}
/>
</div>
) : null}
</div>
)}
2024-12-25 13:46:20 +00:00
2025-01-29 13:49:28 +00:00
<LotteryRulesSection />
2024-12-23 19:13:36 +00:00
2025-01-29 13:49:28 +00:00
<div className="flex flex-col gap-10">
{lotteryData && (status === "ended" || status === "started") && (
<LotteryWinnersSection lotteryStatus={status} />
)}
<div className="w-full">
<div className="container">
<Link
href="/lottery/auth"
className="sm:text-textLarge sm:leading-textLarge text-[16px] rounded-full leading-[24px] sm:py-[12px] py-[8px] w-full flex justify-center items-center border-2 border-lightPrimary hover:bg-lightPrimary font-medium text-lightPrimary hover:text-lightOnPrimary disabled:opacity-50 transition-all duration-300"
>
Çykmak
</Link>
</div>
2025-01-10 13:23:36 +00:00
</div>
</div>
</div>
2025-01-29 13:49:28 +00:00
)}
2024-12-23 19:13:36 +00:00
</ProtectedRoute>
);
};
2024-12-23 19:32:36 +00:00
export default LotteryPage;