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, ''))
|
||||
})
|
||||
Reference in New Issue
Block a user