2026-11-15 21:00:50 +00:00
|
|
|
|
"use client";
|
|
|
|
|
|
|
2026-11-23 11:20:14 +00:00
|
|
|
|
import { useCallback, useEffect, useRef, useState } from "react";
|
2026-11-15 21:00:50 +00:00
|
|
|
|
|
2026-11-23 09:04:51 +00:00
|
|
|
|
import { Modal } from "@/components/Modal";
|
2026-11-17 16:19:20 +00:00
|
|
|
|
import { huf, usePackages } from "@/components/PackageCards";
|
2026-11-15 21:00:50 +00:00
|
|
|
|
import { api, errorText } from "@/lib/api";
|
|
|
|
|
|
import { ROLE_LABELS, formatDate } from "@/lib/astro";
|
2026-11-17 16:19:20 +00:00
|
|
|
|
import type { AdminUser, Package, Role } from "@/lib/types";
|
2026-11-15 21:00:50 +00:00
|
|
|
|
|
2026-11-23 09:04:51 +00:00
|
|
|
|
const PAGE_SIZE = 25;
|
2026-11-16 17:39:15 +00:00
|
|
|
|
const EDITABLE_ROLES: Role[] = ["user", "creator", "admin"];
|
2026-11-20 14:35:50 +00:00
|
|
|
|
type UnitKind = "natal" | "synastry" | "forecast" | "question" | "tarot";
|
2026-11-18 08:19:13 +00:00
|
|
|
|
|
|
|
|
|
|
const UNITS: { value: UnitKind; label: string }[] = [
|
2026-11-16 17:39:15 +00:00
|
|
|
|
{ value: "natal", label: "képlet" },
|
|
|
|
|
|
{ value: "synastry", label: "szinasztria" },
|
2026-11-18 08:19:13 +00:00
|
|
|
|
{ value: "forecast", label: "előrejelzés" },
|
2026-11-18 18:44:20 +00:00
|
|
|
|
{ value: "question", label: "kérdés" },
|
2026-11-20 14:35:50 +00:00
|
|
|
|
{ value: "tarot", label: "kártyajóslás" },
|
2026-11-16 17:39:15 +00:00
|
|
|
|
];
|
2026-11-15 21:00:50 +00:00
|
|
|
|
|
2026-11-23 09:04:51 +00:00
|
|
|
|
type Status = { ok: boolean; text: string } | null;
|
|
|
|
|
|
|
2026-11-15 21:00:50 +00:00
|
|
|
|
export function AdminUsers({ selfId }: { selfId: string }) {
|
|
|
|
|
|
const [query, setQuery] = useState("");
|
2026-11-23 09:04:51 +00:00
|
|
|
|
const [page, setPage] = useState(0);
|
|
|
|
|
|
const [data, setData] = useState<{ users: AdminUser[]; total: number } | null>(null);
|
2026-11-15 21:00:50 +00:00
|
|
|
|
const [error, setError] = useState<string | null>(null);
|
2026-11-23 09:04:51 +00:00
|
|
|
|
const [editing, setEditing] = useState<string | null>(null);
|
2026-11-17 16:19:20 +00:00
|
|
|
|
const packages = usePackages() ?? [];
|
2026-11-15 21:00:50 +00:00
|
|
|
|
|
2026-11-23 09:04:51 +00:00
|
|
|
|
const load = useCallback(async (q: string, p: number) => {
|
2026-11-15 21:00:50 +00:00
|
|
|
|
try {
|
2026-11-23 09:04:51 +00:00
|
|
|
|
const res = await api<{ users: AdminUser[]; total: number }>(
|
|
|
|
|
|
`/admin/users?q=${encodeURIComponent(q)}&limit=${PAGE_SIZE}&offset=${p * PAGE_SIZE}`,
|
|
|
|
|
|
);
|
|
|
|
|
|
setData(res);
|
2026-11-15 21:00:50 +00:00
|
|
|
|
setError(null);
|
|
|
|
|
|
} catch (e) {
|
|
|
|
|
|
setError(errorText(e));
|
|
|
|
|
|
}
|
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
2026-11-23 09:04:51 +00:00
|
|
|
|
const t = setTimeout(() => load(query.trim(), page), 250);
|
2026-11-15 21:00:50 +00:00
|
|
|
|
return () => clearTimeout(t);
|
2026-11-23 09:04:51 +00:00
|
|
|
|
}, [query, page, load]);
|
|
|
|
|
|
|
|
|
|
|
|
const users = data?.users ?? null;
|
|
|
|
|
|
const total = data?.total ?? 0;
|
|
|
|
|
|
const pages = Math.max(1, Math.ceil(total / PAGE_SIZE));
|
|
|
|
|
|
const selected = users?.find((u) => u.id === editing) ?? null;
|
|
|
|
|
|
const update = (next: AdminUser) =>
|
|
|
|
|
|
setData((d) => d && { ...d, users: d.users.map((x) => (x.id === next.id ? next : x)) });
|
2026-11-15 21:00:50 +00:00
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
|
<section className="card" aria-label="Felhasználók listája">
|
2026-11-23 09:04:51 +00:00
|
|
|
|
<div className="row" style={{ justifyContent: "space-between", alignItems: "flex-end" }}>
|
|
|
|
|
|
<div className="field" style={{ flex: "1 1 280px", maxWidth: 420 }}>
|
|
|
|
|
|
<label htmlFor="user-search">Keresés név vagy e-mail alapján</label>
|
|
|
|
|
|
<input
|
|
|
|
|
|
id="user-search"
|
|
|
|
|
|
className="inp"
|
|
|
|
|
|
type="search"
|
|
|
|
|
|
value={query}
|
|
|
|
|
|
onChange={(e) => {
|
|
|
|
|
|
setQuery(e.target.value);
|
|
|
|
|
|
setPage(0);
|
|
|
|
|
|
}}
|
|
|
|
|
|
placeholder="pl. anna@"
|
|
|
|
|
|
/>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
{data && (
|
|
|
|
|
|
<span className="muted" style={{ fontSize: 14 }}>
|
|
|
|
|
|
{total} felhasználó
|
|
|
|
|
|
</span>
|
|
|
|
|
|
)}
|
2026-11-15 21:00:50 +00:00
|
|
|
|
</div>
|
|
|
|
|
|
{error && (
|
|
|
|
|
|
<p className="msg msg-error" role="alert">
|
|
|
|
|
|
{error}
|
|
|
|
|
|
</p>
|
|
|
|
|
|
)}
|
|
|
|
|
|
{users === null ? (
|
|
|
|
|
|
<p className="muted">Betöltés…</p>
|
|
|
|
|
|
) : users.length === 0 ? (
|
|
|
|
|
|
<p className="muted">Nincs találat.</p>
|
|
|
|
|
|
) : (
|
|
|
|
|
|
<div className="table-wrap">
|
2026-11-23 09:04:51 +00:00
|
|
|
|
<table className="admin-users">
|
2026-11-15 21:00:50 +00:00
|
|
|
|
<thead>
|
|
|
|
|
|
<tr>
|
|
|
|
|
|
<th scope="col">Felhasználó</th>
|
2026-11-23 09:04:51 +00:00
|
|
|
|
<th scope="col">Szerepkör</th>
|
|
|
|
|
|
<th scope="col">Hátralévő elemzések</th>
|
2026-11-15 21:00:50 +00:00
|
|
|
|
<th scope="col">Képletek</th>
|
|
|
|
|
|
<th scope="col">Regisztrált</th>
|
|
|
|
|
|
<th scope="col">Utoljára belépett</th>
|
2026-11-23 09:04:51 +00:00
|
|
|
|
<th scope="col">
|
|
|
|
|
|
<span className="sr-only">Műveletek</span>
|
|
|
|
|
|
</th>
|
2026-11-15 21:00:50 +00:00
|
|
|
|
</tr>
|
|
|
|
|
|
</thead>
|
|
|
|
|
|
<tbody>
|
|
|
|
|
|
{users.map((u) => (
|
2026-11-23 09:04:51 +00:00
|
|
|
|
<tr key={u.id}>
|
|
|
|
|
|
<td>
|
|
|
|
|
|
<div>{u.display_name || <span className="muted">(nincs név)</span>}</div>
|
|
|
|
|
|
<div className="muted" style={{ fontSize: 13 }}>
|
|
|
|
|
|
{u.email}
|
|
|
|
|
|
{u.id === selfId && " · te"}
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</td>
|
|
|
|
|
|
<td>
|
|
|
|
|
|
<RoleBadges roles={u.roles} />
|
|
|
|
|
|
</td>
|
|
|
|
|
|
<td>
|
|
|
|
|
|
<Balances user={u} />
|
|
|
|
|
|
</td>
|
|
|
|
|
|
<td className="num">{u.charts}</td>
|
|
|
|
|
|
<td className="num">{formatDate(u.created_at)}</td>
|
|
|
|
|
|
<td className="num">{formatDate(u.last_login_at, true)}</td>
|
|
|
|
|
|
<td style={{ textAlign: "right" }}>
|
|
|
|
|
|
<button type="button" className="btn btn-sm" onClick={() => setEditing(u.id)}>
|
|
|
|
|
|
Kezelés
|
|
|
|
|
|
</button>
|
|
|
|
|
|
</td>
|
|
|
|
|
|
</tr>
|
2026-11-15 21:00:50 +00:00
|
|
|
|
))}
|
|
|
|
|
|
</tbody>
|
|
|
|
|
|
</table>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
)}
|
2026-11-23 09:04:51 +00:00
|
|
|
|
{data && total > PAGE_SIZE && (
|
|
|
|
|
|
<nav className="pager" aria-label="Lapozás">
|
|
|
|
|
|
<button type="button" className="btn btn-sm" disabled={page === 0} onClick={() => setPage(page - 1)}>
|
|
|
|
|
|
‹ Előző
|
|
|
|
|
|
</button>
|
|
|
|
|
|
<span className="num">
|
|
|
|
|
|
{page * PAGE_SIZE + 1}–{Math.min(total, (page + 1) * PAGE_SIZE)} / {total} · {page + 1}/{pages}. oldal
|
|
|
|
|
|
</span>
|
|
|
|
|
|
<button
|
|
|
|
|
|
type="button"
|
|
|
|
|
|
className="btn btn-sm"
|
|
|
|
|
|
disabled={page + 1 >= pages}
|
|
|
|
|
|
onClick={() => setPage(page + 1)}
|
|
|
|
|
|
>
|
|
|
|
|
|
Következő ›
|
|
|
|
|
|
</button>
|
|
|
|
|
|
</nav>
|
|
|
|
|
|
)}
|
|
|
|
|
|
{selected && (
|
|
|
|
|
|
<UserDialog
|
|
|
|
|
|
user={selected}
|
|
|
|
|
|
isSelf={selected.id === selfId}
|
|
|
|
|
|
packages={packages}
|
|
|
|
|
|
onChange={update}
|
|
|
|
|
|
onClose={() => setEditing(null)}
|
|
|
|
|
|
/>
|
|
|
|
|
|
)}
|
2026-11-15 21:00:50 +00:00
|
|
|
|
</section>
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-11-23 09:04:51 +00:00
|
|
|
|
function RoleBadges({ roles }: { roles: Role[] }) {
|
|
|
|
|
|
const shown = roles.filter((r) => r !== "user");
|
|
|
|
|
|
if (shown.length === 0) return <span className="muted">Felhasználó</span>;
|
|
|
|
|
|
return (
|
|
|
|
|
|
<div className="row" style={{ gap: 4 }}>
|
|
|
|
|
|
{shown.map((r) => (
|
|
|
|
|
|
<span key={r} className={r === "admin" ? "badge badge-gold" : "badge"}>
|
|
|
|
|
|
{ROLE_LABELS[r] ?? r}
|
|
|
|
|
|
</span>
|
|
|
|
|
|
))}
|
|
|
|
|
|
</div>
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** The remaining units in one line, only the ones the user has. */
|
|
|
|
|
|
function Balances({ user }: { user: AdminUser }) {
|
|
|
|
|
|
const left = UNITS.map((u) => ({ ...u, n: user.analyses_left[u.value] ?? 0 })).filter((u) => u.n !== 0);
|
|
|
|
|
|
if (left.length === 0) return <span className="muted">–</span>;
|
|
|
|
|
|
return (
|
|
|
|
|
|
<span style={{ fontSize: 13 }}>
|
|
|
|
|
|
{left.map((u, i) => (
|
|
|
|
|
|
<span key={u.value} style={{ whiteSpace: "nowrap" }}>
|
|
|
|
|
|
{i > 0 && " · "}
|
|
|
|
|
|
{u.label} <span className="num">{u.n}</span>
|
|
|
|
|
|
</span>
|
|
|
|
|
|
))}
|
|
|
|
|
|
</span>
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** Roles, a package and single units for one user. */
|
|
|
|
|
|
function UserDialog({
|
2026-11-17 16:19:20 +00:00
|
|
|
|
user,
|
|
|
|
|
|
isSelf,
|
|
|
|
|
|
packages,
|
|
|
|
|
|
onChange,
|
2026-11-23 09:04:51 +00:00
|
|
|
|
onClose,
|
2026-11-17 16:19:20 +00:00
|
|
|
|
}: {
|
|
|
|
|
|
user: AdminUser;
|
|
|
|
|
|
isSelf: boolean;
|
|
|
|
|
|
packages: Package[];
|
|
|
|
|
|
onChange: (u: AdminUser) => void;
|
2026-11-23 09:04:51 +00:00
|
|
|
|
onClose: () => void;
|
2026-11-17 16:19:20 +00:00
|
|
|
|
}) {
|
2026-11-23 11:20:14 +00:00
|
|
|
|
// What was chosen or typed but not sent yet (a package, a number of units).
|
|
|
|
|
|
const unsent = useRef<{ pkg: string | null; credit: string | null }>({ pkg: null, credit: null });
|
|
|
|
|
|
const canClose = () => {
|
|
|
|
|
|
const { pkg, credit } = unsent.current;
|
|
|
|
|
|
const what = [pkg && `a(z) ${pkg} csomag`, credit && `${credit} jóváírás`].filter(Boolean).join(" és ");
|
|
|
|
|
|
return !what || window.confirm(`Még nem küldted el: ${what}. Bezárod nélküle?`);
|
|
|
|
|
|
};
|
2026-11-23 09:04:51 +00:00
|
|
|
|
return (
|
|
|
|
|
|
<Modal
|
|
|
|
|
|
title={user.display_name || "(nincs név)"}
|
|
|
|
|
|
subtitle={
|
|
|
|
|
|
<>
|
|
|
|
|
|
{user.email}
|
|
|
|
|
|
{isSelf && " · te"} · regisztrált {formatDate(user.created_at)}
|
|
|
|
|
|
</>
|
|
|
|
|
|
}
|
|
|
|
|
|
onClose={onClose}
|
2026-11-23 11:20:14 +00:00
|
|
|
|
canClose={canClose}
|
2026-11-23 09:04:51 +00:00
|
|
|
|
>
|
|
|
|
|
|
<RolesEditor user={user} isSelf={isSelf} onChange={onChange} />
|
2026-11-23 11:20:14 +00:00
|
|
|
|
<PackageGrant
|
|
|
|
|
|
user={user}
|
|
|
|
|
|
packages={packages}
|
|
|
|
|
|
onChange={onChange}
|
|
|
|
|
|
onUnsent={(name) => {
|
|
|
|
|
|
unsent.current.pkg = name;
|
|
|
|
|
|
}}
|
|
|
|
|
|
/>
|
|
|
|
|
|
<CreditGrant
|
|
|
|
|
|
user={user}
|
|
|
|
|
|
onChange={onChange}
|
|
|
|
|
|
onUnsent={(text) => {
|
|
|
|
|
|
unsent.current.credit = text;
|
|
|
|
|
|
}}
|
|
|
|
|
|
/>
|
2026-11-23 09:04:51 +00:00
|
|
|
|
<div className="row" style={{ justifyContent: "flex-end" }}>
|
2026-11-23 11:20:14 +00:00
|
|
|
|
<button
|
|
|
|
|
|
type="button"
|
|
|
|
|
|
className="btn"
|
|
|
|
|
|
onClick={() => {
|
|
|
|
|
|
if (canClose()) onClose();
|
|
|
|
|
|
}}
|
|
|
|
|
|
>
|
|
|
|
|
|
Bezárás
|
2026-11-23 09:04:51 +00:00
|
|
|
|
</button>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</Modal>
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
2026-11-15 21:00:50 +00:00
|
|
|
|
|
2026-11-23 09:04:51 +00:00
|
|
|
|
function RolesEditor({ user, isSelf, onChange }: { user: AdminUser; isSelf: boolean; onChange: (u: AdminUser) => void }) {
|
|
|
|
|
|
const [busy, setBusy] = useState(false);
|
|
|
|
|
|
const [status, setStatus] = useState<Status>(null);
|
|
|
|
|
|
|
|
|
|
|
|
async function toggle(role: Role, on: boolean) {
|
2026-11-15 21:00:50 +00:00
|
|
|
|
const roles = on ? [...user.roles, role] : user.roles.filter((r) => r !== role);
|
|
|
|
|
|
if (roles.length === 0) return setStatus({ ok: false, text: "Legalább egy szerepkör kell." });
|
|
|
|
|
|
setBusy(true);
|
|
|
|
|
|
setStatus(null);
|
|
|
|
|
|
try {
|
|
|
|
|
|
const res = await api<{ roles: Role[] }>(`/admin/users/${user.id}`, { method: "PATCH", body: { roles } });
|
|
|
|
|
|
onChange({ ...user, roles: res.roles });
|
2026-11-23 09:04:51 +00:00
|
|
|
|
setStatus({ ok: true, text: "Mentve." });
|
2026-11-15 21:00:50 +00:00
|
|
|
|
} catch (e) {
|
|
|
|
|
|
setStatus({ ok: false, text: errorText(e) });
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
setBusy(false);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return (
|
2026-11-23 09:04:51 +00:00
|
|
|
|
<div className="modal-section">
|
|
|
|
|
|
<fieldset>
|
|
|
|
|
|
<legend>Szerepkörök</legend>
|
|
|
|
|
|
<div className="row" style={{ gap: 16 }}>
|
2026-11-15 21:00:50 +00:00
|
|
|
|
{EDITABLE_ROLES.map((role) => {
|
|
|
|
|
|
const locked = isSelf && role === "admin";
|
|
|
|
|
|
return (
|
2026-11-23 09:04:51 +00:00
|
|
|
|
<label key={role} className="check">
|
2026-11-15 21:00:50 +00:00
|
|
|
|
<input
|
|
|
|
|
|
type="checkbox"
|
2026-11-23 09:04:51 +00:00
|
|
|
|
checked={user.roles.includes(role)}
|
2026-11-15 21:00:50 +00:00
|
|
|
|
disabled={busy || locked}
|
2026-11-23 09:04:51 +00:00
|
|
|
|
onChange={(e) => toggle(role, e.target.checked)}
|
2026-11-15 21:00:50 +00:00
|
|
|
|
title={locked ? "A saját admin szerepköröd nem veheted el." : undefined}
|
|
|
|
|
|
/>
|
|
|
|
|
|
{ROLE_LABELS[role]}
|
|
|
|
|
|
</label>
|
|
|
|
|
|
);
|
|
|
|
|
|
})}
|
|
|
|
|
|
</div>
|
2026-11-23 09:04:51 +00:00
|
|
|
|
<p className="muted modal-hint">A szerző híres embereket vihet fel és szerkeszthet.</p>
|
2026-11-15 21:00:50 +00:00
|
|
|
|
{status && (
|
|
|
|
|
|
<span className={status.ok ? "msg msg-ok" : "msg msg-error"} role="status">
|
|
|
|
|
|
{status.text}
|
|
|
|
|
|
</span>
|
|
|
|
|
|
)}
|
2026-11-23 09:04:51 +00:00
|
|
|
|
</fieldset>
|
|
|
|
|
|
</div>
|
2026-11-15 21:00:50 +00:00
|
|
|
|
);
|
|
|
|
|
|
}
|
2026-11-17 16:19:20 +00:00
|
|
|
|
|
|
|
|
|
|
/** Gives the user a package from the catalog (a gift: nothing is paid). */
|
|
|
|
|
|
function PackageGrant({
|
|
|
|
|
|
user,
|
|
|
|
|
|
packages,
|
|
|
|
|
|
onChange,
|
2026-11-23 11:20:14 +00:00
|
|
|
|
onUnsent,
|
2026-11-17 16:19:20 +00:00
|
|
|
|
}: {
|
|
|
|
|
|
user: AdminUser;
|
|
|
|
|
|
packages: Package[];
|
|
|
|
|
|
onChange: (u: AdminUser) => void;
|
2026-11-23 11:20:14 +00:00
|
|
|
|
/** The chosen package's name while it is not given yet (null: nothing pending). */
|
|
|
|
|
|
onUnsent: (name: string | null) => void;
|
2026-11-17 16:19:20 +00:00
|
|
|
|
}) {
|
|
|
|
|
|
const [id, setId] = useState("");
|
2026-11-23 11:20:14 +00:00
|
|
|
|
useEffect(() => {
|
|
|
|
|
|
onUnsent(packages.find((p) => p.id === id)?.name ?? null);
|
|
|
|
|
|
}, [id, packages, onUnsent]);
|
2026-11-17 16:19:20 +00:00
|
|
|
|
const [busy, setBusy] = useState(false);
|
2026-11-23 09:04:51 +00:00
|
|
|
|
const [status, setStatus] = useState<Status>(null);
|
2026-11-17 16:19:20 +00:00
|
|
|
|
|
|
|
|
|
|
async function give(e: React.FormEvent) {
|
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
const pkg = packages.find((p) => p.id === id);
|
|
|
|
|
|
if (!pkg) return;
|
|
|
|
|
|
setBusy(true);
|
|
|
|
|
|
setStatus(null);
|
|
|
|
|
|
try {
|
2026-11-23 09:04:51 +00:00
|
|
|
|
const res = await api<{ analyses_left: AdminUser["analyses_left"] }>(`/admin/users/${user.id}/packages`, {
|
|
|
|
|
|
body: { package: pkg.id, note: "admin ajándék" },
|
|
|
|
|
|
});
|
2026-11-18 06:20:31 +00:00
|
|
|
|
onChange({ ...user, analyses_left: res.analyses_left });
|
2026-11-23 09:04:51 +00:00
|
|
|
|
setStatus({ ok: true, text: `${pkg.name} kiosztva.` });
|
2026-11-17 16:19:20 +00:00
|
|
|
|
setId("");
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
|
setStatus({ ok: false, text: errorText(err) });
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
setBusy(false);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return (
|
2026-11-23 09:04:51 +00:00
|
|
|
|
<form onSubmit={give} className="modal-section">
|
|
|
|
|
|
<fieldset>
|
|
|
|
|
|
<legend>Csomag kiosztása (ajándék)</legend>
|
|
|
|
|
|
<div className="row" style={{ gap: 8 }}>
|
|
|
|
|
|
<label className="sr-only" htmlFor={`pkg-${user.id}`}>
|
|
|
|
|
|
Kiosztandó csomag
|
|
|
|
|
|
</label>
|
|
|
|
|
|
<select
|
|
|
|
|
|
id={`pkg-${user.id}`}
|
|
|
|
|
|
className="inp"
|
|
|
|
|
|
style={{ flex: "1 1 220px", minHeight: 40 }}
|
|
|
|
|
|
value={id}
|
|
|
|
|
|
onChange={(e) => setId(e.target.value)}
|
|
|
|
|
|
disabled={busy || packages.length === 0}
|
|
|
|
|
|
>
|
|
|
|
|
|
<option value="">Válassz csomagot…</option>
|
|
|
|
|
|
{packages.map((p) => (
|
|
|
|
|
|
<option key={p.id} value={p.id}>
|
|
|
|
|
|
{p.name} ({huf(p.price_huf)})
|
|
|
|
|
|
</option>
|
|
|
|
|
|
))}
|
|
|
|
|
|
</select>
|
2026-11-23 11:20:14 +00:00
|
|
|
|
<button type="submit" className={id ? "btn btn-sm btn-primary" : "btn btn-sm"} disabled={busy || !id}>
|
2026-11-23 09:04:51 +00:00
|
|
|
|
{busy ? "…" : "Kiosztás"}
|
|
|
|
|
|
</button>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
{status && (
|
|
|
|
|
|
<span className={status.ok ? "msg msg-ok" : "msg msg-error"} role="status">
|
|
|
|
|
|
{status.text}
|
|
|
|
|
|
</span>
|
|
|
|
|
|
)}
|
|
|
|
|
|
</fieldset>
|
|
|
|
|
|
</form>
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** Adds (or takes away) single analyses of one kind. */
|
2026-11-23 11:20:14 +00:00
|
|
|
|
function CreditGrant({
|
|
|
|
|
|
user,
|
|
|
|
|
|
onChange,
|
|
|
|
|
|
onUnsent,
|
|
|
|
|
|
}: {
|
|
|
|
|
|
user: AdminUser;
|
|
|
|
|
|
onChange: (u: AdminUser) => void;
|
|
|
|
|
|
/** The typed amount while it is not sent yet (null: nothing pending). */
|
|
|
|
|
|
onUnsent: (text: string | null) => void;
|
|
|
|
|
|
}) {
|
2026-11-23 09:04:51 +00:00
|
|
|
|
const [delta, setDelta] = useState("");
|
|
|
|
|
|
const [unit, setUnit] = useState<UnitKind>("natal");
|
2026-11-23 11:20:14 +00:00
|
|
|
|
useEffect(() => {
|
|
|
|
|
|
const label = UNITS.find((u) => u.value === unit)?.label;
|
|
|
|
|
|
onUnsent(delta.trim() ? `${delta.trim()} ${label}` : null);
|
|
|
|
|
|
}, [delta, unit, onUnsent]);
|
2026-11-23 09:04:51 +00:00
|
|
|
|
const [busy, setBusy] = useState(false);
|
|
|
|
|
|
const [status, setStatus] = useState<Status>(null);
|
|
|
|
|
|
|
|
|
|
|
|
async function grant(e: React.FormEvent) {
|
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
const n = Number.parseInt(delta, 10);
|
|
|
|
|
|
if (!Number.isFinite(n) || n === 0) return setStatus({ ok: false, text: "Adj meg egy nem nulla egész számot." });
|
|
|
|
|
|
setBusy(true);
|
|
|
|
|
|
setStatus(null);
|
|
|
|
|
|
try {
|
|
|
|
|
|
const res = await api<{ analyses_left: AdminUser["analyses_left"] }>(`/admin/users/${user.id}/credits`, {
|
|
|
|
|
|
body: { unit, delta: n, note: "admin" },
|
|
|
|
|
|
});
|
|
|
|
|
|
onChange({ ...user, analyses_left: res.analyses_left });
|
|
|
|
|
|
setDelta("");
|
|
|
|
|
|
const label = UNITS.find((u) => u.value === unit)?.label;
|
|
|
|
|
|
setStatus({ ok: true, text: `${n > 0 ? "+" : ""}${n} ${label}.` });
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
|
setStatus({ ok: false, text: errorText(err) });
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
setBusy(false);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
|
<form onSubmit={grant} className="modal-section">
|
|
|
|
|
|
<fieldset>
|
|
|
|
|
|
<legend>Részletes elemzések</legend>
|
|
|
|
|
|
<dl className="balance-grid">
|
|
|
|
|
|
{UNITS.map((u) => (
|
|
|
|
|
|
<div key={u.value}>
|
|
|
|
|
|
<dt>{u.label}</dt>
|
|
|
|
|
|
<dd className="num">{user.analyses_left[u.value] ?? 0}</dd>
|
|
|
|
|
|
</div>
|
2026-11-17 16:19:20 +00:00
|
|
|
|
))}
|
2026-11-23 09:04:51 +00:00
|
|
|
|
</dl>
|
|
|
|
|
|
<div className="row" style={{ gap: 8 }}>
|
|
|
|
|
|
<label className="sr-only" htmlFor={`unit-${user.id}`}>
|
|
|
|
|
|
Elemzés fajtája
|
|
|
|
|
|
</label>
|
|
|
|
|
|
<select
|
|
|
|
|
|
id={`unit-${user.id}`}
|
|
|
|
|
|
className="inp"
|
|
|
|
|
|
style={{ flex: "1 1 150px", minHeight: 40 }}
|
|
|
|
|
|
value={unit}
|
|
|
|
|
|
onChange={(e) => setUnit(e.target.value as UnitKind)}
|
|
|
|
|
|
>
|
|
|
|
|
|
{UNITS.map((u) => (
|
|
|
|
|
|
<option key={u.value} value={u.value}>
|
|
|
|
|
|
{u.label}
|
|
|
|
|
|
</option>
|
|
|
|
|
|
))}
|
|
|
|
|
|
</select>
|
|
|
|
|
|
<label className="sr-only" htmlFor={`delta-${user.id}`}>
|
|
|
|
|
|
Darabszám (negatív: elvétel)
|
|
|
|
|
|
</label>
|
|
|
|
|
|
<input
|
|
|
|
|
|
id={`delta-${user.id}`}
|
|
|
|
|
|
className="inp"
|
|
|
|
|
|
style={{ width: 90, minHeight: 40 }}
|
|
|
|
|
|
inputMode="numeric"
|
|
|
|
|
|
placeholder="+1"
|
|
|
|
|
|
value={delta}
|
|
|
|
|
|
onChange={(e) => setDelta(e.target.value)}
|
|
|
|
|
|
/>
|
2026-11-23 11:20:14 +00:00
|
|
|
|
<button type="submit" className={delta ? "btn btn-sm btn-primary" : "btn btn-sm"} disabled={busy || !delta}>
|
2026-11-23 09:04:51 +00:00
|
|
|
|
Jóváír
|
|
|
|
|
|
</button>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
<p className="muted modal-hint">Negatív számmal el is vehetsz (pl. -1).</p>
|
|
|
|
|
|
{status && (
|
|
|
|
|
|
<span className={status.ok ? "msg msg-ok" : "msg msg-error"} role="status">
|
|
|
|
|
|
{status.text}
|
|
|
|
|
|
</span>
|
|
|
|
|
|
)}
|
|
|
|
|
|
</fieldset>
|
2026-11-17 16:19:20 +00:00
|
|
|
|
</form>
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|