"use client"; import { useEffect, useState } from "react"; import { useLotteryAuth } from "@/store/useLotteryAuth"; import ProtectedRoute from "@/components/lottery/auth/ProtectedRoute"; import LotteryHeader from "@/components/lottery/LotteryHeader"; 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"; const LotteryPage = () => { const { lotteryData, setAuth } = useLotteryAuth(); const [status, setStatus] = useState<"not-started" | "started" | "ended">( "not-started" ); const router = useRouter(); // ✅ Fetch fresh data on page load useEffect(() => { const phone = localStorage.getItem("lotteryPhone"); const code = localStorage.getItem("lotteryCode"); if (phone && code) { Queries.authenticateLottery(phone, code) .then((response) => { if (response.errorMessage) { // If authentication fails, redirect to the auth page router.replace("/lottery/auth"); } else { // ✅ Set the authenticated state setAuth(response, phone, code); } }) .catch((err) => { console.error("Failed to fetch lottery data:", err); console.error("Authentication failed:", err); router.replace("/lottery/auth"); }); } }, [setAuth]); if (!lotteryData?.errorMessage) { router.replace("/lottery/auth"); } return (
{lotteryData && (
{status === "not-started" ? (
) : null}
)}
{lotteryData && (status === "ended" || status === "started") && ( )}
Çykmak
); }; export default LotteryPage;