feat(terminal): counter POS screen with server-priced cart and shift gate (platform-v2 3.3a)
- Backend /api/pos (pos_counter): session (business day, the cashier's own shift, self-start, who can open the day), menu (sellable products with every kind of choice, no costs; price now from the pricing engine), quote (lines by id → the waiter's selected_options entries with DB prices → resolve_price as orders do; pos.line_invalid). - POS device settings: default order type, start category, receipt printer (new ref sources categories / printers, also in the manager's Devices dialogs). - Terminal apps/pos: shift gate (start shift with drawer cash), categories, search, product grid, price check, options dialog, cart priced by the backend. - Tests: backend test_pos (13), terminal pos.test.mjs, manager refOptions, terminal.mjs +10 POS checks. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,175 @@
|
||||
// The counter POS screen (plan step 3.3a): categories on the left, the product grid with a
|
||||
// search on top, the cart on the right. A tap adds a product (or opens its choices when it
|
||||
// has preferences to pick); the (i) button checks its price without adding it. Every price is
|
||||
// the backend's: the menu's and the cart's quote. Paying comes in 3.3b.
|
||||
import { useMemo, useState } from 'react'
|
||||
import { Info, Minus, Plus, Search, ShoppingBag, Trash2, Utensils, X } from 'lucide-react'
|
||||
import { useFormat, useLanguage, useTranslation } from '@xenia/i18n'
|
||||
import { Button, EmptyState, IconButton, Input, Spinner } from '@xenia/ui'
|
||||
import {
|
||||
addLine, categoriesWithProducts, defaultSelection, needsChoices, productsIn, searchProducts, setLineQty, toLine, unitOf,
|
||||
} from './logic/cart'
|
||||
import ProductDialog from './ProductDialog'
|
||||
import { useMenu, useQuote } from './usePos'
|
||||
|
||||
export default function PosScreen() {
|
||||
const { t } = useTranslation('terminal')
|
||||
const { menu } = useMenu()
|
||||
if (!menu) return <div className="absolute inset-0 flex items-center justify-center bg-canvas"><Spinner /></div>
|
||||
return <Counter menu={menu} key={menu.settings.start_category_id ?? 'all'} t={t} />
|
||||
}
|
||||
|
||||
function Counter({ menu, t }) {
|
||||
const { formatMoney } = useFormat()
|
||||
const { language } = useLanguage()
|
||||
const [category, setCategory] = useState(() => menu.settings.start_category_id ?? null)
|
||||
const [query, setQuery] = useState('')
|
||||
const [orderType, setOrderType] = useState(menu.settings.default_order_type)
|
||||
const [cart, setCart] = useState([])
|
||||
const [dialog, setDialog] = useState(null) // product
|
||||
const quote = useQuote(cart, orderType)
|
||||
|
||||
const byId = useMemo(() => new Map(menu.products.map(p => [p.id, p])), [menu.products])
|
||||
const categories = useMemo(() => categoriesWithProducts(menu.categories, menu.products), [menu])
|
||||
const shown = useMemo(() => (query.trim() ? searchProducts(menu.products, query) : productsIn(menu.products, category)),
|
||||
[menu.products, query, category])
|
||||
|
||||
const tap = product => {
|
||||
if (needsChoices(product)) { setDialog(product); return }
|
||||
const unit = unitOf(product)
|
||||
setCart(c => addLine(c, toLine(product, defaultSelection(product), unit ? unit.start : 1)))
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="absolute inset-0 grid grid-cols-[13rem_1fr_24rem] bg-canvas text-ink" data-testid="pos-screen" lang={language}>
|
||||
{/* categories */}
|
||||
<nav className="border-r border-line bg-surface overflow-y-auto p-2 grid content-start gap-1" data-testid="pos-categories">
|
||||
<CategoryButton active={category == null && !query} onClick={() => { setCategory(null); setQuery('') }}>{t('pos.allProducts')}</CategoryButton>
|
||||
{categories.map(c => (
|
||||
<CategoryButton key={c.id} colour={c.color} active={category === c.id && !query} onClick={() => { setCategory(c.id); setQuery('') }} testId={`category-${c.id}`}>
|
||||
{c.name}
|
||||
</CategoryButton>
|
||||
))}
|
||||
</nav>
|
||||
|
||||
{/* products */}
|
||||
<main className="flex flex-col min-h-0 min-w-0">
|
||||
<div className="p-3 border-b border-line bg-surface flex items-center gap-2">
|
||||
<div className="relative flex-1">
|
||||
<Search size={18} className="absolute left-3 top-1/2 -translate-y-1/2 text-ink-3 pointer-events-none" />
|
||||
<Input value={query} onChange={e => setQuery(e.target.value)} placeholder={t('pos.search')} className="pl-10" data-testid="pos-search" aria-label={t('pos.search')} />
|
||||
</div>
|
||||
{query && <IconButton label={t('pos.clearSearch')} onClick={() => setQuery('')}><X size={18} /></IconButton>}
|
||||
</div>
|
||||
<div className="flex-1 overflow-y-auto p-3">
|
||||
{shown.length === 0 ? (
|
||||
<EmptyState title={query ? t('pos.noResults', { query }) : t('pos.noProducts')} />
|
||||
) : (
|
||||
<div className="grid gap-3" style={{ gridTemplateColumns: 'repeat(auto-fill, minmax(9.5rem, 1fr))' }} data-testid="pos-grid">
|
||||
{shown.map(p => <ProductCard key={p.id} product={p} onTap={() => tap(p)} onInfo={() => setDialog(p)} />)}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</main>
|
||||
|
||||
{/* cart */}
|
||||
<aside className="border-l border-line bg-surface flex flex-col min-h-0" data-testid="pos-cart">
|
||||
<div className="p-3 border-b border-line grid grid-cols-2 gap-1" role="radiogroup" aria-label={t('pos.orderType')}>
|
||||
{[['takeaway', ShoppingBag], ['here', Utensils]].map(([type, Icon]) => (
|
||||
<button key={type} type="button" role="radio" aria-checked={orderType === type} onClick={() => setOrderType(type)} data-testid={`type-${type}`}
|
||||
className={`min-h-[var(--x-control-md)] rounded-control flex items-center justify-center gap-2 text-body font-medium transition-colors duration-fast
|
||||
${orderType === type ? 'bg-accent text-accent-fg' : 'bg-surface-2 text-ink-2 hover:bg-surface-3'}`}>
|
||||
<Icon size={16} />{t(`pos.type.${type}`)}
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
<div className="flex-1 overflow-y-auto">
|
||||
{cart.length === 0
|
||||
? <EmptyState compact title={t('pos.cartEmpty')} description={t('pos.cartEmptyBody')} />
|
||||
: cart.map((line, i) => (
|
||||
<CartLine key={i} line={line} priced={quote.quote?.lines?.[i]} product={byId.get(line.product_id)}
|
||||
onQty={q => setCart(c => setLineQty(c, i, q))} />
|
||||
))}
|
||||
</div>
|
||||
<div className="border-t border-line p-3 grid gap-3">
|
||||
{quote.error && <p role="alert" className="text-label text-danger-ink">{t('pos.quoteFailed')}</p>}
|
||||
<div className="flex items-baseline justify-between">
|
||||
<span className="text-body-lg text-ink-2">{t('pos.total')}</span>
|
||||
<span className={`text-display font-semibold ${quote.pending ? 'opacity-50' : ''}`} data-testid="cart-total">
|
||||
{formatMoney(quote.quote?.total ?? 0)}
|
||||
</span>
|
||||
</div>
|
||||
<Button variant="secondary" icon={<Trash2 size={16} />} disabled={!cart.length} onClick={() => setCart([])}>{t('pos.clearCart')}</Button>
|
||||
</div>
|
||||
</aside>
|
||||
|
||||
{dialog && (
|
||||
<ProductDialog product={dialog} orderType={orderType} onClose={() => setDialog(null)}
|
||||
onAdd={line => setCart(c => addLine(c, line))} />
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
function CategoryButton({ active, colour, onClick, children, testId }) {
|
||||
return (
|
||||
<button type="button" onClick={onClick} data-testid={testId} aria-pressed={active}
|
||||
className={`min-h-[var(--x-control-lg)] px-3 rounded-control text-left text-body font-medium flex items-center gap-2 transition-colors duration-fast
|
||||
${active ? 'bg-accent-soft text-accent-ink' : 'text-ink hover:bg-surface-2'}`}>
|
||||
{colour && <span className="size-2.5 rounded-full shrink-0" style={{ background: colour }} aria-hidden="true" />}
|
||||
<span className="truncate">{children}</span>
|
||||
</button>
|
||||
)
|
||||
}
|
||||
|
||||
function ProductCard({ product, onTap, onInfo }) {
|
||||
const { t } = useTranslation('terminal')
|
||||
const { formatMoney } = useFormat()
|
||||
const unit = unitOf(product)
|
||||
return (
|
||||
<div className="relative bg-surface border border-line rounded-card shadow-raised overflow-hidden flex flex-col" data-testid="pos-product" data-product={product.id}>
|
||||
<button type="button" onClick={onTap} className="flex-1 flex flex-col text-left active:scale-[0.98] transition-transform duration-fast">
|
||||
{product.image_url
|
||||
? <img src={product.image_url} alt="" loading="lazy" className="w-full aspect-[4/3] object-cover bg-surface-2" />
|
||||
: <span className="w-full aspect-[4/3] bg-surface-2 flex items-center justify-center text-heading text-ink-3" aria-hidden="true">{product.name.slice(0, 1)}</span>}
|
||||
<span className="p-2 grid gap-0.5">
|
||||
<span className="text-body font-medium text-ink line-clamp-2">{product.name}</span>
|
||||
<span className="text-label text-ink-2">{formatMoney(product.price)}{unit ? ` / ${unit.label}` : ''}</span>
|
||||
</span>
|
||||
</button>
|
||||
<IconButton label={t('pos.checkPrice', { name: product.name })} size="sm" variant="secondary" onClick={onInfo}
|
||||
className="absolute top-1.5 right-1.5" data-testid="product-info">
|
||||
<Info size={16} />
|
||||
</IconButton>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
function CartLine({ line, priced, product, onQty }) {
|
||||
const { t } = useTranslation('terminal')
|
||||
const { formatMoney } = useFormat()
|
||||
const unit = product ? unitOf(product) : null
|
||||
const step = unit ? unit.step : 1
|
||||
const qtyText = unit ? `${Number(line.quantity).toFixed(step < 1 ? 1 : 0)} ${unit.label}` : `${line.quantity}×`
|
||||
return (
|
||||
<div className="px-3 py-2 border-b border-line grid gap-1" data-testid="cart-line" data-product={line.product_id}>
|
||||
<div className="flex items-start gap-2">
|
||||
<span className="flex-1 min-w-0 text-body font-medium text-ink">{product?.name ?? priced?.name}</span>
|
||||
<span className="text-body text-ink" data-testid="line-total">{priced ? formatMoney(priced.total) : '…'}</span>
|
||||
</div>
|
||||
{priced && (priced.entries.length > 0 || priced.removed.length > 0 || priced.notes) && (
|
||||
<div className="text-label text-ink-2 grid">
|
||||
{priced.entries.map((e, i) => <span key={i}>+ {e.name}{e.price ? ` (${formatMoney(e.price)})` : ''}</span>)}
|
||||
{priced.removed.map(n => <span key={n} className="text-danger-ink">− {n}</span>)}
|
||||
{priced.notes && <span className="italic">“{priced.notes}”</span>}
|
||||
</div>
|
||||
)}
|
||||
<div className="flex items-center gap-2">
|
||||
<IconButton size="sm" variant="secondary" label={t('pos.less')} onClick={() => onQty(line.quantity - step)}><Minus size={16} /></IconButton>
|
||||
<span className="min-w-12 text-center text-body font-semibold" data-testid="line-qty">{qtyText}</span>
|
||||
<IconButton size="sm" variant="secondary" label={t('pos.more')} onClick={() => onQty(line.quantity + step)}><Plus size={16} /></IconButton>
|
||||
{priced && line.quantity !== 1 && <span className="text-caption text-ink-3">{t('pos.each', { price: formatMoney(priced.unit_price) })}</span>}
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user