Refactor checklist module structure
All checks were successful
Deploy Sokko G / deploy (push) Successful in 15s

This commit is contained in:
Shinuwa 2026-08-24 22:19:11 +02:00
parent f2c88ee891
commit ed710b3399
5 changed files with 396 additions and 352 deletions

View file

@ -0,0 +1,225 @@
// Rôle : rend les sections et items de checklist avec édition inline et quantités.
import { useState } from "react";
import { Icon } from "../../../components/Icon.jsx";
import { useInlineEdit } from "../../../hooks/useInlineEdit.js";
import {
getSectionHideWhenComplete,
isSectionComplete,
keepChecklistSection
} from "./checklistUtils.js";
export function ChecklistSection({ section, context, data, reorder, onRenameSection, onUpdateSection, onSaveSections, onUpdateItem, onMoveItemToCategory, onDeleteItem }) {
const showTitle = Boolean(section.title);
const sectionComplete = showTitle && isSectionComplete(section, context);
const hideWhenComplete = getSectionHideWhenComplete(section, data.hideCompletedSections);
const isAutoCollapsed = sectionComplete && hideWhenComplete;
const isCollapsed = Boolean(section.collapsed) || isAutoCollapsed;
const textContent = context.moduleText?.checklist || {};
const className = [
"checklist-section",
showTitle ? "is-grouped" : "",
sectionComplete ? "is-complete" : "",
isCollapsed ? "is-collapsed" : "",
reorder.isGroupDragging(section.id) ? "is-dragging" : "",
reorder.isGroupDropTarget(section.id) ? "is-drop-target" : "",
reorder.getDropPlacement("group", section.id) === "after" ? "drop-after" : ""
].filter(Boolean).join(" ");
const titleEdit = useInlineEdit({
value: section.title,
onCommit: (title) => onRenameSection(section.id, title)
});
const [editingItemId, setEditingItemId] = useState("");
if (sectionComplete && data.hideCompletedSectionsFully) return null;
function saveItems(items) {
onUpdateSection(section.id, (entry) => ({ ...entry, items }));
}
function toggleSectionCollapsed() {
const nextCollapsed = !isCollapsed;
const nextSections = data.sections.map((entry) => {
if (entry.id === section.id) {
const nextSection = { ...entry };
if (nextCollapsed) {
if (sectionComplete) {
nextSection.hideWhenComplete = true;
delete nextSection.collapsed;
} else {
nextSection.collapsed = true;
}
} else {
delete nextSection.collapsed;
if (sectionComplete) nextSection.hideWhenComplete = false;
}
return nextSection;
}
if (data.hideCompletedSections && !nextCollapsed && entry.title && isSectionComplete(entry, context)) return { ...entry, hideWhenComplete: true };
return entry;
});
const nextCompletedSections = nextSections.filter((entry) => entry.title && isSectionComplete(entry, context));
const allHidden = nextCompletedSections.length > 0 && nextCompletedSections.every((entry) => getSectionHideWhenComplete(entry, false));
onSaveSections(nextSections.filter(keepChecklistSection), { hideCompletedSections: allHidden });
}
return (
<li className={className} {...reorder.getGroupProps({ groupId: section.id })}>
{showTitle && (
<div className="checklist-section-header">
<button
className="checklist-drag-handle"
type="button"
onPointerDown={(event) => reorder.groupReorder.startDrag(event, { groupId: section.id })}
aria-label={`${textContent.reorderSectionTitle || "Déplacer la catégorie"} ${section.title}`}
title={textContent.reorderTitle || "Déplacer"}
>
<Icon name="drag" />
</button>
<h3>
<input
{...titleEdit.getInputProps({
className: "checklist-section-title-input",
title: textContent.sectionEditTitle || "Renommer la catégorie",
"aria-label": textContent.sectionEditTitle || "Renommer la catégorie"
})}
/>
</h3>
<div className="checklist-section-actions">
<span>{section.items.filter((item) => context.clampQty(item.qtyCurrent, item.qtyTarget) >= item.qtyTarget).length} / {section.items.length}</span>
<button
className="checklist-section-collapse-button"
type="button"
onClick={toggleSectionCollapsed}
aria-expanded={!isCollapsed}
title={isCollapsed ? textContent.showCompletedSectionTitle || "Afficher cette catégorie" : textContent.hideCompletedSectionTitle || "Réduire cette catégorie"}
aria-label={isCollapsed ? textContent.showCompletedSectionTitle || "Afficher cette catégorie" : textContent.hideCompletedSectionTitle || "Réduire cette catégorie"}
>
<Icon name={isCollapsed ? "chevron-down" : "chevron-up"} />
</button>
</div>
</div>
)}
{!isCollapsed && (
<ul className="checklist-section-items">
{section.items.map((item) => (
<ChecklistItem
key={item.id}
item={item}
section={section}
context={context}
reorder={reorder}
items={section.items}
save={saveItems}
onUpdateItem={onUpdateItem}
onMoveItemToCategory={onMoveItemToCategory}
onDeleteItem={onDeleteItem}
editing={editingItemId === item.id}
onToggleEditing={() => setEditingItemId(editingItemId === item.id ? "" : item.id)}
/>
))}
</ul>
)}
</li>
);
}
function ChecklistItem({ item, section, context, reorder, items, save, onUpdateItem, onMoveItemToCategory, onDeleteItem, editing, onToggleEditing }) {
const done = context.clampQty(item.qtyCurrent, item.qtyTarget) >= item.qtyTarget;
const textContent = context.moduleText?.checklist || {};
const className = [
"checklist-item",
done ? "is-complete" : "",
reorder.isItemDragging(item.id) ? "is-dragging" : "",
reorder.isItemDropTarget(item.id) ? "is-drop-target" : "",
reorder.getDropPlacement("item", item.id) === "after" ? "drop-after" : ""
].filter(Boolean).join(" ");
const labelEdit = useInlineEdit({
value: item.label,
transform: (draft) => String(draft || "").trim() || item.label,
onCommit: (label) => onUpdateItem(item.id, (entry) => ({ ...entry, label })),
autoFocus: editing,
focusKey: item.id
});
const categoryEdit = useInlineEdit({
value: section.title,
onCommit: (title) => onMoveItemToCategory(item.id, title)
});
function updateItem(updater) {
save(items.map((entry) => entry.id === item.id ? updater(entry) : entry));
}
function toggleChecked() {
updateItem((entry) => ({
...entry,
qtyCurrent: done ? 0 : entry.qtyTarget
}));
}
return (
<li className={className} {...reorder.getItemProps({ itemId: item.id, groupId: section.id })}>
<div className={`checklist-item-main ${editing ? "is-editing" : ""}`}>
<button
className="checklist-drag-handle"
type="button"
onPointerDown={(event) => reorder.itemReorder.startDrag(event, item.id)}
aria-label={`${textContent.reorderTitle || "Déplacer"} ${item.label}`}
title={textContent.reorderTitle || "Déplacer"}
>
<Icon name="drag" />
</button>
{item.qtyTarget === 1 ? (
<input
type="checkbox"
checked={done}
onChange={(event) => updateItem((entry) => ({ ...entry, qtyTarget: 1, qtyCurrent: event.target.checked ? 1 : 0 }))}
aria-label={`Terminer ${item.label}`}
/>
) : (
<div className="checklist-qty-controls" aria-label={`Quantité ${item.label}`}>
<button type="button" onClick={() => updateItem((entry) => ({ ...entry, qtyCurrent: context.clampQty(entry.qtyCurrent - 1, entry.qtyTarget) }))} aria-label={textContent.decrementLabel || "Retirer une quantité"}>-</button>
<label className="checklist-qty-current">
<input
type="number"
max={item.qtyTarget}
value={context.clampQty(item.qtyCurrent, item.qtyTarget)}
style={{ "--qty-current-digits": String(context.clampQty(item.qtyCurrent, item.qtyTarget)).length }}
onChange={(event) => updateItem((entry) => ({ ...entry, qtyCurrent: context.clampQty(event.target.value, entry.qtyTarget) }))}
aria-label={`${textContent.currentQuantityLabel || "Quantité actuelle"} ${item.label}`}
/>
<span>/ {item.qtyTarget}</span>
</label>
<button type="button" onClick={() => updateItem((entry) => ({ ...entry, qtyCurrent: context.clampQty(entry.qtyCurrent + 1, entry.qtyTarget) }))} aria-label={textContent.incrementLabel || "Ajouter une quantité"}>+</button>
</div>
)}
{editing ? (
<>
<input
{...labelEdit.getInputProps({
className: "checklist-label-input",
"aria-label": textContent.itemEditLabel || "Libellé"
})}
/>
<input
{...categoryEdit.getInputProps({
className: "checklist-category-input",
placeholder: textContent.sectionPlaceholder || "Catégorie",
"aria-label": textContent.categoryEditLabel || "Catégorie"
})}
/>
</>
) : (
<button className="checklist-item-summary" type="button" onClick={toggleChecked} title={item.label}>
<span>{item.label}</span>
</button>
)}
</div>
<button type="button" className="checklist-edit-button" onClick={onToggleEditing} aria-label={`${textContent.editTitle || "Modifier"} ${item.label}`} title={textContent.editTitle || "Modifier"}>
<Icon name="edit" />
</button>
<button className="checklist-delete-button danger" onClick={() => onDeleteItem(item.id)} aria-label={`${textContent.deleteTitle || "Supprimer"} ${item.label}`} title={textContent.deleteTitle || "Supprimer"}>
<Icon name="trash" />
</button>
</li>
);
}