"use client"; import React, { useEffect, useState } from "react"; import { HeartHandshake, X } from "lucide-react"; import { apiFetch } from "@/lib/api"; import { useAuth } from "@/hooks/useAuth"; import { errorMessage } from "@/lib/utils"; import { useOnlinePayments } from "@/lib/onlinePayments"; import { ModalOverlay } from "@/components/shared/ModalOverlay"; type Sponsorship = { enabled: boolean; open?: boolean; spotPrice?: number | null; showProgress?: boolean; goalSpots?: number | null; spotsFunded?: number | null; sponsors?: number | null }; const inputCls = "w-full border rounded-lg px-3 py-2 text-sm focus:outline-none focus:ring-2 focus:ring-brand-400"; const rand = (n: number) => `R${n.toLocaleString("en-ZA", { minimumFractionDigits: n % 1 ? 2 : 0, maximumFractionDigits: 2 })}`; /** * "Sponsor a spot": progress towards the event's sponsorship goal, and a form to fund * one or more spots (or any amount) for people who can't afford to come. Payment is * online; the money lands in the event's sponsorship pool. */ export function SponsorSpot({ eventId, eventTitle }: { eventId: string; eventTitle: string }) { const { token, user } = useAuth(); const [data, setData] = useState(null); const [open, setOpen] = useState(false); const [mode, setMode] = useState<"spots" | "amount">("spots"); const [spots, setSpots] = useState(1); const [amount, setAmount] = useState(""); const [anonymous, setAnonymous] = useState(false); const [sponsorName, setSponsorName] = useState(""); const [message, setMessage] = useState(""); const [guest, setGuest] = useState({ name: "", email: "" }); const [busy, setBusy] = useState(false); const [error, setError] = useState(null); const [returned, setReturned] = useState(null); const online = useOnlinePayments(); useEffect(() => { apiFetch(`/api/sponsorships/event/${encodeURIComponent(eventId)}`).then(setData).catch(() => setData(null)); try { const s = new URLSearchParams(window.location.search).get("sponsored"); if (s) setReturned(s); } catch { /* no window */ } }, [eventId]); // Sponsoring is paid online, so it's offered only when online payment is possible. if (!data?.enabled || (!online.available && !returned)) return null; const price = data.spotPrice || null; const loggedIn = !!(token && user); const total = mode === "spots" && price ? spots * price : Number(amount) || 0; const goal = data.goalSpots || 0; const funded = data.spotsFunded ?? 0; const submit = async (e: React.FormEvent) => { e.preventDefault(); setBusy(true); setError(null); try { const res = await apiFetch<{ redirectUrl: string }>("/api/sponsorships/checkout", { method: "POST", ...(loggedIn ? { authToken: token! } : {}), body: { eventId, ...(mode === "spots" && price ? { spots } : { amount: Number(amount) }), anonymous, ...(!anonymous && sponsorName.trim() ? { sponsorName: sponsorName.trim() } : {}), ...(message.trim() ? { message: message.trim() } : {}), ...(!loggedIn ? { name: guest.name, email: guest.email } : {}), }, }); window.location.href = res.redirectUrl; } catch (err) { setError(errorMessage(err) || "Couldn't start the payment"); setBusy(false); } }; return (
{returned === "1" &&
Thank you for sponsoring! Your receipt is on its way.
} {returned === "failed" &&
The payment didn't go through. You can try again.
} {data.showProgress && price && (goal > 0 || funded > 0) && (
{goal > 0 ? `${Math.min(funded, goal)} of ${goal} spots sponsored` : `${funded} spot${funded === 1 ? "" : "s"} sponsored`} {data.sponsors ? {data.sponsors} sponsor{data.sponsors === 1 ? "" : "s"} : null}
{goal > 0 && (
)}
)} {data.open && ( )} {open && ( setOpen(false)} closeOnBackdrop className="fixed inset-0 bg-black/40 flex items-center justify-center z-50">

Help someone come to {eventTitle} who couldn't otherwise afford it. Your gift goes into a sponsorship pool that the organisers use to cover places.

{price ? (
) : null} {mode === "spots" && price ? (
setSpots(Math.max(1, Math.min(100, parseInt(e.target.value, 10) || 1)))} aria-label="Number of spots" /> spot{spots === 1 ? "" : "s"} Ă— {rand(price)} = {rand(spots * price)}
) : (
setAmount(e.target.value)} placeholder="e.g. 200" required />

Minimum R15.

)} {!loggedIn && (
setGuest({ ...guest, name: e.target.value })} required /> setGuest({ ...guest, email: e.target.value })} required />
)} {!anonymous && ( setSponsorName(e.target.value)} maxLength={100} /> )}