From e4e8c50438ad2777e1cdaf61f598a0360c35f45a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorem=20Ju=C3=A1rez?= Date: Mon, 15 Jun 2026 16:26:27 -0600 Subject: [PATCH] Relocate macros toggle and add per-section macros control MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Move the global 'Show macros' toggle from the Palette tab to the Catalog tab, next to 'Cargar menú', where catalog assembly lives - Section.showMacros boolean (default true) + migration 0006; existing sections default to showing (so turning the global on shows all by default) - Per-section toggle in the editor, visible only when the global toggle is on - Hierarchy: global wins. A dish shows macros only if Restaurant.showMacros AND its Section.showMacros are both true. Applies to card flip, list tap-image, the gallery, and the color-code legend (legend shows only if some visible section does) - Turning the global off only HIDES — macro data is never deleted; the disclaimer is reassuring ('Macros will stop showing to the public, but they are not deleted') - Live read: global + per-section showMacros are overlaid onto the published snapshot at read time (new applyLiveSectionMacros helper) so toggles take effect instantly without republishing; old snapshots default section macros on - sections PATCH accepts showMacros; settings PATCH still handles the global - i18n: sectionMacros + soft disclaimer (es/en parity); hint reworded Verified: build passes; 6 migrations apply; section toggle persists; /menu reflects the global+section hierarchy live; macro data preserved in DB and API when toggled off. --- messages/en.json | 4 +- messages/es.json | 4 +- .../0006_section_show_macros/migration.sql | 2 + prisma/schema.prisma | 1 + src/app/[slug]/editor/page.tsx | 1 + src/app/[slug]/page.tsx | 12 +++- src/app/api/[slug]/menu/route.ts | 9 ++- src/app/api/[slug]/sections/[id]/route.ts | 3 +- src/components/editor/CatalogTab.tsx | 72 ++++++++++++++++++- src/components/editor/Editor.tsx | 11 ++- src/components/editor/PaletteTab.tsx | 30 -------- src/components/editor/types.ts | 1 + src/components/public/PublicMenu.tsx | 26 +++++-- src/lib/menu.ts | 16 +++++ src/lib/types.ts | 1 + 15 files changed, 140 insertions(+), 53 deletions(-) create mode 100644 prisma/migrations/0006_section_show_macros/migration.sql diff --git a/messages/en.json b/messages/en.json index 552f8ec..08fceff 100644 --- a/messages/en.json +++ b/messages/en.json @@ -100,7 +100,9 @@ "copied": "Copied", "paletteHint": "This does not affect the macro color code", "showMacros": "Show macros on the menu", - "showMacrosHint": "When off, the public menu shows no macro layer at all." + "showMacrosHint": "Each section can hide its macros individually.", + "showMacrosDisclaimer": "Macros will stop showing to the public, but they are not deleted.", + "sectionMacros": "Macros in this section" }, "qr": { "templates": "Templates", diff --git a/messages/es.json b/messages/es.json index 0a7d0f4..ad2025b 100644 --- a/messages/es.json +++ b/messages/es.json @@ -100,7 +100,9 @@ "copied": "Copiado", "paletteHint": "Esto no afecta el código de color de macros", "showMacros": "Mostrar macros en el menú", - "showMacrosHint": "Si está apagado, el menú público no muestra ninguna capa de macros." + "showMacrosHint": "Cada sección puede ocultar sus macros individualmente.", + "showMacrosDisclaimer": "Las macros dejarán de mostrarse al público, pero no se borran.", + "sectionMacros": "Macros en esta sección" }, "qr": { "templates": "Plantillas", diff --git a/prisma/migrations/0006_section_show_macros/migration.sql b/prisma/migrations/0006_section_show_macros/migration.sql new file mode 100644 index 0000000..3988ae8 --- /dev/null +++ b/prisma/migrations/0006_section_show_macros/migration.sql @@ -0,0 +1,2 @@ +-- AlterTable +ALTER TABLE "Section" ADD COLUMN "showMacros" BOOLEAN NOT NULL DEFAULT true; diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 9101a75..d496647 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -55,6 +55,7 @@ model Section { restaurantId String name Json // { es, en } order Int @default(0) + showMacros Boolean @default(true) // per-section override; only applies when Restaurant.showMacros is on createdAt DateTime @default(now()) restaurant Restaurant @relation(fields: [restaurantId], references: [id], onDelete: Cascade) diff --git a/src/app/[slug]/editor/page.tsx b/src/app/[slug]/editor/page.tsx index d25e179..3106724 100644 --- a/src/app/[slug]/editor/page.tsx +++ b/src/app/[slug]/editor/page.tsx @@ -46,6 +46,7 @@ export default async function EditorPage({ id: s.id, name: s.name as { es?: string; en?: string }, order: s.order, + showMacros: s.showMacros, products: s.products.map((p) => ({ id: p.id, sectionId: p.sectionId, diff --git a/src/app/[slug]/page.tsx b/src/app/[slug]/page.tsx index 5269be9..a22bcea 100644 --- a/src/app/[slug]/page.tsx +++ b/src/app/[slug]/page.tsx @@ -1,6 +1,7 @@ import { notFound } from 'next/navigation'; import { prisma } from '@/lib/prisma'; import { paletteToCssVars } from '@/lib/palette'; +import { applyLiveSectionMacros } from '@/lib/menu'; import type { MenuSnapshot } from '@/lib/types'; import PublicMenu from '@/components/public/PublicMenu'; @@ -9,7 +10,10 @@ export const dynamic = 'force-dynamic'; async function getPublishedMenu(slug: string): Promise { const restaurant = await prisma.restaurant.findUnique({ where: { slug }, - include: { publishedVersion: true }, + include: { + publishedVersion: true, + sections: { select: { id: true, showMacros: true } }, + }, }); if (!restaurant || !restaurant.active) return null; if (!restaurant.publishedVersion) { @@ -27,9 +31,11 @@ async function getPublishedMenu(slug: string): Promise { }; } const snapshot = restaurant.publishedVersion.snapshot as unknown as MenuSnapshot; - // showMacros is read live (not from the snapshot) so toggling it takes effect - // immediately without republishing — and old snapshots without the flag default off. + // showMacros (global + per-section) is read live (not from the snapshot) so + // toggling takes effect immediately without republishing — old snapshots without + // the flags default to off (global) / on (section). snapshot.restaurant.showMacros = restaurant.showMacros; + applyLiveSectionMacros(snapshot, restaurant.sections); return snapshot; } diff --git a/src/app/api/[slug]/menu/route.ts b/src/app/api/[slug]/menu/route.ts index 7721636..d5912c7 100644 --- a/src/app/api/[slug]/menu/route.ts +++ b/src/app/api/[slug]/menu/route.ts @@ -1,5 +1,6 @@ import { NextResponse } from 'next/server'; import { prisma } from '@/lib/prisma'; +import { applyLiveSectionMacros } from '@/lib/menu'; import type { MenuSnapshot } from '@/lib/types'; export const dynamic = 'force-dynamic'; @@ -8,7 +9,10 @@ export const dynamic = 'force-dynamic'; export async function GET(_req: Request, { params }: { params: { slug: string } }) { const restaurant = await prisma.restaurant.findUnique({ where: { slug: params.slug }, - include: { publishedVersion: true }, + include: { + publishedVersion: true, + sections: { select: { id: true, showMacros: true } }, + }, }); if (!restaurant || !restaurant.active) { return NextResponse.json({ error: 'not_found' }, { status: 404 }); @@ -17,7 +21,8 @@ export async function GET(_req: Request, { params }: { params: { slug: string } return NextResponse.json({ error: 'not_published' }, { status: 404 }); } const snapshot = restaurant.publishedVersion.snapshot as unknown as MenuSnapshot; - // Read showMacros live so the toggle applies without republishing. + // Read showMacros (global + per-section) live so toggles apply without republishing. snapshot.restaurant.showMacros = restaurant.showMacros; + applyLiveSectionMacros(snapshot, restaurant.sections); return NextResponse.json(snapshot); } diff --git a/src/app/api/[slug]/sections/[id]/route.ts b/src/app/api/[slug]/sections/[id]/route.ts index bf790d4..22b0f74 100644 --- a/src/app/api/[slug]/sections/[id]/route.ts +++ b/src/app/api/[slug]/sections/[id]/route.ts @@ -10,8 +10,9 @@ export async function PATCH( if ('error' in guard) return guard.error; const body = await req.json().catch(() => null); - const data: { name?: object } = {}; + const data: { name?: object; showMacros?: boolean } = {}; if (body?.name) data.name = body.name as object; + if (typeof body?.showMacros === 'boolean') data.showMacros = body.showMacros; const result = await prisma.section.updateMany({ where: { id: params.id, restaurantId: guard.restaurant.id }, diff --git a/src/components/editor/CatalogTab.tsx b/src/components/editor/CatalogTab.tsx index 4b2f25b..f07c4b5 100644 --- a/src/components/editor/CatalogTab.tsx +++ b/src/components/editor/CatalogTab.tsx @@ -16,6 +16,8 @@ export default function CatalogTab({ sections, setSections, ordersEnabled, + showMacros, + setShowMacros, onPublish, onRestored, }: { @@ -23,6 +25,8 @@ export default function CatalogTab({ sections: EditorSection[]; setSections: React.Dispatch>; ordersEnabled: boolean; + showMacros: boolean; + setShowMacros: (v: boolean) => void; onPublish: () => void; onRestored: (name: string | null) => void; }) { @@ -31,13 +35,34 @@ export default function CatalogTab({ const locale = useLocale() as Locale; const [editing, setEditing] = useState<{ product: EditorProduct } | null>(null); const [importOpen, setImportOpen] = useState(false); + + async function toggleGlobalMacros() { + const next = !showMacros; + setShowMacros(next); + await api(slug, '/settings', { method: 'PATCH', json: { showMacros: next } }); + } + + 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 }; + }), + ); + await api(slug, `/sections/${id}`, { method: 'PATCH', json: { showMacros: next } }); + } const [dragSection, setDragSection] = useState(null); const [dragProduct, setDragProduct] = useState<{ sectionId: string; id: string } | null>(null); async function addSection() { const name = { es: 'Nueva sección', en: 'New section' }; const created = await api<{ id: string }>(slug, '/sections', { method: 'POST', json: { name } }); - setSections((prev) => [...prev, { id: created.id, name, order: prev.length, products: [] }]); + setSections((prev) => [ + ...prev, + { id: created.id, name, order: prev.length, showMacros: true, products: [] }, + ]); } async function deleteSection(id: string) { @@ -153,6 +178,29 @@ export default function CatalogTab({ + {/* Global macros toggle — lives here with catalog assembly (near Cargar menú) */} +
+
+
{t('showMacros')}
+
+ {showMacros ? t('showMacrosHint') : t('showMacrosDisclaimer')} +
+
+ +
+ {sections.map((s) => (
+ {/* Per-section macros toggle — only when the global toggle is on */} + {showMacros && ( + + )} +
{s.products.map((p) => (
('catalog'); const [sections, setSections] = useState(data.sections); const [ordersEnabled, setOrdersEnabled] = useState(data.ordersEnabled); + const [showMacros, setShowMacros] = useState(data.showMacros); const [publishOpen, setPublishOpen] = useState(false); const [publishedName, setPublishedName] = useState(data.publishedVersionName); @@ -45,17 +46,13 @@ export default function Editor({ data, baseUrl }: { data: EditorData; baseUrl: s sections={sections} setSections={setSections} ordersEnabled={ordersEnabled} + showMacros={showMacros} + setShowMacros={setShowMacros} onPublish={() => setPublishOpen(true)} onRestored={(name) => setPublishedName(name)} /> )} - {tab === 'palette' && ( - - )} + {tab === 'palette' && } {tab === 'orders' && ( ⚠️ {t('paletteHint')}

- {/* Show-macros toggle (controls whether the public menu exposes any macro layer) */} -
-
-
{t('showMacros')}
-
{t('showMacrosHint')}
-
- -
-
@@ -213,7 +221,11 @@ export default function PublicMenu({ menu, slug }: { menu: MenuSnapshot; slug: s {/* Modals */} {gallery && ( - setGallery(null)} showMacros={showMacros} /> + setGallery(null)} + showMacros={gallery.showMacros} + /> )} {aiOpen && setAiOpen(false)} />} {orderOpen && ( diff --git a/src/lib/menu.ts b/src/lib/menu.ts index f60214b..9f10779 100644 --- a/src/lib/menu.ts +++ b/src/lib/menu.ts @@ -39,6 +39,7 @@ export async function buildSnapshot(restaurantId: string): Promise id: s.id, name: s.name as LocalizedText, order: s.order, + showMacros: s.showMacros, products: s.products.map( (p): SnapshotProduct => ({ id: p.id, @@ -58,6 +59,20 @@ export async function buildSnapshot(restaurantId: string): Promise }; } +// Overlay live per-section showMacros onto a published snapshot (matched by id) +// so section toggles take effect immediately without republishing. Sections not +// present live keep their snapshot value (default true). +export function applyLiveSectionMacros( + snapshot: MenuSnapshot, + liveSections: { id: string; showMacros: boolean }[], +): void { + const map = new Map(liveSections.map((s) => [s.id, s.showMacros])); + for (const sec of snapshot.sections) { + const live = map.get(sec.id); + sec.showMacros = live !== undefined ? live : (sec.showMacros ?? true); + } +} + // Create a published version from the current draft, keeping max 5 versions. export async function publishVersion( restaurantId: string, @@ -111,6 +126,7 @@ export async function restoreSnapshotToDraft( restaurantId, name: s.name as object, order: s.order, + showMacros: s.showMacros ?? true, }, }); for (const p of s.products) { diff --git a/src/lib/types.ts b/src/lib/types.ts index ba06769..5266817 100644 --- a/src/lib/types.ts +++ b/src/lib/types.ts @@ -44,6 +44,7 @@ export type SnapshotSection = { id: string; name: LocalizedText; order: number; + showMacros: boolean; products: SnapshotProduct[]; };