|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { useEffect, useState } from "react"; |
| 4 | +import { Invitation } from "@/types"; |
| 5 | +import { Button } from "@/components/ui/button"; |
| 6 | +import { Badge } from "@/components/ui/badge"; |
| 7 | +import { toast } from "sonner"; |
| 8 | + |
| 9 | +export function InvitationList({ refreshKey }: { refreshKey?: number }) { |
| 10 | + const [invitations, setInvitations] = useState<Invitation[]>([]); |
| 11 | + const [loading, setLoading] = useState(true); |
| 12 | + const [revoking, setRevoking] = useState<string | null>(null); |
| 13 | + |
| 14 | + useEffect(() => { |
| 15 | + setLoading(true); |
| 16 | + fetch("/api/invitations") |
| 17 | + .then((r) => (r.ok ? r.json() : [])) |
| 18 | + .then(setInvitations) |
| 19 | + .catch(console.error) |
| 20 | + .finally(() => setLoading(false)); |
| 21 | + }, [refreshKey]); |
| 22 | + |
| 23 | + const rescind = async (code: string, name: string) => { |
| 24 | + setRevoking(code); |
| 25 | + try { |
| 26 | + const res = await fetch(`/api/invitations/${code}`, { method: "DELETE" }); |
| 27 | + if (!res.ok) throw new Error("Failed"); |
| 28 | + setInvitations((prev) => prev.filter((i) => i.code !== code)); |
| 29 | + toast.success(`Invitation to ${name} rescinded.`); |
| 30 | + } catch { |
| 31 | + toast.error("Failed to rescind invitation."); |
| 32 | + } finally { |
| 33 | + setRevoking(null); |
| 34 | + } |
| 35 | + }; |
| 36 | + |
| 37 | + return ( |
| 38 | + <div> |
| 39 | + <h2 className="text-lg font-semibold mb-1">Sent invitations</h2> |
| 40 | + <p className="text-sm text-muted-foreground mb-4"> |
| 41 | + Pending invitations can be rescinded to invalidate the signup link. |
| 42 | + </p> |
| 43 | + {loading ? ( |
| 44 | + <p className="text-sm text-muted-foreground">Loading…</p> |
| 45 | + ) : invitations.length === 0 ? ( |
| 46 | + <p className="text-sm text-muted-foreground">No invitations sent yet.</p> |
| 47 | + ) : ( |
| 48 | + <div className="divide-y rounded-md border max-w-2xl"> |
| 49 | + {invitations.map((inv) => ( |
| 50 | + <div key={inv.code} className="flex items-center justify-between px-4 py-3 gap-4"> |
| 51 | + <div className="min-w-0"> |
| 52 | + <p className="font-medium truncate"> |
| 53 | + {inv.firstName} {inv.lastName} |
| 54 | + </p> |
| 55 | + <p className="text-sm text-muted-foreground truncate">{inv.email}</p> |
| 56 | + <p className="text-xs text-muted-foreground mt-0.5"> |
| 57 | + Sent {new Date(inv.sentAt).toLocaleDateString()} |
| 58 | + </p> |
| 59 | + </div> |
| 60 | + <div className="flex items-center gap-3 shrink-0"> |
| 61 | + {inv.usedAt ? ( |
| 62 | + <Badge variant="secondary">Accepted</Badge> |
| 63 | + ) : ( |
| 64 | + <> |
| 65 | + <Badge variant="outline">Pending</Badge> |
| 66 | + <button |
| 67 | + className="text-sm underline text-muted-foreground hover:text-foreground disabled:opacity-50" |
| 68 | + disabled={revoking === inv.code} |
| 69 | + onClick={() => rescind(inv.code, `${inv.firstName} ${inv.lastName}`)} |
| 70 | + > |
| 71 | + {revoking === inv.code ? "Cancelling…" : "Cancel"} |
| 72 | + </button> |
| 73 | + </> |
| 74 | + )} |
| 75 | + </div> |
| 76 | + </div> |
| 77 | + ))} |
| 78 | + </div> |
| 79 | + )} |
| 80 | + </div> |
| 81 | + ); |
| 82 | +} |
0 commit comments