"use client"; import { useCallback, useEffect, useState } from "react"; import { api, errorText } from "@/lib/api"; import { formatDate } from "@/lib/astro"; import type { AdminGift } from "@/lib/types"; const huf = (n: number) => `${n.toLocaleString("hu-HU")} Ft`; const STORNO: Record = { pending: "sztornó folyamatban", done: "sztornózva", failed: "sztornó hiba" }; /** The gifts with their payment, invoice and storno, and the refund button. */ export function AdminGifts() { const [gifts, setGifts] = useState(null); const [q, setQ] = useState(""); const [busy, setBusy] = useState(null); const [error, setError] = useState(null); const [done, setDone] = useState(null); const load = useCallback(async (search: string) => { try { const r = await api<{ gifts: AdminGift[] }>(`/admin/gifts${search ? `?q=${encodeURIComponent(search)}` : ""}`); setGifts(r.gifts); } catch (e) { setError(errorText(e)); } }, []); useEffect(() => { const t = setTimeout(() => load(q.trim()), 250); return () => clearTimeout(t); }, [q, load]); async function refund(g: AdminGift) { const late = g.within_withdrawal ? "" : "\n\nFigyelem: a 14 napos elállási idő már letelt."; const money = g.checkout?.amount_huf ? ` (${huf(g.checkout.amount_huf)})` : ""; if ( !window.confirm( `Visszatéríted a(z) „${g.package.name}” ajándékot${money}? A ${g.code} kód érvénytelen lesz, a Stripe visszautalja az árát, a számla sztornózódik.${late}`, ) ) return; setBusy(g.id); setError(null); setDone(null); try { await api(`/admin/gifts/${g.id}/refund`, { method: "POST", body: {} }); setDone(`Visszatérítve: ${g.code}.`); } catch (e) { setError(errorText(e)); } finally { setBusy(null); await load(q.trim()); } } return (
setQ(e.target.value)} placeholder="Kód, a vásárló vagy a címzett e-mail-címe, a címzett neve" />
{error && (

{error}

)} {done && (

{done}

)} {gifts === null ? (

Betöltés…

) : gifts.length === 0 ? (

Nincs ilyen ajándék.

) : (
{gifts.map((g) => ( ))}
Kód Csomag Vásárló → címzett Fizetve Állapot Számla
{g.code} {g.package.name ?? g.package.id} {g.checkout?.amount_huf ? {huf(g.checkout.amount_huf)} : null} {g.checkout && g.checkout.live === false && teszt fizetés} {g.buyer_email ?? törölt fiók} → {g.recipient_name ?? "?"} {g.recipient_email ? ` (${g.recipient_email})` : " (maga adja át)"} {formatDate(g.paid_at, true)} {g.status === "redeemed" ? ( <> beváltva {g.redeemed_at ? formatDate(g.redeemed_at, true) : ""} {g.redeemed_by && {g.redeemed_by}} ) : g.status === "refunded" ? ( <> {g.refund_pending ? ( visszatérítés félbemaradt ) : ( "visszatérítve" )}{" "} {g.refunded_at ? formatDate(g.refunded_at, true) : ""} {g.refund_pending && g.refund_error && {g.refund_error}} ) : ( <> beváltatlan {g.within_withdrawal ? "14 napon belül" : "14 nap letelt"} )} {g.invoice ? ( <> {g.invoice.number ?? (g.invoice.status === "pending" ? "kiállítás alatt" : g.invoice.status)} {g.storno && ( {STORNO[g.storno.status] ?? g.storno.status} {g.storno.number ? `: ${g.storno.number}` : ""} {g.storno.error ? ` – ${g.storno.error}` : ""} )} ) : ( – )} {(g.status === "open" || g.refund_pending) && ( )}
)}
); }