feat(manager): the menu board editor and widgets (platform-v2 step 5.6b)
- Menu board: everything or chosen whole categories (sub-categories included, new products join) plus single products found by search; hide a product from the board (and show it again); the board's own order with arrows on what the server resolved (categories and products; boardOrder writes it); a badge per product; grouping, headings, photos, descriptions, whose prices (a sales point or none), sold out crossed out or hidden, columns, products per page (or as many as fit), seconds per page; colours, font, size, background. - Widgets (text and menu slides): logo, venue name, date (long / short), clock (seconds), message of the day (or its own text), QR code (ordering / QR menu / a link + caption), weather (1-3 days, says it needs internet); where (corners, bars) and size. - The preview stays in view while the settings scroll. - @xenia/signage: the venue clock's Intl call names its time zone where the i18n guard reads it. - tests: manager signage.test.mjs +1; e2e signage.mjs builds a menu board in the editor (categories, a single product, the order, a badge, hiding, a clock) and checks what was saved and resolved. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,27 +1,184 @@
|
||||
// A menu board's settings (plan steps 5.6a, 5.6b): its title, look and background. Which products
|
||||
// it lists and in what order: step 5.6b.
|
||||
import { useTranslation } from '@xenia/i18n'
|
||||
import { Field, Input, Select } from '@xenia/ui'
|
||||
// A menu board's settings (plan step 5.6b): which products (everything, whole categories that stay
|
||||
// up to date, single products, minus hidden ones), the board's own order of categories and products
|
||||
// (arrows on what the board shows), badges, prices of a sales point, photos / descriptions, sold-out
|
||||
// style, columns and pages, the look, and the widgets. `slide.menu` is the server's resolution of
|
||||
// the draft (PlaylistEditor asks for it as you change things).
|
||||
import { useEffect, useMemo, useState } from 'react'
|
||||
import { ArrowDown, ArrowUp, EyeOff, X } from 'lucide-react'
|
||||
import { SALES_POINTS } from '@xenia/access'
|
||||
import { useFormat, useTranslation } from '@xenia/i18n'
|
||||
import { Field, IconButton, Input, Select, Switch } from '@xenia/ui'
|
||||
import client from '../../api/client'
|
||||
import { BADGES, boardOrder, moveGroup, moveItem, toggled } from './signageLogic'
|
||||
import { BackgroundField, ColourField } from './SignageFields'
|
||||
import { TextLook } from './SlideEditor'
|
||||
import WidgetsEditor from './WidgetsEditor'
|
||||
|
||||
export default function MenuEditor({ slide, onChange, media, onMedia, misfit = [] }) {
|
||||
const { t } = useTranslation('manager')
|
||||
const { formatMoney } = useFormat()
|
||||
const c = slide.content || {}
|
||||
const setC = patch => onChange({ ...slide, content: { ...c, ...patch } })
|
||||
const [cats, setCats] = useState([])
|
||||
const [products, setProducts] = useState([])
|
||||
const [q, setQ] = useState('')
|
||||
useEffect(() => {
|
||||
client.get('/api/products/categories').then(r => setCats(r.data)).catch(() => {})
|
||||
client.get('/api/products/').then(r => setProducts(r.data.filter(p => p.lifecycle_status === 'active' && !p.is_service_item))).catch(() => {})
|
||||
}, [])
|
||||
const byId = useMemo(() => Object.fromEntries(products.map(p => [p.id, p])), [products])
|
||||
const top = cats.filter(x => !x.parent_id)
|
||||
const subsOf = id => cats.filter(x => x.parent_id === id)
|
||||
const groups = slide.menu?.groups || []
|
||||
const shown = new Set(groups.flatMap(g => g.items.map(i => i.id)))
|
||||
const found = q.trim().length < 1 ? [] : products.filter(p => p.name.toLowerCase().includes(q.trim().toLowerCase()) && !shown.has(p.id)).slice(0, 8)
|
||||
const reorder = next => setC(boardOrder(next))
|
||||
const badges = c.badges || {}
|
||||
|
||||
return (
|
||||
<div className="grid gap-3" data-testid="signage-menu-editor">
|
||||
<div className="grid gap-4" data-testid="signage-menu-editor">
|
||||
<Field label={t('signage.title')}><Input value={c.title || ''} maxLength={120} onChange={e => setC({ title: e.target.value })} data-testid="signage-menu-title" /></Field>
|
||||
<div className="grid gap-3 sm:grid-cols-2">
|
||||
<Field label={t('signage.columns')}>
|
||||
<Select value={String(c.columns || 2)} onChange={e => setC({ columns: Number(e.target.value) })}
|
||||
options={[1, 2, 3].map(n => ({ value: String(n), label: String(n) }))} data-testid="signage-columns" />
|
||||
|
||||
{/* what it lists */}
|
||||
<fieldset className="grid gap-3 rounded-control border border-line p-3">
|
||||
<legend className="px-1 text-label font-medium text-ink-2">{t('signage.menu.what')}</legend>
|
||||
<Select value={c.select || 'custom'} onChange={e => setC({ select: e.target.value })} aria-label={t('signage.menu.what')} data-testid="signage-menu-select"
|
||||
options={[{ value: 'all', label: t('signage.menu.all') }, { value: 'custom', label: t('signage.menu.custom') }]} />
|
||||
{c.select !== 'all' && <>
|
||||
<div className="grid gap-1.5">
|
||||
<span className="text-label text-ink-2">{t('signage.menu.categories')}</span>
|
||||
<p className="text-caption text-ink-3">{t('signage.menu.categoriesHint')}</p>
|
||||
<div className="flex flex-wrap gap-1.5" data-testid="signage-menu-categories">
|
||||
{top.flatMap(cat => [cat, ...subsOf(cat.id)]).map(cat => {
|
||||
const on = (c.category_ids || []).includes(cat.id)
|
||||
return (
|
||||
<button key={cat.id} type="button" aria-pressed={on} onClick={() => setC({ category_ids: toggled(c.category_ids, cat.id) })}
|
||||
data-testid="signage-menu-category" data-name={cat.name}
|
||||
className={`px-2.5 py-1 rounded-control border text-label transition-colors duration-fast
|
||||
${on ? 'border-accent bg-accent-soft text-accent-ink' : 'border-line bg-surface-2 text-ink hover:bg-surface-3'}`}>
|
||||
{cat.parent_id ? `↳ ${cat.name}` : cat.name}
|
||||
</button>
|
||||
)
|
||||
})}
|
||||
</div>
|
||||
</div>
|
||||
<div className="grid gap-1.5">
|
||||
<span className="text-label text-ink-2">{t('signage.menu.products')}</span>
|
||||
{(c.product_ids || []).length > 0 && (
|
||||
<div className="flex flex-wrap gap-1.5">
|
||||
{(c.product_ids || []).map(id => (
|
||||
<span key={id} className="inline-flex items-center gap-1 pl-2.5 pr-1 py-0.5 rounded-control bg-surface-2 text-label text-ink">
|
||||
{byId[id]?.name ?? `#${id}`}
|
||||
<IconButton size="sm" variant="ghost" label={t('media.removePhoto')} onClick={() => setC({ product_ids: (c.product_ids || []).filter(x => x !== id) })}><X size={12} /></IconButton>
|
||||
</span>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
<Input value={q} onChange={e => setQ(e.target.value)} placeholder={t('signage.menu.searchProduct')} aria-label={t('signage.menu.searchProduct')} data-testid="signage-menu-search" />
|
||||
{found.length > 0 && (
|
||||
<ul className="grid gap-1" data-testid="signage-menu-found">
|
||||
{found.map(p => (
|
||||
<li key={p.id}>
|
||||
<button type="button" className="w-full text-left px-2 py-1 rounded-control hover:bg-surface-2 text-label text-ink" data-testid="signage-menu-found-item"
|
||||
onClick={() => { setC({ product_ids: toggled(c.product_ids, p.id), exclude_ids: (c.exclude_ids || []).filter(x => x !== p.id) }); setQ('') }}>
|
||||
+ {p.name}
|
||||
</button>
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
)}
|
||||
</div>
|
||||
</>}
|
||||
{(c.exclude_ids || []).length > 0 && (
|
||||
<div className="grid gap-1.5">
|
||||
<span className="text-label text-ink-2">{t('signage.menu.hidden')}</span>
|
||||
<div className="flex flex-wrap gap-1.5">
|
||||
{(c.exclude_ids || []).map(id => (
|
||||
<button key={id} type="button" onClick={() => setC({ exclude_ids: (c.exclude_ids || []).filter(x => x !== id) })}
|
||||
className="px-2.5 py-1 rounded-control border border-line bg-surface-2 text-label text-ink-2 hover:bg-surface-3" title={t('signage.menu.showAgain')}>
|
||||
{byId[id]?.name ?? `#${id}`} · {t('signage.menu.showAgain')}
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</fieldset>
|
||||
|
||||
{/* the board's own order, badges, hiding */}
|
||||
<fieldset className="grid gap-2 rounded-control border border-line p-3">
|
||||
<legend className="px-1 text-label font-medium text-ink-2">{t('signage.menu.onTheBoard', { count: slide.menu?.count ?? 0 })}</legend>
|
||||
<p className="text-caption text-ink-3">{t('signage.menu.orderHint')}</p>
|
||||
<div className="grid gap-3 max-h-[28rem] overflow-auto pr-1" data-testid="signage-menu-board">
|
||||
{groups.map((g, gi) => (
|
||||
<div key={`${g.category?.id ?? 'all'}`} className="grid gap-1" data-testid="signage-menu-group" data-name={g.category?.name ?? ''}>
|
||||
{g.category && (
|
||||
<div className="flex items-center gap-1">
|
||||
<span className="text-label font-semibold text-ink flex-1">{g.category.name}</span>
|
||||
<IconButton size="sm" variant="ghost" label={t('display.earlier')} disabled={gi === 0} onClick={() => reorder(moveGroup(groups, gi, -1))} data-testid="signage-group-up"><ArrowUp size={14} /></IconButton>
|
||||
<IconButton size="sm" variant="ghost" label={t('display.later')} disabled={gi === groups.length - 1} onClick={() => reorder(moveGroup(groups, gi, 1))}><ArrowDown size={14} /></IconButton>
|
||||
</div>
|
||||
)}
|
||||
<ul className="grid gap-0.5">
|
||||
{g.items.map((it, ii) => (
|
||||
<li key={it.id} className="flex items-center gap-1.5 pl-2 rounded-control hover:bg-surface-2" data-testid="signage-menu-row" data-id={it.id}>
|
||||
<span className={`flex-1 min-w-0 truncate text-label ${it.available ? 'text-ink' : 'text-ink-3 line-through'}`}>{it.name}</span>
|
||||
{it.price != null && <span className="text-caption text-ink-2 tabular-nums">{formatMoney(it.price)}</span>}
|
||||
<Select size="sm" value={badges[String(it.id)] || ''} aria-label={t('signage.menu.badge')} data-testid="signage-menu-badge"
|
||||
onChange={e => {
|
||||
const next = { ...badges }
|
||||
if (e.target.value) next[String(it.id)] = e.target.value
|
||||
else delete next[String(it.id)]
|
||||
setC({ badges: next })
|
||||
}}
|
||||
options={[{ value: '', label: t('signage.menu.noBadge') }, ...BADGES.map(b => ({ value: b, label: t(`signage.badges.${b}`) }))]} />
|
||||
<IconButton size="sm" variant="ghost" label={t('display.earlier')} disabled={ii === 0} onClick={() => reorder(moveItem(groups, gi, ii, -1))} data-testid="signage-item-up"><ArrowUp size={14} /></IconButton>
|
||||
<IconButton size="sm" variant="ghost" label={t('display.later')} disabled={ii === g.items.length - 1} onClick={() => reorder(moveItem(groups, gi, ii, 1))}><ArrowDown size={14} /></IconButton>
|
||||
<IconButton size="sm" variant="ghost" label={t('signage.menu.hide')} data-testid="signage-item-hide"
|
||||
onClick={() => setC({ exclude_ids: toggled(c.exclude_ids, it.id), product_ids: (c.product_ids || []).filter(x => x !== it.id) })}><EyeOff size={14} /></IconButton>
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
</div>
|
||||
))}
|
||||
{groups.length === 0 && <p className="text-label text-ink-3">{t('signage.menu.empty')}</p>}
|
||||
</div>
|
||||
</fieldset>
|
||||
|
||||
{/* what each line shows, and the pages */}
|
||||
<fieldset className="grid gap-3 rounded-control border border-line p-3 sm:grid-cols-2">
|
||||
<legend className="px-1 text-label font-medium text-ink-2">{t('signage.menu.options')}</legend>
|
||||
<Switch checked={c.group_by_category !== false} onChange={on => setC({ group_by_category: on })} label={t('signage.menu.group')} data-testid="signage-menu-group-switch" />
|
||||
<Switch checked={c.category_titles !== false} onChange={on => setC({ category_titles: on })} label={t('signage.menu.headings')} />
|
||||
<Switch checked={!!c.show_photos} onChange={on => setC({ show_photos: on })} label={t('signage.menu.photos')} data-testid="signage-menu-photos" />
|
||||
<Switch checked={!!c.show_descriptions} onChange={on => setC({ show_descriptions: on })} label={t('signage.menu.descriptions')} />
|
||||
<Field label={t('signage.menu.prices')}>
|
||||
<Select value={c.sales_point ?? ''} onChange={e => setC({ sales_point: e.target.value || null })} data-testid="signage-menu-prices"
|
||||
options={[{ value: '', label: t('signage.menu.noPrices') }, ...SALES_POINTS.map(sp => ({ value: sp, label: t(`common:salesPoints.${sp}.name`) }))]} />
|
||||
</Field>
|
||||
<Field label={t('signage.menu.soldOut')}>
|
||||
<Select value={c.sold_out || 'strike'} onChange={e => setC({ sold_out: e.target.value })}
|
||||
options={[{ value: 'strike', label: t('signage.menu.strike') }, { value: 'hide', label: t('signage.menu.hideSoldOut') }]} />
|
||||
</Field>
|
||||
<Field label={t('signage.columns')}>
|
||||
<Select value={String(c.columns || 2)} onChange={e => setC({ columns: Number(e.target.value) })} data-testid="signage-columns"
|
||||
options={[1, 2, 3].map(n => ({ value: String(n), label: String(n) }))} />
|
||||
</Field>
|
||||
<Field label={t('signage.menu.perPage')} hint={t('signage.menu.perPageHint')}>
|
||||
<Input inputMode="numeric" value={c.page_size ? String(c.page_size) : ''} placeholder={t('signage.menu.auto')}
|
||||
onChange={e => { const n = Number(e.target.value.replace(/\D/g, '')); setC({ page_size: n ? Math.min(60, Math.max(3, n)) : null }) }} />
|
||||
</Field>
|
||||
<Field label={t('signage.menu.pageSeconds')}>
|
||||
<Input inputMode="numeric" value={String(c.page_seconds || 10)} onChange={e => setC({ page_seconds: Math.min(120, Math.max(3, Number(e.target.value.replace(/\D/g, '')) || 3)) })} />
|
||||
</Field>
|
||||
</fieldset>
|
||||
|
||||
<div className="grid gap-3 sm:grid-cols-2">
|
||||
<ColourField label={t('signage.accent')} value={c.accent_colour || '#f59e0b'} onChange={v => setC({ accent_colour: v })} testid="signage-accent" />
|
||||
</div>
|
||||
<TextLook c={c} setC={setC} align={false} />
|
||||
<BackgroundField value={c.background} onChange={background => setC({ background })} media={media} onMedia={onMedia}
|
||||
misfit={misfit.includes(c.background?.media_id)} />
|
||||
<WidgetsEditor widgets={c.widgets || []} onChange={widgets => setC({ widgets })} media={media} onMedia={onMedia} />
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user