"use client"; import React, { useCallback, useEffect, useState } from "react"; import { Ticket, CheckCircle2 } from "lucide-react"; import { useSearchParams } from "next/navigation"; import { useAuth } from "@/hooks/useAuth"; import { apiFetch } from "@/lib/api"; import { errorMessage } from "@/lib/utils"; import { formatDateTimeRange } from "@/lib/date"; import type { SeasonPassPurchase, SeasonPassView } from "@/types"; const rand = (n: number) => `R ${n.toFixed(2)}`; type BuyResult = { purchaseId: string; total: number; paid: boolean; redirectUrl: string | null; message: string | null }; /** * The season passes on a series page: what each includes and costs, Buy (registers you for every * event on it, then one payment), and your own passes for this series with "Pay now" when unpaid. */ export function SeasonPassOffers({ slug, passes: initial }: { slug: string; passes: SeasonPassView[] }) { const { user, token, loading } = useAuth(); const params = useSearchParams(); const [qty, setQty] = useState>({}); const [busy, setBusy] = useState(null); const [error, setError] = useState(null); const [notice, setNotice] = useState(null); const [mine, setMine] = useState([]); // The page is cached for everyone; ask for the passes as they are now. const [passes, setPasses] = useState(initial); useEffect(() => { apiFetch<{ passes?: SeasonPassView[] }>(`/api/series/by-slug/${encodeURIComponent(slug)}`).then(s => setPasses(s.passes || [])).catch(() => {}); }, [slug]); const loadMine = useCallback(async () => { if (!token) return; const list = await apiFetch("/api/season-passes/mine", { authToken: token }).catch(() => []); setMine(list.filter(p => p.pass.series.slug === slug && p.status !== "cancelled")); }, [token, slug]); useEffect(() => { loadMine(); }, [loadMine]); const returned = params.get("pass"); const go = (res: BuyResult | { redirectUrl: string | null; message: string | null }) => { if (res.redirectUrl) { window.location.href = res.redirectUrl; return; } setNotice(res.message || "Done."); loadMine(); }; const buy = async (p: SeasonPassView) => { setBusy(p.id); setError(null); setNotice(null); try { go(await apiFetch(`/api/season-passes/${p.id}/buy`, { method: "POST", authToken: token || undefined, body: { quantity: qty[p.id] || 1 } })); } catch (e) { setError(errorMessage(e) || "Couldn't buy the pass"); } finally { setBusy(null); } }; const pay = async (purchaseId: string) => { setBusy(purchaseId); setError(null); setNotice(null); try { go(await apiFetch<{ redirectUrl: string | null; message: string | null }>(`/api/season-passes/purchases/${purchaseId}/checkout`, { method: "POST", authToken: token || undefined })); } catch (e) { setError(errorMessage(e) || "Couldn't start the payment"); } finally { setBusy(null); } }; if (passes.length === 0 && mine.length === 0) return null; return (

Season passes

One payment, a ticket for every event on the pass.

{returned === "paid" &&
Thank you! Your tickets are on their way by email once the payment comes through.
} {(returned === "cancelled" || returned === "failed") &&
The payment didn't go through. Your places are kept — use "Pay now" below to try again.
} {notice &&
{notice}
} {error &&
{error}
} {mine.length > 0 && (
Your passes
    {mine.map(m => (
  • {m.quantity} × {m.pass.name} · {rand(m.total)} · {m.status === "paid" ? paid — tickets in My events : not paid yet} {m.status === "pending" && ( )}
  • ))}
)}
{passes.map(p => (

{p.name}

{p.description &&

{p.description}

}
{p.price === 0 ? "Free" : rand(p.price)}
{p.saving > 0 &&
save {rand(p.saving)}
}
    {p.items.map(i => (
  • {i.eventTitle} · {formatDateTimeRange(i.startDate, i.endDate)} · {i.ticket}
  • ))}
{p.unavailable ? (

{p.unavailable}

) : loading ? null : !user ? ( Log in to buy ) : (
)}
))}
); }