Archived
Replace `catch (e: any)` with `catch (e)` and read the message through a new errorMessage() helper. ApiError.data gets a real type, and useDismissingState's setter keeps one identity across renders. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
101 lines
4.7 KiB
TypeScript
101 lines
4.7 KiB
TypeScript
"use client";
|
|
import React, { useEffect, useState } from "react";
|
|
import { useParams } from "next/navigation";
|
|
import { Navbar } from "@/components/layout/Navbar";
|
|
import { Footer } from "@/components/layout/Footer";
|
|
import { apiFetch } from "@/lib/api";
|
|
import { formatDate } from "@/lib/date";
|
|
import { Ticket, CheckCircle2, XCircle } from "lucide-react";
|
|
import { errorMessage } from "@/lib/utils";
|
|
|
|
type Offer = {
|
|
status: "pending" | "accepted" | "declined" | "cancelled" | "expired";
|
|
toName: string;
|
|
fromName: string;
|
|
expiresAt: string;
|
|
option: string;
|
|
event: { id: string; title: string; startDate: string; location: string | null };
|
|
};
|
|
|
|
const STATUS_TEXT: Record<string, string> = {
|
|
accepted: "This ticket has already been accepted.",
|
|
declined: "This ticket offer was declined.",
|
|
cancelled: "This ticket offer was withdrawn.",
|
|
expired: "This ticket offer has expired.",
|
|
};
|
|
|
|
/** Public page the person a ticket is offered to opens from their email or WhatsApp. */
|
|
export default function TransferOfferPage() {
|
|
const params = useParams<{ token: string }>();
|
|
const token = params?.token;
|
|
const [offer, setOffer] = useState<Offer | null>(null);
|
|
const [loadError, setLoadError] = useState<string | null>(null);
|
|
const [busy, setBusy] = useState(false);
|
|
const [error, setError] = useState<string | null>(null);
|
|
const [done, setDone] = useState<{ ok: boolean; message: string } | null>(null);
|
|
|
|
useEffect(() => {
|
|
if (!token) return;
|
|
apiFetch<Offer>(`/api/tickets/transfer-offer/${encodeURIComponent(token)}`)
|
|
.then(setOffer)
|
|
.catch(e => setLoadError(e?.message || "This transfer link isn't valid."));
|
|
}, [token]);
|
|
|
|
const answer = async (action: "accept" | "decline") => {
|
|
setBusy(true);
|
|
setError(null);
|
|
try {
|
|
const r = await apiFetch<{ message: string }>(`/api/tickets/transfer-offer/${encodeURIComponent(token!)}/${action}`, { method: "POST" });
|
|
setDone({ ok: action === "accept", message: r.message });
|
|
} catch (e) {
|
|
setError(errorMessage(e) || "Something went wrong");
|
|
} finally {
|
|
setBusy(false);
|
|
}
|
|
};
|
|
|
|
return (
|
|
<div className="min-h-screen flex flex-col bg-gray-50">
|
|
<Navbar />
|
|
<main className="flex-1 max-w-xl w-full mx-auto p-4 sm:p-6">
|
|
<div className="flex items-center gap-3 mb-4">
|
|
<div className="w-10 h-10 rounded-xl bg-brand-50 flex items-center justify-center shrink-0">
|
|
<Ticket className="w-5 h-5 text-brand-600" />
|
|
</div>
|
|
<h1 className="text-2xl font-semibold text-gray-900">A ticket for you</h1>
|
|
</div>
|
|
{loadError && <div className="p-4 border rounded-xl bg-white text-sm text-gray-700">{loadError}</div>}
|
|
{done && (
|
|
<div className="p-5 border rounded-xl bg-white shadow-sm flex items-start gap-3">
|
|
{done.ok ? <CheckCircle2 className="w-6 h-6 text-green-600 shrink-0" /> : <XCircle className="w-6 h-6 text-gray-500 shrink-0" />}
|
|
<p className="text-sm text-gray-700">{done.message}</p>
|
|
</div>
|
|
)}
|
|
{offer && !done && (
|
|
<div className="p-5 border rounded-xl bg-white shadow-sm space-y-3">
|
|
<p className="text-sm text-gray-700">Hi {offer.toName.split(" ")[0]}, {offer.fromName || "someone"} would like to give you their ticket.</p>
|
|
<div className="rounded-lg bg-gray-50 p-3 text-sm">
|
|
<div className="font-semibold">{offer.event.title}</div>
|
|
<div className="text-gray-600">{formatDate(new Date(offer.event.startDate))}{offer.event.location ? ` · ${offer.event.location}` : ""}</div>
|
|
<div className="text-gray-600">{offer.option}</div>
|
|
</div>
|
|
{offer.status === "pending" ? (
|
|
<>
|
|
<p className="text-xs text-gray-500">Accepting sends you your own ticket and QR code. If you don't have an account with us yet, we'll create one and send you a link to activate it. This offer is open until {new Date(offer.expiresAt).toLocaleString()}.</p>
|
|
{error && <div className="p-2 rounded bg-red-50 text-red-800 text-sm">{error}</div>}
|
|
<div className="flex gap-2">
|
|
<button onClick={() => answer("accept")} disabled={busy} className="px-4 py-2 rounded-lg bg-brand-600 text-white hover:bg-brand-700 disabled:opacity-50 font-medium">{busy ? "Please wait…" : "Accept ticket"}</button>
|
|
<button onClick={() => answer("decline")} disabled={busy} className="px-4 py-2 rounded-lg border hover:bg-gray-50 disabled:opacity-50">Decline</button>
|
|
</div>
|
|
</>
|
|
) : (
|
|
<p className="text-sm text-gray-600">{STATUS_TEXT[offer.status]}</p>
|
|
)}
|
|
</div>
|
|
)}
|
|
</main>
|
|
<Footer />
|
|
</div>
|
|
);
|
|
}
|