Files
sorsfonalak/services/frontend/src/app/admin/promokodok/PromoCodes.tsx
T
fecoandClaude Opus 5.5 de629a068d
CI / test (push) Successful in 3m10s
CI / build (push) Skipped
CI / deploy (push) Skipped
Forum photos, a one-row bottom menu, and the site's own question windows
Photos in posts and replies: a "Fotó" button in the new thread form and the
reply boxes. The browser makes the picture small before uploading it (1600
pixels at most, WebP, JPEG where WebP cannot be made); the forum checks it
is a still picture, strips its metadata and makes a square preview
(forum-photos/ in the storage). Five a member a day, five a post; one taken
off before sending is deleted and does not count. Under the post a row of
square previews; a click opens a viewer paged with arrows, swipes and keys.
Seen by whoever may read the thread (long public cache only for public
topics). Unsent photos go after a day, those of removed posts after a year;
removed with the account and listed in the data export. Terms and privacy
notice updated.

The forum's bottom menu keeps its five items in one row (the fifth wrapped
to a second row). On iPhones, when the keyboard goes away without the usual
tap (a photo picker opening over it), the page is put back in place instead
of the menu floating mid-screen over an empty strip.

The browser's own confirm() and prompt() windows (23 places: deleting,
revoking, blocking, muting, moderators' reasons, admin actions) are replaced
by a question window in the site's look, following the forum's light or
dark theme, with named buttons and a text box for reasons.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012vLHzh5qUhFtmJhzkcTbXL
2026-11-27 00:34:59 +00:00

293 lines
9.5 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"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";
import { confirmDialog } from "@/components/Dialogs";
const PAGE = 100;
const STATUSES: Record<string, string> = {
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 (
<>
<NewCodes packages={packages} />
<CodeList packages={packages} />
</>
);
}
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<string | null>(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 (
<section className="card" aria-labelledby="new-codes" style={{ gap: 14 }}>
<h2 id="new-codes">Új kódok</h2>
<form className="promo-form" onSubmit={create}>
<div className="field">
<label htmlFor="promo-package">Csomag</label>
<select id="promo-package" className="inp" value={pkg} onChange={(e) => setPkg(e.target.value)}>
{packages.map((p) => (
<option key={p.id} value={p.id}>
{p.name} ({p.tier === "main" ? "csomag" : "kiegészítő"})
</option>
))}
</select>
</div>
<div className="field">
<label htmlFor="promo-count">Darab</label>
<input
id="promo-count"
className="inp"
type="number"
min={1}
max={500}
value={count}
onChange={(e) => setCount(Math.max(1, Math.min(500, Number(e.target.value) || 1)))}
/>
</div>
<div className="field promo-note">
<label htmlFor="promo-note">Megjegyzés (kinek, melyik kampány)</label>
<input
id="promo-note"
className="inp"
value={note}
maxLength={200}
onChange={(e) => setNote(e.target.value)}
placeholder="pl. meghívottak, első kör"
/>
</div>
<button type="submit" className="btn btn-primary" disabled={busy || !pkg}>
{busy ? "Készül…" : "Kódok készítése"}
</button>
</form>
{error && (
<p className="msg msg-error" role="alert">
{error}
</p>
)}
{made && (
<div className="promo-made">
<p className="msg msg-ok" role="status">
{made.codes.length} új kód: {packages.find((p) => p.id === made.package)?.name ?? made.package}
</p>
<textarea className="inp promo-codes" readOnly rows={Math.min(made.codes.length, 10)} value={made.codes.join("\n")} />
<button type="button" className="btn btn-sm" onClick={copy}>
{copied ? "Kimásolva" : "Mind másolása"}
</button>
</div>
)}
</section>
);
}
function CodeList({ packages }: { packages: Package[] }) {
const [status, setStatus] = useState("unused");
const [pkg, setPkg] = useState("");
const [q, setQ] = useState("");
const [query, setQuery] = useState("");
const [codes, setCodes] = useState<PromoCode[] | null>(null);
const [total, setTotal] = useState(0);
const [busy, setBusy] = useState(false);
const [error, setError] = useState<string | null>(null);
const load = useCallback(
async (offset: number) => {
setBusy(true);
setError(null);
const params = new URLSearchParams({ status, limit: String(PAGE), offset: String(offset) });
if (pkg) params.set("package", pkg);
if (query) params.set("q", query);
try {
const page = await api<Page>(`/admin/promo-codes?${params}`);
setCodes((all) => (offset === 0 ? page.codes : [...(all ?? []), ...page.codes]));
setTotal(page.total);
} catch (e) {
setError(errorText(e));
} finally {
setBusy(false);
}
},
[status, pkg, query],
);
useEffect(() => {
load(0);
const reload = () => load(0);
window.addEventListener("promo-codes-changed", reload);
return () => window.removeEventListener("promo-codes-changed", reload);
}, [load]);
// Searching after a short pause in typing.
useEffect(() => {
const t = setTimeout(() => setQuery(q.trim()), 300);
return () => clearTimeout(t);
}, [q]);
async function revoke(c: PromoCode) {
const ok = await confirmDialog({
title: `Visszavonod a(z) ${c.code} kódot?`,
text: "Utána nem váltható be.",
confirm: "Visszavonás",
danger: true,
});
if (!ok) return;
try {
await api(`/admin/promo-codes/${c.id}/revoke`, { body: {} });
load(0);
} catch (e) {
setError(errorText(e));
}
}
return (
<section className="card" aria-labelledby="code-list" style={{ gap: 12 }}>
<h2 id="code-list">Kódok</h2>
<div className="row" style={{ gap: 12, alignItems: "flex-end" }}>
<div className="field">
<label htmlFor="code-status">Állapot</label>
<select id="code-status" className="inp" value={status} onChange={(e) => setStatus(e.target.value)}>
{Object.entries(STATUSES).map(([v, l]) => (
<option key={v} value={v}>
{l}
</option>
))}
</select>
</div>
<div className="field">
<label htmlFor="code-package">Csomag</label>
<select id="code-package" className="inp" value={pkg} onChange={(e) => setPkg(e.target.value)}>
<option value="">mind</option>
{packages.map((p) => (
<option key={p.id} value={p.id}>
{p.name}
</option>
))}
</select>
</div>
<div className="field" style={{ flex: "1 1 220px" }}>
<label htmlFor="code-search">Keresés</label>
<input
id="code-search"
className="inp"
type="search"
value={q}
onChange={(e) => setQ(e.target.value)}
placeholder="kód, megjegyzés vagy e-mail"
/>
</div>
<p className="hint" style={{ margin: 0 }}>
{total} kód
</p>
</div>
{error && (
<p className="msg msg-error" role="alert">
{error}
</p>
)}
{codes === null ? (
<p className="muted">Betöltés…</p>
) : codes.length === 0 ? (
<p className="muted">Nincs ilyen kód.</p>
) : (
<div className="table-wrap">
<table className="promo-table">
<thead>
<tr>
<th>Kód</th>
<th>Csomag</th>
<th>Megjegyzés</th>
<th>Készült</th>
<th>Állapot</th>
<th />
</tr>
</thead>
<tbody>
{codes.map((c) => (
<tr key={c.id}>
<td>
<code className="promo-code">{c.code}</code>
</td>
<td>{c.package_name ?? c.package}</td>
<td>{c.note ?? <span className="muted">–</span>}</td>
<td className="num">{formatDate(c.created_at)}</td>
<td>
{c.redeemed_at ? (
<>
beváltva {formatDate(c.redeemed_at, true)}
{c.redeemed_by && (
<span className="hint">{c.redeemed_by.name ? `${c.redeemed_by.name} · ` : ""}{c.redeemed_by.email}</span>
)}
</>
) : c.revoked_at ? (
<span className="muted">visszavonva {formatDate(c.revoked_at)}</span>
) : (
<span className="promo-open">szabad</span>
)}
</td>
<td>
{!c.redeemed_at && !c.revoked_at && (
<button type="button" className="btn btn-sm btn-ghost" onClick={() => revoke(c)}>
Visszavonás
</button>
)}
</td>
</tr>
))}
</tbody>
</table>
</div>
)}
{codes && codes.length < total && (
<button type="button" className="btn btn-sm" onClick={() => load(codes.length)} disabled={busy}>
{busy ? "Betöltés…" : "Továbbiak"}
</button>
)}
</section>
);
}