import { useEffect, useState } from 'react'; import { setBadgeCopies } from '../lib/api'; /** Number of labels a welcome check-in can print, per the DB check constraint. */ export const BADGE_COPIES_MIN = 1; export const BADGE_COPIES_MAX = 4; export function clampBadgeCopies(n: number): number { return Number.isFinite(n) ? Math.max(BADGE_COPIES_MIN, Math.min(BADGE_COPIES_MAX, Math.round(n))) : BADGE_COPIES_MIN; } /** * Presentational 1..4 picker. Two consumers on purpose: * - the Events form holds this in its own state, so Cancel still cancels; * - `BadgeCopiesSetting` wraps it for the Settings view, which saves at once. */ export function BadgeCopiesPicker({ value, onChange, disabled = false, labelId }: { value: number; onChange: (v: number) => void; disabled?: boolean; labelId?: string; }) { return (
{[1, 2, 3, 4].map((n) => ( ))}
); } const HELP = 'How many labels print when an attendee is welcomed in at the front desk'; /** * Badge copies on welcome check-in (1..4, default 1) for one event. * `events.badge_copies` drives `welcome_checkin`, which enqueues that many * labels; a manual reprint from Registrations always prints one. * * Saves immediately through `setBadgeCopies` — a scoped single-column update, * so it can never overwrite another event field the rest of the UI holds stale. */ export function BadgeCopiesSetting({ eventId, initialCopies, eventName }: { eventId: string; initialCopies: number; eventName?: string; }) { const [copies, setCopies] = useState(clampBadgeCopies(initialCopies)); const [saved, setSaved] = useState(clampBadgeCopies(initialCopies)); const [committed, setCommitted] = useState(false); const [busy, setBusy] = useState(false); const [error, setError] = useState(null); // Follow the event when the caller switches to a different one. useEffect(() => { const next = clampBadgeCopies(initialCopies); setCopies(next); setSaved(next); setCommitted(false); setError(null); }, [eventId, initialCopies]); const dirty = copies !== saved; async function save() { if (busy || !dirty) return; setBusy(true); setError(null); try { await setBadgeCopies(eventId, copies); setSaved(copies); setCommitted(true); } catch (e) { setError(e instanceof Error ? e.message : 'Could not save'); } finally { setBusy(false); } } return (
{HELP}{eventName ? <> for {eventName} : null}. Manual reprints from the Registrations view always print one.
{error &&

{error}

}
); }