"use client" import { useState, useEffect, Suspense } from "react" import { useSearchParams } from "next/navigation" import { Card, CardContent } from "@/components/ui/card" import { Badge } from "@/components/ui/badge" import { Button } from "@/components/ui/button" import { Input } from "@/components/ui/input" import { formatPence } from "@/lib/utils" import { Search, Loader2, Download, RefreshCw, ArrowLeft } from "lucide-react" import Link from "next/link" const statusColors: Record = { new: "secondary", initiated: "warning", paid: "success", overdue: "destructive", cancelled: "outline", } const statusLabels: Record = { new: "New", initiated: "Initiated", paid: "Paid", overdue: "Overdue", cancelled: "Cancelled", } const railLabels: Record = { bank: "Bank Transfer", gocardless: "Direct Debit", card: "Card", fpx: "FPX", } interface PledgeRow { id: string reference: string amountPence: number status: string rail: string donorName: string | null donorEmail: string | null donorPhone: string | null giftAid: boolean eventName: string source: string | null createdAt: string paidAt: string | null } function PledgesContent() { const searchParams = useSearchParams() const eventId = searchParams.get("event") const [pledges, setPledges] = useState([]) const [loading, setLoading] = useState(true) const [search, setSearch] = useState("") const [statusFilter, setStatusFilter] = useState("all") const [updating, setUpdating] = useState(null) const [eventName, setEventName] = useState(null) const fetchPledges = () => { const url = eventId ? `/api/dashboard?eventId=${eventId}` : "/api/dashboard" fetch(url, { headers: { "x-org-id": "demo" } }) .then((r) => r.json()) .then((data) => { if (data.pledges) { setPledges(data.pledges) if (eventId && data.pledges.length > 0) { setEventName(data.pledges[0].eventName) } } }) .catch(() => {}) .finally(() => setLoading(false)) } useEffect(() => { fetchPledges() const interval = setInterval(fetchPledges, 15000) return () => clearInterval(interval) }, [eventId]) const handleStatusChange = async (pledgeId: string, newStatus: string) => { setUpdating(pledgeId) try { const res = await fetch(`/api/pledges/${pledgeId}`, { method: "PATCH", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ status: newStatus }), }) if (res.ok) { setPledges((prev) => prev.map((p) => p.id === pledgeId ? { ...p, status: newStatus, paidAt: newStatus === "paid" ? new Date().toISOString() : p.paidAt } : p ) ) } } catch {} setUpdating(null) } const filtered = pledges.filter((p) => { const matchSearch = !search || p.reference.toLowerCase().includes(search.toLowerCase()) || p.donorName?.toLowerCase().includes(search.toLowerCase()) || p.donorEmail?.toLowerCase().includes(search.toLowerCase()) || p.donorPhone?.includes(search) const matchStatus = statusFilter === "all" || p.status === statusFilter return matchSearch && matchStatus }) const statusCounts = pledges.reduce((acc, p) => { acc[p.status] = (acc[p.status] || 0) + 1 return acc }, {} as Record) if (loading) { return (
) } return (
{eventId && ( Back to Events )}

{eventName ? `${eventName} — Pledges` : "All Pledges"}

{pledges.length} pledge{pledges.length !== 1 ? "s" : ""} totalling{" "} {formatPence(pledges.reduce((s, p) => s + p.amountPence, 0))}

{/* Filters */}
setSearch(e.target.value)} className="pl-10" />
{["all", "new", "initiated", "paid", "overdue", "cancelled"].map((s) => ( ))}
{/* Pledges list */} {filtered.length === 0 ? (

{search ? "No pledges match your search." : "No pledges yet. Share a QR code to get started!"}

) : (
{filtered.map((p) => (
{p.reference} {statusLabels[p.status]} {railLabels[p.rail] || p.rail} {p.giftAid && ( Gift Aid ✓ )}

{p.donorName || "Anonymous"}

{[p.donorEmail, p.donorPhone].filter(Boolean).join(" · ") || "No contact info"}

{p.eventName}{p.source ? ` · ${p.source}` : ""}

{formatPence(p.amountPence)}

{new Date(p.createdAt).toLocaleDateString("en-GB", { day: "numeric", month: "short", year: "numeric", })}

{p.paidAt && (

Paid {new Date(p.paidAt).toLocaleDateString("en-GB", { day: "numeric", month: "short" })}

)}
{p.status !== "paid" && p.status !== "cancelled" && (
{p.status === "new" && ( )}
)}
))}
)}
) } export default function PledgesPage() { return ( }> ) }