From 57a9b191bd56d5a918777a4ecf49d4b4ccbd191b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorem=20Ju=C3=A1rez?= Date: Mon, 15 Jun 2026 17:04:50 -0600 Subject: [PATCH] Fix section macros toggle not persisting (stale value in setState updater) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- src/components/editor/CatalogTab.tsx | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/components/editor/CatalogTab.tsx b/src/components/editor/CatalogTab.tsx index f07c4b5..5512976 100644 --- a/src/components/editor/CatalogTab.tsx +++ b/src/components/editor/CatalogTab.tsx @@ -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(null);