Admin gifts: an e-mail to the user; the user dialog no longer loses a pick
CI / test (push) Successful in 2m3s
CI / build (push) Skipped
CI / deploy (push) Skipped

- A package or units an admin gives a user are announced in a "gift"
  e-mail (taking units away is not). Queued in the same transaction.
- The user dialog asks before closing with a chosen package or a typed
  amount not sent yet; the send buttons turn gold when ready, "Kész" is now
  a plain "Bezárás".
- The modal closes on the backdrop only when the press started there too,
  so picking from a select whose list hangs over the backdrop, or dragging
  a selection out, no longer closes it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012vLHzh5qUhFtmJhzkcTbXL
This commit is contained in:
feco
2026-11-23 11:20:14 +00:00
co-authored by Claude Opus 5.5
parent e5c90b260d
commit f8abdac790
5 changed files with 221 additions and 14 deletions
+54 -8
View File
@@ -1,6 +1,6 @@
"use client";
import { useCallback, useEffect, useState } from "react";
import { useCallback, useEffect, useRef, useState } from "react";
import { Modal } from "@/components/Modal";
import { huf, usePackages } from "@/components/PackageCards";
@@ -207,6 +207,13 @@ function UserDialog({
onChange: (u: AdminUser) => void;
onClose: () => void;
}) {
// 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?`);
};
return (
<Modal
title={user.display_name || "(nincs név)"}
@@ -217,13 +224,33 @@ function UserDialog({
</>
}
onClose={onClose}
canClose={canClose}
>
<RolesEditor user={user} isSelf={isSelf} onChange={onChange} />
<PackageGrant user={user} packages={packages} onChange={onChange} />
<CreditGrant user={user} onChange={onChange} />
<PackageGrant
user={user}
packages={packages}
onChange={onChange}
onUnsent={(name) => {
unsent.current.pkg = name;
}}
/>
<CreditGrant
user={user}
onChange={onChange}
onUnsent={(text) => {
unsent.current.credit = text;
}}
/>
<div className="row" style={{ justifyContent: "flex-end" }}>
<button type="button" className="btn btn-primary" onClick={onClose}>
Kész
<button
type="button"
className="btn"
onClick={() => {
if (canClose()) onClose();
}}
>
Bezárás
</button>
</div>
</Modal>
@@ -287,12 +314,18 @@ function PackageGrant({
user,
packages,
onChange,
onUnsent,
}: {
user: AdminUser;
packages: Package[];
onChange: (u: AdminUser) => void;
/** The chosen package's name while it is not given yet (null: nothing pending). */
onUnsent: (name: string | null) => void;
}) {
const [id, setId] = useState("");
useEffect(() => {
onUnsent(packages.find((p) => p.id === id)?.name ?? null);
}, [id, packages, onUnsent]);
const [busy, setBusy] = useState(false);
const [status, setStatus] = useState<Status>(null);
@@ -339,7 +372,7 @@ function PackageGrant({
</option>
))}
</select>
<button type="submit" className="btn btn-sm" disabled={busy || !id}>
<button type="submit" className={id ? "btn btn-sm btn-primary" : "btn btn-sm"} disabled={busy || !id}>
{busy ? "…" : "Kiosztás"}
</button>
</div>
@@ -354,9 +387,22 @@ function PackageGrant({
}
/** Adds (or takes away) single analyses of one kind. */
function CreditGrant({ user, onChange }: { user: AdminUser; onChange: (u: AdminUser) => void }) {
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;
}) {
const [delta, setDelta] = useState("");
const [unit, setUnit] = useState<UnitKind>("natal");
useEffect(() => {
const label = UNITS.find((u) => u.value === unit)?.label;
onUnsent(delta.trim() ? `${delta.trim()} ${label}` : null);
}, [delta, unit, onUnsent]);
const [busy, setBusy] = useState(false);
const [status, setStatus] = useState<Status>(null);
@@ -422,7 +468,7 @@ function CreditGrant({ user, onChange }: { user: AdminUser; onChange: (u: AdminU
value={delta}
onChange={(e) => setDelta(e.target.value)}
/>
<button type="submit" className="btn btn-sm" disabled={busy || !delta}>
<button type="submit" className={delta ? "btn btn-sm btn-primary" : "btn btn-sm"} disabled={busy || !delta}>
Jóváír
</button>
</div>
+21 -2
View File
@@ -5,20 +5,30 @@ import { useEffect, useId, useRef } from "react";
/**
* A modal window (the browser's own <dialog>: focus stays inside, Escape
* closes it). Shown while mounted; a click on the dimmed backdrop closes it.
* `canClose`: asked first (e.g. about unsaved input); false keeps it open.
*/
export function Modal({
title,
subtitle,
onClose,
canClose,
children,
}: {
title: string;
subtitle?: React.ReactNode;
onClose: () => void;
canClose?: () => boolean;
children: React.ReactNode;
}) {
const ref = useRef<HTMLDialogElement>(null);
const titleId = useId();
// Where the press started: only a press and release both on the backdrop close
// it (not a text selection dragged out, nor a pick from a select's list that
// hangs over the backdrop).
const pressedBackdrop = useRef(false);
const close = () => {
if (!canClose || canClose()) onClose();
};
// No close() on unmount: that would fire onClose; a removed dialog is gone anyway.
useEffect(() => {
@@ -32,8 +42,17 @@ export function Modal({
className="modal"
aria-labelledby={titleId}
onClose={onClose}
onCancel={(e) => {
// Escape: the dialog would close by itself.
if (canClose && !canClose()) e.preventDefault();
}}
onPointerDown={(e) => {
pressedBackdrop.current = e.target === ref.current;
}}
onClick={(e) => {
if (e.target === ref.current) onClose();
const onBackdrop = pressedBackdrop.current && e.target === ref.current;
pressedBackdrop.current = false;
if (onBackdrop) close();
}}
>
<div className="modal-inner">
@@ -42,7 +61,7 @@ export function Modal({
<h2 id={titleId}>{title}</h2>
{subtitle && <div className="muted modal-sub">{subtitle}</div>}
</div>
<button type="button" className="modal-close" onClick={onClose} aria-label="Bezárás">
<button type="button" className="modal-close" onClick={close} aria-label="Bezárás">
<svg viewBox="0 0 24 24" aria-hidden="true">
<path d="M6 6l12 12M18 6L6 18" />
</svg>