feat: staff apps show the menu in the reader's language (plan 6R.L)
The waiters' phones and the counter show categories, products and every choice (quick options, extras and sub-choices, preferences, shared sets, ingredients, groups) in the signed-in person's language, falling back to the stored text. Display only: carts, lines, tickets and the kitchen keep the venue's text (ids, sub-choices by stored name). In the venue's own language nothing changes. - @xenia/access: menuText / useMenuText (mt, mt.choice, mt.line, mt.matches) - /api/pos/menu carries translations for products, categories and choices - shared ProductDialog takes a text() prop (the counter passes useMenuText) - search finds a product by its name in any language (counter, waiter) - waiter: useMenuNames for sent lines; picker, drawer, cart, summary, table lines, quantity / price history, favourites setup - counter: categories, cards, dialog, cart lines, table lines, payment rows - tests: access menuText, ordering search, test_pos per-language, e2e waiter.mjs and terminal.mjs 6R.L blocks Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -8,6 +8,7 @@
|
||||
// useAllowed('ordering.orders.cancel') → 'yes' | 'approve' (a manager's PIN) | 'no' (4.3)
|
||||
// <Can perm="…" fallback={…}> · <RequireModule module="…">
|
||||
//
|
||||
// useMenuText() → mt(item) / mt.choice(product, name) menu names in the reader's language (6R.L)
|
||||
// SALES_POINTS, IN_PERSON, DIGITAL where an order is sold (phase 3S)
|
||||
// ALLERGENS, allergenList, setAllergen the 14 EU allergens (6.3)
|
||||
//
|
||||
@@ -20,4 +21,5 @@ export { OverrideHost } from './OverrideHost.jsx'
|
||||
export { SALES_POINTS, IN_PERSON, DIGITAL, normalizeSalesPoints } from './salesPoints.js'
|
||||
export { ALLERGENS, ALLERGEN_LEVELS, allergenList, nextAllergenLevel, setAllergen } from './allergens.js'
|
||||
export { Allergens } from './Allergens.jsx'
|
||||
export { DEFAULT_LANGUAGE, MENU_LANGUAGES, inLanguage, shownText, slotOf, translatedLanguages } from './menuLanguages.js'
|
||||
export { DEFAULT_LANGUAGE, MENU_LANGUAGES, inLanguage, shownText, slotOf, translatedLanguages, ownText, choiceTexts, menuText } from './menuLanguages.js'
|
||||
export { useMenuText } from './useMenuText.js' // 6R.L: staff apps in the reader's language (display only)
|
||||
|
||||
@@ -24,3 +24,73 @@ const filled = v => (typeof v === 'string' && v.trim() ? v.trim() : '')
|
||||
* that has one, else `fallback` (the row's stored text, for items never given a language). */
|
||||
export const shownText = (texts, venueLanguage, fallback = '') =>
|
||||
filled(texts?.[venueLanguage]) || MENU_LANGUAGES.map(l => filled(texts?.[l])).find(Boolean) || filled(fallback)
|
||||
|
||||
// ── Staff apps in the device's language (plan 6R.L, owner 2026-11-21) ───────────────────────
|
||||
// The waiters' phones and the counter show menu names in the reader's language (the user's,
|
||||
// else the device's, else the venue's), falling back to the shown text. **Display only:** the
|
||||
// rows keep their `name` (the venue's text), carts and lines are built from it and from ids, so
|
||||
// order lines and tickets keep the venue's text whatever the phone shows. When the reader's
|
||||
// language is the venue's, every text is exactly the stored one (nothing changes).
|
||||
//
|
||||
// `translations` shapes (as the backend sends them): a product / category
|
||||
// {lang: {name, description}}, a choice / ingredient / group / sub-choice {lang: text}.
|
||||
|
||||
/** An item's own text in `lang` (no fallback): '' when it has none. */
|
||||
export function ownText(item, lang, field = 'name') {
|
||||
const v = item?.translations?.[lang]
|
||||
if (typeof v === 'string') return field === 'name' ? filled(v) : ''
|
||||
return filled(v?.[field])
|
||||
}
|
||||
|
||||
/** base name → its text in `lang`, for every choice inside a product (quick options, extras and
|
||||
* their sub-choices, ingredients, preferences, their choices, sub-choices and shared set,
|
||||
* groups), whatever the app's menu shape. Lines and carts keep choices by these base names. */
|
||||
export function choiceTexts(product, lang) {
|
||||
const out = new Map()
|
||||
const seen = new Set()
|
||||
const walk = v => {
|
||||
if (!v || typeof v !== 'object' || seen.has(v)) return
|
||||
seen.add(v)
|
||||
if (Array.isArray(v)) { v.forEach(walk); return }
|
||||
if (v !== product && typeof v.name === 'string' && v.translations) {
|
||||
const text = ownText(v, lang)
|
||||
if (text && !out.has(v.name)) out.set(v.name, text)
|
||||
}
|
||||
for (const [k, x] of Object.entries(v)) if (k !== 'translations' && k !== 'image') walk(x)
|
||||
}
|
||||
walk(product)
|
||||
return out
|
||||
}
|
||||
|
||||
const QTY_SUFFIX = /^(.*) x(\d+)$/
|
||||
|
||||
/** The menu-text function for a reader: `mt(item, field?)` the item's text (product, category,
|
||||
* choice…), `mt.choice(product, name)` a choice kept by its base name (a cart entry, a sent
|
||||
* line's option, a removed ingredient; "name x2" keeps its count), `mt.same` true when nothing
|
||||
* is translated (the reader's language is the venue's, or not a menu language), `mt.line(product,
|
||||
* stored)` an ordered line's name. */
|
||||
export function menuText(lang, venueLanguage) {
|
||||
const same = !lang || lang === venueLanguage || !MENU_LANGUAGES.includes(lang)
|
||||
const cache = new WeakMap()
|
||||
const mt = (item, field = 'name') => {
|
||||
if (!item) return ''
|
||||
const stored = item[field] ?? ''
|
||||
return same ? stored : (ownText(item, lang, field) || stored)
|
||||
}
|
||||
mt.choice = (product, name) => {
|
||||
if (same || !product || !name) return name
|
||||
let map = cache.get(product)
|
||||
if (!map) { map = choiceTexts(product, lang); cache.set(product, map) }
|
||||
if (map.has(name)) return map.get(name)
|
||||
const m = QTY_SUFFIX.exec(name)
|
||||
return m && map.has(m[1]) ? `${map.get(m[1])} x${m[2]}` : name
|
||||
}
|
||||
/** A line already ordered: its product's text in the reader's language, else the name it was
|
||||
* ordered under (`stored`: the line's own record, kept when there is no translation). */
|
||||
mt.line = (product, stored) => (same || !product ? stored : (ownText(product, lang) || stored))
|
||||
/** Search: does the item's text in either language contain `q` (lower case)? */
|
||||
mt.matches = (item, q) => !q || [item?.name, ...MENU_LANGUAGES.map(l => ownText(item, l))].some(s => s && s.toLowerCase().includes(q))
|
||||
mt.same = same
|
||||
mt.lang = lang
|
||||
return mt
|
||||
}
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
// The menu-text function for the signed-in reader (plan 6R.L): their language (user, else device,
|
||||
// else venue: what the app's texts are in) against the venue's. See menuText in menuLanguages.js.
|
||||
import { useMemo } from 'react'
|
||||
import { useLanguage } from '@xenia/i18n'
|
||||
import { useAccess } from './hooks.js'
|
||||
import { menuText } from './menuLanguages.js'
|
||||
|
||||
export function useMenuText() {
|
||||
const { venue } = useAccess()
|
||||
const { language } = useLanguage()
|
||||
const venueLanguage = venue?.language
|
||||
return useMemo(() => menuText(language, venueLanguage), [language, venueLanguage])
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
// Staff apps in the device's language (plan 6R.L): display only, the venue's text kept underneath.
|
||||
import { test } from 'node:test'
|
||||
import assert from 'node:assert/strict'
|
||||
import { choiceTexts, menuText } from '../src/menuLanguages.js'
|
||||
|
||||
const pita = {
|
||||
id: 1, name: 'Πίτα γύρος', description: 'Με τζατζίκι',
|
||||
translations: { el: { name: 'Πίτα γύρος' }, en: { name: 'Gyros pita' } }, // no English description
|
||||
quick_options: [{ id: 5, name: 'Χωρίς κρεμμύδι', translations: { en: 'No onion' } }],
|
||||
options: [{ id: 7, name: 'Πατάτες', translations: { el: 'Πατάτες', en: 'Fries' },
|
||||
sub_choices: [{ name: 'Λίγες', translations: { en: 'A few' } }, { name: 'Πολλές' }] }],
|
||||
ingredients: [{ id: 9, name: 'Ντομάτα', translations: { en: 'Tomato' } }],
|
||||
preference_sets: [{ id: 3, name: 'Κρέας', translations: { en: 'Meat' },
|
||||
choices: [{ id: 30, name: 'Χοιρινό', translations: { en: 'Pork' } }],
|
||||
shared_subset: { name: 'Ψήσιμο', translations: { en: 'Cooking' }, choices: [{ name: 'Καλοψημένο', translations: { en: 'Well done' } }] } }],
|
||||
image: { id: 4, name: 'photo.jpg' },
|
||||
}
|
||||
|
||||
test('an English phone at a Greek venue sees English, Greek where there is none', () => {
|
||||
const mt = menuText('en', 'el')
|
||||
assert.equal(mt.same, false)
|
||||
assert.equal(mt(pita), 'Gyros pita')
|
||||
assert.equal(mt(pita, 'description'), 'Με τζατζίκι')
|
||||
assert.equal(mt(pita.options[0]), 'Fries')
|
||||
assert.equal(mt(pita.options[0].sub_choices[1]), 'Πολλές')
|
||||
assert.equal(mt({ name: 'Ορεκτικά', translations: { en: { name: 'Starters' } } }), 'Starters')
|
||||
})
|
||||
|
||||
test('a phone in the venue language shows the stored text, untouched', () => {
|
||||
const mt = menuText('el', 'el')
|
||||
assert.equal(mt.same, true)
|
||||
assert.equal(mt(pita), 'Πίτα γύρος')
|
||||
assert.equal(mt({ name: 'Only English', translations: { en: { name: 'Only English' } } }), 'Only English')
|
||||
assert.equal(mt.choice(pita, 'Πατάτες'), 'Πατάτες')
|
||||
})
|
||||
|
||||
test('a language that is not a menu language changes nothing', () => {
|
||||
assert.equal(menuText('de', 'el').same, true)
|
||||
assert.equal(menuText(undefined, 'el').same, true)
|
||||
})
|
||||
|
||||
test('choices kept by their base name are shown translated; counts kept', () => {
|
||||
const mt = menuText('en', 'el')
|
||||
assert.equal(mt.choice(pita, 'Χωρίς κρεμμύδι'), 'No onion')
|
||||
assert.equal(mt.choice(pita, 'Χωρίς κρεμμύδι x2'), 'No onion x2')
|
||||
assert.equal(mt.choice(pita, 'Λίγες'), 'A few')
|
||||
assert.equal(mt.choice(pita, 'Ντομάτα'), 'Tomato')
|
||||
assert.equal(mt.choice(pita, 'Χοιρινό'), 'Pork')
|
||||
assert.equal(mt.choice(pita, 'Καλοψημένο'), 'Well done')
|
||||
assert.equal(mt.choice(pita, 'Πολλές'), 'Πολλές') // no English: the base text
|
||||
assert.equal(mt.choice(pita, 'Unknown'), 'Unknown')
|
||||
assert.equal(mt.choice(null, 'Λίγες'), 'Λίγες')
|
||||
})
|
||||
|
||||
test('the counter menu shape (quick / options.subs / prefs.shared) is walked too', () => {
|
||||
const counter = { id: 1, name: 'Καφές', translations: {}, prefs: [{ id: 1, name: 'Ζάχαρη', translations: { en: 'Sugar' },
|
||||
choices: [{ id: 2, name: 'Γλυκός', translations: { en: 'Sweet' }, subs: [] }],
|
||||
shared: { name: 'Γάλα', translations: { en: 'Milk' }, subs: [{ name: 'Φρέσκο', translations: { en: 'Fresh' } }] } }] }
|
||||
const map = choiceTexts(counter, 'en')
|
||||
assert.deepEqual([...map], [['Ζάχαρη', 'Sugar'], ['Γλυκός', 'Sweet'], ['Γάλα', 'Milk'], ['Φρέσκο', 'Fresh']])
|
||||
assert.equal(choiceTexts(pita, 'en').has('photo.jpg'), false)
|
||||
})
|
||||
|
||||
test('an ordered line: the product translation, else the name it was ordered under', () => {
|
||||
assert.equal(menuText('en', 'el').line(pita, 'Πίτα γύρος (παλιά)'), 'Gyros pita')
|
||||
assert.equal(menuText('en', 'el').line({ name: 'Νέο', translations: {} }, 'Παλιό'), 'Παλιό')
|
||||
assert.equal(menuText('en', 'el').line(undefined, 'Παλιό'), 'Παλιό')
|
||||
assert.equal(menuText('el', 'el').line(pita, 'Πίτα γύρος (παλιά)'), 'Πίτα γύρος (παλιά)')
|
||||
})
|
||||
|
||||
test('search finds an item by either language', () => {
|
||||
const mt = menuText('en', 'el')
|
||||
assert.ok(mt.matches(pita, 'gyros'))
|
||||
assert.ok(mt.matches(pita, 'γύρος'))
|
||||
assert.ok(!mt.matches(pita, 'burger'))
|
||||
assert.ok(mt.matches(pita, ''))
|
||||
})
|
||||
@@ -3,6 +3,8 @@
|
||||
// product with preferences to pick opens it on tap. The price shown is the backend's (the menu
|
||||
// price, then the app's quote endpoint through `fetchQuote(lines)`), never added up here.
|
||||
// `product` is the counter's product shape (routers/pos._product_out).
|
||||
// `text` (6R.L): how a name is shown, `text(item, field?)` (the counter passes `useMenuText()`, the
|
||||
// cashier's language); display only: choices are still picked and sent by id / stored name.
|
||||
import { useMemo, useState } from 'react'
|
||||
import { Minus, Plus } from 'lucide-react'
|
||||
import { useFormat, useTranslation } from '@xenia/i18n'
|
||||
@@ -33,7 +35,9 @@ function Price({ value }) {
|
||||
|
||||
// fetchQuote(lines) → the server's quote ({lines, total}); each app its endpoint (the menu tablet's
|
||||
// has in-store prices and no discounts, 6.6)
|
||||
export function ProductDialog({ product, onAdd, onClose, fetchQuote, addLabel }) {
|
||||
const asStored = (item, field = 'name') => item?.[field] ?? ''
|
||||
|
||||
export function ProductDialog({ product, onAdd, onClose, fetchQuote, addLabel, text = asStored }) {
|
||||
const { t } = useTranslation('common')
|
||||
const { formatMoney } = useFormat()
|
||||
const unit = unitOf(product)
|
||||
@@ -47,7 +51,7 @@ export function ProductDialog({ product, onAdd, onClose, fetchQuote, addLabel })
|
||||
const add = () => { onAdd(line); onClose() }
|
||||
|
||||
return (
|
||||
<Dialog open onClose={onClose} size="lg" title={product.name} description={product.description || undefined}
|
||||
<Dialog open onClose={onClose} size="lg" title={text(product)} description={text(product, 'description') || undefined}
|
||||
footer={<>
|
||||
<Button variant="secondary" size="lg" onClick={onClose}>{t('ordering.dialog.close')}</Button>
|
||||
<Button size="lg" disabled={missing.length > 0} onClick={add} data-testid="dialog-add">
|
||||
@@ -84,35 +88,35 @@ export function ProductDialog({ product, onAdd, onClose, fetchQuote, addLabel })
|
||||
const need = missing.find(m => m.kind === 'pref' && m.name === ps.name)
|
||||
return (
|
||||
<div key={ps.id} className="grid gap-3" data-pref={ps.id}>
|
||||
<Section title={ps.name} hint={need ? t('ordering.dialog.pickOne') : null}>
|
||||
<Section title={text(ps)} hint={need ? t('ordering.dialog.pickOne') : null}>
|
||||
{ps.choices.map(c => {
|
||||
const p = picked.find(x => x.id === c.id)
|
||||
return (
|
||||
<span key={c.id} className="flex items-center gap-1">
|
||||
<button type="button" className={chip(!!p)} onClick={() => setSel(s => pickChoice(product, s, ps.id, c.id))} data-choice={c.id}>
|
||||
{c.name} <Price value={c.price} />
|
||||
{text(c)} <Price value={c.price} />
|
||||
</button>
|
||||
{ps.multi && p && (
|
||||
<Stepper value={p.qty} min={1} max={99} onChange={v => setSel(s => setChoiceQty(s, ps.id, c.id, v))} label={c.name} />
|
||||
<Stepper value={p.qty} min={1} max={99} onChange={v => setSel(s => setChoiceQty(s, ps.id, c.id, v))} label={text(c)} />
|
||||
)}
|
||||
</span>
|
||||
)
|
||||
})}
|
||||
</Section>
|
||||
{one?.subs?.length > 0 && (
|
||||
<Section title={one.name}>
|
||||
<Section title={text(one)}>
|
||||
{one.subs.map(s => (
|
||||
<button key={s.name} type="button" className={chip(picked[0].sub === s.name)} onClick={() => setSel(x => pickChoiceSub(x, ps.id, s.name))}>
|
||||
{s.name} <Price value={s.price} />
|
||||
{text(s)} <Price value={s.price} />
|
||||
</button>
|
||||
))}
|
||||
</Section>
|
||||
)}
|
||||
{ps.shared && one && !one.disables_shared && (
|
||||
<Section title={ps.shared.name}>
|
||||
<Section title={text(ps.shared)}>
|
||||
{ps.shared.subs.map(s => (
|
||||
<button key={s.name} type="button" className={chip(sel.prefs[ps.id]?.shared === s.name)} onClick={() => setSel(x => pickShared(x, ps.id, s.name))}>
|
||||
{s.name} <Price value={s.price} />
|
||||
{text(s)} <Price value={s.price} />
|
||||
</button>
|
||||
))}
|
||||
</Section>
|
||||
@@ -128,10 +132,10 @@ export function ProductDialog({ product, onAdd, onClose, fetchQuote, addLabel })
|
||||
return (
|
||||
<span key={q.id} className="flex items-center gap-1">
|
||||
<button type="button" className={chip(n > 0)} onClick={() => setSel(s => setQuick(s, q.id, n > 0 ? 0 : 1))} data-quick={q.id}>
|
||||
{q.name} <Price value={q.price} />
|
||||
{text(q)} <Price value={q.price} />
|
||||
</button>
|
||||
{q.allow_multiple && n > 0 && (
|
||||
<Stepper value={n} min={1} max={99} onChange={v => setSel(s => setQuick(s, q.id, v))} label={q.name} />
|
||||
<Stepper value={n} min={1} max={99} onChange={v => setSel(s => setQuick(s, q.id, v))} label={text(q)} />
|
||||
)}
|
||||
</span>
|
||||
)
|
||||
@@ -147,15 +151,15 @@ export function ProductDialog({ product, onAdd, onClose, fetchQuote, addLabel })
|
||||
<span key={o.id} className="grid gap-2">
|
||||
<span className="flex items-center gap-1">
|
||||
<button type="button" className={chip(!!s)} onClick={() => setSel(x => toggleOption(product, x, o.id))} data-option={o.id}>
|
||||
{o.name} <Price value={o.price} />
|
||||
{text(o)} <Price value={o.price} />
|
||||
</button>
|
||||
{o.allow_multiple && s && <Stepper value={s.qty} min={1} max={99} onChange={v => setSel(x => setOptionQty(x, o.id, v))} label={o.name} />}
|
||||
{o.allow_multiple && s && <Stepper value={s.qty} min={1} max={99} onChange={v => setSel(x => setOptionQty(x, o.id, v))} label={text(o)} />}
|
||||
</span>
|
||||
{s && o.subs.length > 0 && (
|
||||
<span className="flex flex-wrap gap-1 pl-3">
|
||||
{o.subs.map(sub => (
|
||||
<button key={sub.name} type="button" className={chip(s.subs.includes(sub.name))} onClick={() => setSel(x => pickOptionSub(product, x, o.id, sub.name))}>
|
||||
{sub.name} <Price value={sub.price} />
|
||||
{text(sub)} <Price value={sub.price} />
|
||||
</button>
|
||||
))}
|
||||
</span>
|
||||
@@ -170,7 +174,7 @@ export function ProductDialog({ product, onAdd, onClose, fetchQuote, addLabel })
|
||||
<Section title={t('ordering.dialog.without')}>
|
||||
{product.ingredients.map(i => (
|
||||
<button key={i.id} type="button" className={chip(sel.removed.includes(i.id))} onClick={() => setSel(s => toggleRemoved(s, i.id))} data-ingredient={i.id}>
|
||||
{sel.removed.includes(i.id) ? <Minus size={14} /> : <Plus size={14} className="opacity-40" />}{i.name}
|
||||
{sel.removed.includes(i.id) ? <Minus size={14} /> : <Plus size={14} className="opacity-40" />}{text(i)}
|
||||
</button>
|
||||
))}
|
||||
</Section>
|
||||
@@ -189,7 +193,7 @@ export function ProductDialog({ product, onAdd, onClose, fetchQuote, addLabel })
|
||||
</div>
|
||||
</div>
|
||||
{missing.length > 0 && (
|
||||
<p className="text-label text-warning-ink" data-testid="dialog-missing">{t('ordering.dialog.missing', { list: missing.map(m => m.name).join(', ') })}</p>
|
||||
<p className="text-label text-warning-ink" data-testid="dialog-missing">{t('ordering.dialog.missing', { list: missing.map(m => (text.choice ? text.choice(product, m.name) : m.name)).join(', ') })}</p>
|
||||
)}
|
||||
</div>
|
||||
</Dialog>
|
||||
|
||||
@@ -23,7 +23,9 @@ export function productsIn(products, categoryId) {
|
||||
export function searchProducts(products, query) {
|
||||
const words = fold(query).split(/\s+/).filter(Boolean)
|
||||
if (!words.length) return []
|
||||
return products.filter(p => { const name = fold(p.name); return words.every(w => name.includes(w)) })
|
||||
// the stored name or its text in any language (6R.L: a cashier may type in theirs)
|
||||
const texts = p => [p.name, ...Object.values(p.translations ?? {}).map(v => (typeof v === 'string' ? v : v?.name))].filter(Boolean).map(fold)
|
||||
return products.filter(p => { const all = texts(p); return words.every(w => all.some(name => name.includes(w))) })
|
||||
}
|
||||
|
||||
/** Categories that have products, in their order (top level first, then by sort order). */
|
||||
|
||||
@@ -21,3 +21,15 @@ test('a product with a preference to pick needs the dialog; the line holds ids o
|
||||
assert.equal('unit_price' in line, false)
|
||||
assert.deepEqual(line.removed, [9])
|
||||
})
|
||||
|
||||
test('search: the stored name or its text in any language (6R.L)', async () => {
|
||||
const { searchProducts } = await import('../src/cart.js')
|
||||
const list = [
|
||||
{ id: 1, name: 'Πίτα γύρος', translations: { el: { name: 'Πίτα γύρος' }, en: { name: 'Gyros pita' } } },
|
||||
{ id: 2, name: 'Freddo', translations: {} },
|
||||
]
|
||||
assert.deepEqual(searchProducts(list, 'gyros').map(p => p.id), [1])
|
||||
assert.deepEqual(searchProducts(list, 'γυρος πιτα').map(p => p.id), [1])
|
||||
assert.deepEqual(searchProducts(list, 'fred').map(p => p.id), [2])
|
||||
assert.deepEqual(searchProducts(list, 'gyros fred').map(p => p.id), [])
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user