"use client"; import React, { useEffect, useState } from "react"; import { useAuth } from "@/hooks/useAuth"; import { apiFetch } from "@/lib/api"; import { useDismissingState } from "@/hooks/useDismissingState"; export default function DonatePage() { const { token } = useAuth(); const [events, setEvents] = useState([]); const [eventId, setEventId] = useState(""); const [amount, setAmount] = useState(""); const [loading, setLoading] = useState(false); const [error, setError] = useDismissingState(null); const [info, setInfo] = useDismissingState(null); useEffect(() => { (async () => { try { const evs = await apiFetch("/api/events"); setEvents(evs); if (evs.length > 0) setEventId(evs[0].id); } catch (e: any) { setError(e?.message || "Failed to load events"); } })(); }, []); const submit = async () => { if (!token) { setError("Please login"); return; } const amt = parseFloat(amount); if (!(amt > 0)) { setError("Enter a valid amount"); return; } if (amt < 15) { setError("Minimum donation is R15"); return; } if (!eventId) { setError("Select an event"); return; } try { setError(null); setInfo(null); setLoading(true); const res = await apiFetch<{ redirectUrl: string }>("/api/payments/yoco-checkout", { method: "POST", body: { eventId, amount: amt, successUrl: window.location.origin + "/payment/success", cancelUrl: window.location.origin + "/payment/cancel", failureUrl: window.location.origin + "/payment/failure", }, authToken: token, }); setInfo("Redirecting to payment..."); window.location.href = res.redirectUrl; } catch (e: any) { setError(e?.message || "Failed to start donation checkout"); } finally { setLoading(false); } }; return (

Make a donation

{error &&

{error}

} {info &&

{info}

} setAmount(e.target.value)} className="w-full border rounded px-3 py-2 mb-1" />

Minimum donation is R15.

); }