"use client"; import { useCallback, useEffect, useState } from "react"; import { api, errorText } from "@/lib/api"; import { formatDate } from "@/lib/astro"; import type { Package, PromoCode } from "@/lib/types"; const PAGE = 100; const STATUSES: Record = { unused: "fel nem használt", used: "beváltott", revoked: "visszavont", all: "mind", }; interface Page { codes: PromoCode[]; total: number; } /** Making promo codes and the filtered list of them. */ export function PromoCodes({ packages }: { packages: Package[] }) { return ( <> ); } function NewCodes({ packages }: { packages: Package[] }) { const [pkg, setPkg] = useState(packages.find((p) => p.id === "single")?.id ?? packages[0]?.id ?? ""); const [count, setCount] = useState(1); const [note, setNote] = useState(""); const [busy, setBusy] = useState(false); const [error, setError] = useState(null); const [made, setMade] = useState<{ codes: string[]; package: string } | null>(null); const [copied, setCopied] = useState(false); async function create(e: React.FormEvent) { e.preventDefault(); setBusy(true); setError(null); try { const r = await api<{ codes: string[]; package: string }>("/admin/promo-codes", { body: { package: pkg, count, note: note || null }, }); setMade(r); setCopied(false); window.dispatchEvent(new Event("promo-codes-changed")); } catch (err) { setError(errorText(err)); } finally { setBusy(false); } } async function copy() { if (!made) return; await navigator.clipboard.writeText(made.codes.join("\n")); setCopied(true); } return (

Új kódok

setCount(Math.max(1, Math.min(500, Number(e.target.value) || 1)))} />
setNote(e.target.value)} placeholder="pl. meghívottak, első kör" />
{error && (

{error}

)} {made && (

{made.codes.length} új kód: {packages.find((p) => p.id === made.package)?.name ?? made.package}