Files
xenia-pos-local/manager_dashboard/src/pages/pricing/PricingParts.jsx
T
bonaminandClaude Opus 5.5 e3f4f3eeee feat(manager,waiter): money and weights typed with a comma or a dot everywhere
Owner 2026-11-23 (plan 6F.2): new DecimalInput / MoneyInput in @xenia/ui (keeps what is typed,
reports a number, null when empty, NaN when not a number) on parseNumber. The waiter app's starting
cash, weighed quantity and typed prices were type=number + parseFloat (a Greek keypad's comma was
dropped: 12,50 became 12 or nothing); now text fields read with parseNumber, and a wrong starting
cash shows a message. The manager's price rules, deals and order-total condition use the new
fields; every Number(x.replace(',', '.')) now reads through parseNumber (1.234,50 too). Guard test
packages/i18n/tests/typed-numbers.test.mjs refuses parseFloat, .replace(',', '.') and decimal
type=number fields in the apps. e2e: shift close with 20,50, waiter starting cash 25,50.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-23 07:08:54 +02:00

304 lines
18 KiB
React

// Pieces the pricing tabs share (plan step 6.7): the colour and day pickers, the condition list (AND) and the target list (OR), a searchable
// pick-several list, a tag picker, and the card a rule, a deal or a group shows as.
import { useState } from 'react'
import { ArrowDown, ArrowUp, Check, Pencil, Plus, Power, Trash2, X } from 'lucide-react'
import { SALES_POINTS, normalizeSalesPoints } from '@xenia/access'
import { latin, useFormat, useTranslation } from '@xenia/i18n'
import { Button, Dropdown, IconButton, Input, MoneyInput, Select, cx } from '@xenia/ui'
import { COLOURS } from './useCatalog'
import { OLD_PLACEHOLDERS, WEEKDAYS, blankCondition, conditionComplete, toggleIn } from './pricingLogic'
export function ColourPicker({ value, onChange, label }) {
return (
<div className="grid gap-2">
<span className="text-label font-semibold text-ink">{label}</span>
<div className="flex flex-wrap gap-2" role="radiogroup" aria-label={label}>
{COLOURS.map(c => (
<button key={c} type="button" role="radio" aria-checked={value === c} aria-label={c} onClick={() => onChange(c)}
className={cx('w-8 h-8 rounded-full grid place-items-center text-accent-fg ring-offset-2 ring-offset-surface', value === c && 'ring-2 ring-[var(--x-ink)]')}
style={{ background: c }}>
{value === c && <Check size={14} />}
</button>
))}
</div>
</div>
)
}
export function DayPicker({ value = [], onChange, testid }) {
const { t } = useTranslation('manager')
return (
<div className="flex flex-wrap gap-1.5" data-testid={testid}>
{WEEKDAYS.map(d => (
<button key={d} type="button" aria-pressed={value.includes(d)} onClick={() => onChange(toggleIn(value, d).sort())}
className={cx('px-3 py-1.5 rounded-control border text-label font-medium transition-colors duration-fast',
value.includes(d) ? 'border-accent bg-accent-soft text-accent-ink' : 'border-line bg-surface text-ink-2 hover:bg-surface-2')}>
{t(`signage.weekdays.${d}`)}
</button>
))}
</div>
)
}
/** Chips to pick several values (sales points, days…). */
export function Chips({ options, value, onChange, testid }) {
return (
<div className="flex flex-wrap gap-1.5" data-testid={testid}>
{options.map(o => (
<button key={o.value} type="button" aria-pressed={value.includes(o.value)} onClick={() => onChange(toggleIn(value, o.value))}
className={cx('px-3 py-1.5 rounded-control border text-label font-medium transition-colors duration-fast',
value.includes(o.value) ? 'border-accent bg-accent-soft text-accent-ink' : 'border-line bg-surface text-ink-2 hover:bg-surface-2')}>
{o.label}
</button>
))}
</div>
)
}
/** One of a few choices as buttons (scope, action…). */
export function Segmented({ options, value, onChange, testid }) {
return (
<div className="flex flex-wrap gap-2" role="radiogroup" data-testid={testid}>
{options.map(o => (
<button key={o.value} type="button" role="radio" aria-checked={value === o.value} onClick={() => onChange(o.value)} data-value={o.value}
className={cx('flex-1 min-w-[8rem] px-3 py-2 rounded-control border text-label font-medium text-center transition-colors duration-fast',
value === o.value ? 'border-accent bg-accent-soft text-accent-ink' : 'border-line bg-surface text-ink-2 hover:bg-surface-2')}>
{o.label}
</button>
))}
</div>
)
}
/** A searchable list to pick several (products, categories, zones). */
export function PickSeveral({ items, value, onChange, testid }) {
const { t } = useTranslation('manager')
const [q, setQ] = useState('')
const fold = v => latin(String(v)).toLowerCase()
const shown = q.trim() ? items.filter(i => fold(i.plain ?? i.name).includes(fold(q.trim()))) : items
const chosen = items.filter(i => value.includes(i.id))
return (
<div className="grid gap-2" data-testid={testid}>
{chosen.length > 0 && (
<div className="flex flex-wrap gap-1.5">
{chosen.map(i => (
<span key={i.id} className="inline-flex items-center gap-1 pl-2.5 pr-1 py-0.5 rounded-full bg-accent-soft text-accent-ink text-label">
{i.plain ?? i.name}
<button type="button" aria-label={t('pricing.remove')} onClick={() => onChange(value.filter(v => v !== i.id))} className="p-0.5 rounded-full hover:bg-surface"><X size={12} /></button>
</span>
))}
</div>
)}
{items.length > 8 && <Input size="sm" value={q} onChange={e => setQ(e.target.value)} placeholder={t('pricing.search')} aria-label={t('pricing.search')} />}
<ul className="max-h-48 overflow-y-auto rounded-control border border-line divide-y divide-line bg-surface">
{shown.length === 0 && <li className="px-3 py-2 text-label text-ink-3">{t('pricing.nothingFound')}</li>}
{shown.map(i => {
const on = value.includes(i.id)
return (
<li key={i.id}>
<button type="button" role="checkbox" aria-checked={on} onClick={() => onChange(toggleIn(value, i.id))} data-name={i.plain ?? i.name}
className={cx('w-full flex items-center gap-2.5 px-3 py-1.5 text-left text-label', on ? 'bg-accent-soft text-accent-ink' : 'text-ink hover:bg-surface-2')}>
<span className={cx('w-4 h-4 rounded border grid place-items-center shrink-0', on ? 'bg-accent border-accent text-accent-fg' : 'border-line-strong bg-surface')}>
{on && <Check size={12} />}
</span>
<span className="truncate">{i.name}</span>
</button>
</li>
)
})}
</ul>
</div>
)
}
export function TagPicker({ value, onChange, known }) {
const { t } = useTranslation('manager')
const [draft, setDraft] = useState('')
const add = () => { const v = draft.trim(); if (v && !value.includes(v)) onChange([...value, v]); setDraft('') }
return (
<div className="grid gap-2">
{known.length > 0 && <Chips options={known.map(k => ({ value: k, label: k }))} value={value} onChange={onChange} />}
<div className="flex gap-2">
<Input size="sm" value={draft} onChange={e => setDraft(e.target.value)} placeholder={t('pricing.newTag')} aria-label={t('pricing.newTag')}
onKeyDown={e => { if (e.key === 'Enter') { e.preventDefault(); add() } }} />
<Button size="sm" variant="secondary" onClick={add} disabled={!draft.trim()}>{t('pricing.add')}</Button>
</div>
{value.filter(v => !known.includes(v)).length > 0 && (
<Chips options={value.filter(v => !known.includes(v)).map(k => ({ value: k, label: k }))} value={value} onChange={onChange} />
)}
</div>
)
}
/** The products a rule or a deal looks at: any one of the rows (OR). */
export function TargetList({ value, onChange, types, catalog, testid }) {
const { t } = useTranslation('manager')
const set = (i, v) => onChange(value.map((x, j) => (j === i ? v : x)))
return (
<div className="grid gap-2" data-testid={testid}>
{value.map((tg, i) => (
<div key={i} className="grid gap-2 p-3 rounded-control border border-line bg-surface-2" data-testid="pricing-target">
<div className="flex items-center gap-2">
<Select size="sm" className="flex-1" value={tg.target_type} aria-label={t('pricing.target.type')}
onChange={e => set(i, { target_type: e.target.value, target_ids: [], target_tags: [] })}
options={types.map(v => ({ value: v, label: t(`pricing.target.types.${v}`) }))} />
{value.length > 1 && <IconButton size="sm" variant="ghost" label={t('pricing.remove')} onClick={() => onChange(value.filter((_, j) => j !== i))}><X size={14} /></IconButton>}
</div>
{tg.target_type === 'item' && <PickSeveral items={catalog.products} value={tg.target_ids} onChange={v => set(i, { ...tg, target_ids: v })} testid="pricing-target-products" />}
{tg.target_type === 'category' && <PickSeveral items={catalog.categories} value={tg.target_ids} onChange={v => set(i, { ...tg, target_ids: v })} testid="pricing-target-categories" />}
{tg.target_type === 'prep_zone' && <PickSeveral items={catalog.zones} value={tg.target_ids} onChange={v => set(i, { ...tg, target_ids: v })} />}
{tg.target_type === 'tag' && <TagPicker value={tg.target_tags} onChange={v => set(i, { ...tg, target_tags: v })} known={catalog.tags} />}
</div>
))}
<div><Button size="sm" variant="ghost" icon={<Plus size={14} />} onClick={() => onChange([...value, { target_type: types[0], target_ids: [], target_tags: [] }])}>{t('pricing.target.add')}</Button></div>
</div>
)
}
/** The conditions: all of them must hold (AND). */
export function ConditionList({ value, onChange, types, catalog, testid }) {
const { t } = useTranslation('manager')
const set = (i, v) => onChange(value.map((x, j) => (j === i ? v : x)))
return (
<div className="grid gap-2" data-testid={testid}>
{value.map((c, i) => (
<div key={i} className={cx('grid gap-2 p-3 rounded-control border bg-surface-2', conditionComplete(c) ? 'border-line' : 'border-warning')} data-testid="pricing-condition">
<div className="flex items-center gap-2">
{OLD_PLACEHOLDERS.includes(c.condition_type)
? <span className="flex-1 text-label text-ink-2">{t('pricing.condition.placeholder')}</span>
: <Select size="sm" className="flex-1" value={c.condition_type} aria-label={t('pricing.condition.type')}
onChange={e => set(i, blankCondition(e.target.value))}
options={types.map(v => ({ value: v, label: t(`pricing.condition.types.${v}`) }))} />}
<IconButton size="sm" variant="ghost" label={t('pricing.remove')} onClick={() => onChange(value.filter((_, j) => j !== i))}><X size={14} /></IconButton>
</div>
<ConditionFields cond={c} onChange={v => set(i, v)} catalog={catalog} />
</div>
))}
<div><Button size="sm" variant="ghost" icon={<Plus size={14} />} onClick={() => onChange([...value, blankCondition(types[0])])} data-testid="pricing-condition-add">{t('pricing.condition.add')}</Button></div>
</div>
)
}
function ConditionFields({ cond, onChange, catalog }) {
const { t } = useTranslation('manager')
const { t: tc } = useTranslation('common')
const p = cond.params ?? {}
const set = (k, v) => onChange({ ...cond, params: { ...p, [k]: v } })
const num = (k, low, step) => (
<Input size="sm" type="number" min={low} step={step} className="w-32" value={p[k] ?? ''} aria-label={t(`pricing.condition.fields.${k === 'min' ? cond.condition_type : k}`)}
onChange={e => set(k, e.target.value === '' ? null : Number(e.target.value))} />
)
const pair = (type, a, b) => (
<div className="grid grid-cols-2 gap-2">
<label className="grid gap-1 text-caption text-ink-2">{t('pricing.condition.from')}
<Input size="sm" type={type} value={p[a] ?? ''} onChange={e => set(a, e.target.value)} data-testid={`pricing-cond-${a}`} /></label>
<label className="grid gap-1 text-caption text-ink-2">{t('pricing.condition.to')}
<Input size="sm" type={type} value={p[b] ?? ''} onChange={e => set(b, e.target.value)} data-testid={`pricing-cond-${b}`} /></label>
</div>
)
switch (cond.condition_type) {
case 'time_range': return <>{pair('time', 'from', 'to')}<p className="text-caption text-ink-3">{t('pricing.condition.timeHint')}</p></>
case 'date_range': return pair('date', 'from', 'to')
case 'specific_date': return <DateList value={p.dates ?? []} onChange={v => set('dates', v)} />
case 'day_of_week': return <DayPicker value={p.days ?? []} onChange={v => set('days', v)} />
case 'min_item_quantity': return <label className="flex items-center gap-2 text-label text-ink-2">{num('min', 1, 1)}{t('pricing.condition.ofTheDish')}</label>
case 'min_order_value': return (
<label className="flex items-center gap-2 text-label text-ink-2">
<MoneyInput size="sm" className="w-32" value={p.min ?? null} aria-label={t('pricing.condition.fields.min_order_value')}
onChange={v => set('min', Number.isFinite(v) ? v : null)} />
{t('pricing.condition.orderTotal')}
</label>
)
case 'min_category_qty': return (
<div className="flex flex-wrap items-center gap-2">
{num('min', 1, 1)}
<span className="text-label text-ink-2">{t('pricing.condition.fromCategory')}</span>
<Select size="sm" className="flex-1 min-w-[10rem]" value={p.category_id ?? ''} aria-label={t('pricing.condition.fields.category_id')}
onChange={e => set('category_id', e.target.value === '' ? null : Number(e.target.value))}
placeholder={t('pricing.choose')} options={catalog.categories.map(c => ({ value: c.id, label: c.name }))} />
</div>
)
case 'order_channel': return <Chips options={SALES_POINTS.map(s => ({ value: s, label: tc(`salesPoints.${s}.name`) }))}
value={normalizeSalesPoints(p.channels)} onChange={v => set('channels', v)} />
case 'price_group_active': return (
<Select size="sm" value={p.price_group_id ?? ''} aria-label={t('pricing.condition.fields.price_group_id')}
onChange={e => set('price_group_id', e.target.value === '' ? null : Number(e.target.value))}
placeholder={catalog.groups.length ? t('pricing.choose') : t('pricing.condition.noGroups')}
options={catalog.groups.map(g => ({ value: g.id, label: g.name }))} />
)
case 'scene_active': return (
<Dropdown size="sm" value={p.scene_id ?? null} aria-label={t('pricing.condition.fields.scene_id')} data-testid="pricing-cond-scene"
onChange={v => set('scene_id', v == null || v === '' ? null : Number(v))}
placeholder={catalog.scenes.length ? t('pricing.choose') : t('pricing.condition.noScenes')}
options={catalog.scenes.map(s => ({ value: s.id, label: s.name, dot: s.color || undefined }))} />
)
default: return null
}
}
function DateList({ value, onChange }) {
const { t } = useTranslation('manager')
const { formatDate } = useFormat()
const [draft, setDraft] = useState('')
return (
<div className="grid gap-2">
<div className="flex gap-2">
<Input size="sm" type="date" value={draft} onChange={e => setDraft(e.target.value)} aria-label={t('pricing.condition.date')} />
<Button size="sm" variant="secondary" disabled={!draft || value.includes(draft)} onClick={() => { onChange([...value, draft].sort()); setDraft('') }}>{t('pricing.add')}</Button>
</div>
{value.length > 0 && <Chips options={value.map(d => ({ value: d, label: formatDate(d) }))} value={value} onChange={onChange} />}
</div>
)
}
/** A rule's, a deal's or a group's row: colour, name, badges, a summary, and its buttons. */
export function RuleCard({ colour, name, active, badges, summary, description, onToggle, onEdit, onDelete, onUp, onDown, testid, busy }) {
const { t } = useTranslation('manager')
return (
<li className={cx('flex items-center gap-3 p-3 rounded-card border bg-surface shadow-raised transition-opacity', active ? 'border-line' : 'border-line opacity-60')}
data-testid={testid} data-name={name} data-active={active ? 'true' : 'false'}>
<span className="w-1.5 self-stretch rounded-full shrink-0" style={{ background: colour }} aria-hidden="true" />
<div className="flex-1 min-w-0 grid gap-0.5">
<div className="flex items-center gap-2 flex-wrap">
<span className="text-body font-semibold text-ink truncate">{name}</span>
<span className={cx('px-2 py-0.5 rounded-full text-caption font-medium', active ? 'bg-success-soft text-success-ink' : 'bg-surface-2 text-ink-2')}>
{active ? t('pricing.on') : t('pricing.off')}
</span>
{badges}
</div>
{description && <p className="text-label text-ink-2 truncate">{description}</p>}
{summary && <p className="text-caption text-ink-3">{summary}</p>}
</div>
<div className="flex items-center gap-0.5 shrink-0">
{onUp && <IconButton size="sm" variant="ghost" label={t('pricing.up')} onClick={onUp} disabled={busy}><ArrowUp size={16} /></IconButton>}
{onDown && <IconButton size="sm" variant="ghost" label={t('pricing.down')} onClick={onDown} disabled={busy}><ArrowDown size={16} /></IconButton>}
<IconButton size="sm" variant="ghost" label={active ? t('pricing.turnOff') : t('pricing.turnOn')} onClick={onToggle} disabled={busy} data-testid="pricing-toggle"><Power size={16} /></IconButton>
<IconButton size="sm" variant="ghost" label={t('pricing.edit')} onClick={onEdit} data-testid="pricing-edit"><Pencil size={16} /></IconButton>
<IconButton size="sm" variant="ghost" label={t('pricing.delete')} onClick={onDelete} data-testid="pricing-delete"><Trash2 size={16} /></IconButton>
</div>
</li>
)
}
/** The tab's head: what it is, and its "New" button. */
export function TabHead({ title, intro, action }) {
return (
<header className="flex flex-wrap items-start gap-3">
<div className="flex-1 min-w-64 grid gap-1">
<h2 className="text-body-lg font-semibold text-ink">{title}</h2>
<p className="text-label text-ink-2 max-w-3xl">{intro}</p>
</div>
{action && <div className="shrink-0 ml-auto">{action}</div>}
</header>
)
}
/** "Fill in: …" under a dialog when something is missing. */
export function Missing({ problems, prefix }) {
const { t } = useTranslation('manager')
if (!problems.length) return null
return <p className="p-3 rounded-control bg-warning-soft text-warning-ink text-label" role="note" data-testid="pricing-missing">
{t('pricing.missing', { list: problems.map(p => t(`${prefix}.${p}`)).join(', ') })}
</p>
}