sokko-g/website/src/features/cards/admin/SettingFields.jsx
2026-11-26 13:35:38 +01:00

85 lines
5.8 KiB
JavaScript

// Rôle : champs et barre d'application des formulaires de réglages de l'administration (Expéditions, Équipe du jour).
import { useState } from "react";
import { ConfirmModal } from "../../../components/ToolboxModals.jsx";
import { inRange, parse } from "./adminSettings.js";
import { formatDefault, readPath, same, toDraft, validValue } from "./settingsDraft.js";
export function DefaultHint({ field, value, defaults, busy, onReset }) {
const fallback = readPath(defaults, field.path);
if (fallback === undefined || same(value, fallback)) return null;
return <button className="nebula-admin-setting-default" type="button" disabled={busy} title="Rétablir la valeur par défaut" onClick={onReset}>
Défaut : {formatDefault(fallback, field.unit)} <span aria-hidden="true">↺</span>
</button>;
}
export function NumberInput({ field, value, label, busy, onChange }) {
return <span className="nebula-admin-setting-input" data-unit={field.unit || undefined}>
<input className="nds-input" type="number" inputMode="numeric" min={field.min} max={field.max} step="1" value={value} disabled={busy} aria-label={label} aria-invalid={!inRange(parse(value), field.min, field.max) || undefined} onChange={(event) => onChange(event.target.value)} />
{field.unit && <span aria-hidden="true">{field.unit}</span>}
</span>;
}
// Un réglage : interrupteur, nombre ou suite de nombres, avec son état (modifié, invalide) et la valeur par défaut s'il s'en écarte.
function SettingField({ field, draft, settings, defaults, busy }) {
const value = draft.value(field.path);
const raw = draft.raw(field.path);
const changed = !same(value, readPath(settings, field.path));
const invalid = !validValue(field, value);
const label = field.unit ? `${field.label} (${field.unit})` : field.label;
const state = { "data-changed": changed || undefined, "data-invalid": invalid || undefined };
const hint = <DefaultHint field={field} value={value} defaults={defaults} busy={busy} onReset={() => draft.set(field.path, toDraft(readPath(defaults, field.path)))} />;
const error = invalid && <small role="alert">Entier de {field.min.toLocaleString("fr-FR")} à {field.max.toLocaleString("fr-FR")}</small>;
if (field.kind === "bool") return <div className="nebula-admin-expedition-field" {...state}>
<span className="nebula-admin-setting-label">{field.label}</span>
<button className="nds-button" data-size="sm" data-variant={raw ? "primary" : "secondary"} type="button" aria-pressed={raw} aria-label={field.label} disabled={busy} onClick={() => draft.set(field.path, !raw)}>{raw ? "Oui" : "Non"}</button>
{hint}
</div>;
if (Array.isArray(raw)) return <fieldset className="nebula-admin-expedition-field" {...state}>
<legend>{field.label}</legend>
<div className="nebula-admin-expedition-items">{raw.map((item, index) => <label key={index}><span>{field.items?.[index] ?? index + 1}</span>
<NumberInput field={field} value={item} label={`${label} · ${field.items?.[index] ?? index + 1}`} busy={busy} onChange={(next) => draft.set(field.path, raw.map((current, position) => (position === index ? next : current)))} />
</label>)}</div>
{error}{hint}
</fieldset>;
return <label className="nebula-admin-expedition-field" {...state}><span className="nebula-admin-setting-label">{field.label}</span>
<NumberInput field={field} value={raw} label={label} busy={busy} onChange={(next) => draft.set(field.path, next)} />
{error}{hint}
</label>;
}
export function SettingsGroup({ group, fields, ...props }) {
return <section className="nds-panel nebula-admin-shop nebula-admin-expedition-group" aria-labelledby={`nebula-admin-expedition-${group.id}`}>
<div className="nds-section-heading nebula-admin-panel-heading"><h3 id={`nebula-admin-expedition-${group.id}`}>{group.title}</h3></div>
<p className="nebula-form-help">{group.help}</p>
<div className="nebula-admin-expedition-fields">{fields.map((field) => <SettingField key={field.path} field={field} {...props} />)}</div>
</section>;
}
// Barre commune : nombre de réglages modifiés ou invalides, puis confirmation avec la liste des changements.
// `labelOf` et `formatValue` nomment un réglage et sa valeur ; `blocked` : ce qui empêche d'appliquer des valeurs valides une à une.
export function ApplyBar({ draft, settings, busy, message, labelOf, formatValue, blocked = "", onApply }) {
const [confirming, setConfirming] = useState(false);
const { changes, invalid } = draft;
if (!changes.length && !invalid.length) return null;
const count = (total, word) => `${total} réglage${total > 1 ? "s" : ""} ${word}${total > 1 ? "s" : ""}`;
return <div className="nds-panel nebula-admin-apply-bar" role="region" aria-label="Modifications en attente">
<p role="status">{[changes.length && count(changes.length, "modifié"), invalid.length && count(invalid.length, "invalide"), blocked].filter(Boolean).join(" · ")}</p>
<div className="nds-toolbar">
<button className="nds-button" data-size="sm" data-variant="secondary" type="button" disabled={busy} aria-label="Annuler les modifications" onClick={draft.reset}>Annuler</button>
<button className="nds-button" data-size="sm" data-variant="primary" type="button" disabled={busy || !changes.length || invalid.length > 0 || Boolean(blocked)} onClick={() => setConfirming(true)}>Appliquer</button>
</div>
{confirming && <ConfirmModal
title={`Appliquer ${count(changes.length, "modifié")} ?`}
message={message}
confirmLabel="Appliquer"
onClose={async (confirmed) => {
setConfirming(false);
if (confirmed) await onApply();
}}
>
<ul className="nebula-admin-apply-changes">{changes.map((field) => <li key={field.path}>
<span>{labelOf(field.path)}</span> : {formatValue(readPath(settings, field.path))} → <strong>{formatValue(draft.value(field.path))}</strong>
</li>)}</ul>
</ConfirmModal>}
</div>;
}