Fix section macros toggle not persisting (stale value in setState updater)
The per-section macros toggle read 'next' inside the setSections updater callback, which React runs during the render phase — after the synchronous PATCH had already fired. So 'next' stayed at its initial 'true', and turning a section OFF sent showMacros:true to the server: the DB never became false while the editor UI flipped, so the public kept showing that section's macros even though it looked off. Fix: compute 'next' from the current sections state before calling setSections, and PATCH that value. The public hierarchy (global AND section) and applyLiveSectionMacros were already correct. Verified: build passes; global ON + a section toggled OFF now persists showMacros=f in the DB and /menu returns that section showMacros:false, so its dishes hide macros.
This commit is contained in:
1 parent
e4e8c50438
commit
57a9b191bd
1 file changed
+6
-8
@@ -43,14 +43,12 @@ export default function CatalogTab({
|
||||
}
|
||||
|
||||
async function toggleSectionMacros(id: string) {
|
||||
let next = true;
|
||||
setSections((prev) =>
|
||||
prev.map((s) => {
|
||||
if (s.id !== id) return s;
|
||||
next = !s.showMacros;
|
||||
return { ...s, showMacros: next };
|
||||
}),
|
||||
);
|
||||
// Compute next from current state (NOT inside the setState updater — that runs
|
||||
// during render, after this PATCH would already have read a stale value).
|
||||
const section = sections.find((s) => s.id === id);
|
||||
if (!section) return;
|
||||
const next = !section.showMacros;
|
||||
setSections((prev) => prev.map((s) => (s.id === id ? { ...s, showMacros: next } : s)));
|
||||
await api(slug, `/sections/${id}`, { method: 'PATCH', json: { showMacros: next } });
|
||||
}
|
||||
const [dragSection, setDragSection] = useState<string | null>(null);
|
||||
|
||||
Reference in new issue
Block a user