feat: the table map (platform-v2 step 6.8, D-31)
- floor_plans / floor_items (0035), tables' plan_id + width/height/rotation/shape; /api/floor-plans (set-up, catalog.tables.manage, floor_plans.changed) and /api/floor-plans/live (each table's state, guests, waiters, time; zone rule) - @xenia/floorplan: the drawing (Floor, Item, TableShape, FloorView) and its geometry (grid snap in whole cells, outlines, chairs), tested - manager: Management -> Tables -> Floor plans (editor: drag, grid, irregular floor, walls/bar/labels/areas, background picture), Tables page List / Map - counter POS: Tables List / Map; waiter: Map mode, the list stays the default - tests: test_floor_plans.py, floor + geometry unit tests, manager/waiter/terminal e2e Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,99 @@
|
||||
// Management → Tables → Floor plans (plan step 6.8, D-31): the venue's floor plans by zone (a zone
|
||||
// can have several, e.g. "Ground floor" and "Upstairs"; a plan may have no zone), "New floor plan",
|
||||
// and the chosen plan's editor (FloorEditor). The phones, the counter and the Tables page draw
|
||||
// the saved plans live (@xenia/floorplan FloorView).
|
||||
import { useState } from 'react'
|
||||
import toast from 'react-hot-toast'
|
||||
import { Plus } from 'lucide-react'
|
||||
import { errorMessage } from '@xenia/api'
|
||||
import { useTranslation } from '@xenia/i18n'
|
||||
import { Button, Dialog, EmptyState, Field, Input, Select, Spinner, cx } from '@xenia/ui'
|
||||
import { useLiveQuery } from '../../live/useLiveQuery'
|
||||
import FloorEditor from './FloorEditor'
|
||||
import * as api from './floorApi'
|
||||
|
||||
const SIZES = { small: [800, 600], medium: [1200, 800], large: [1800, 1200] }
|
||||
|
||||
export default function FloorPlansTab() {
|
||||
const { t } = useTranslation('manager')
|
||||
const { data, refetch } = useLiveQuery({ queryKey: ['floor-plans'], queryFn: api.listPlans, events: api.FLOOR_EVENTS })
|
||||
const { data: groups = [] } = useLiveQuery({ queryKey: ['table-groups'], queryFn: api.listZones, events: ['table_list_changed'] })
|
||||
const [chosen, setChosen] = useState(null)
|
||||
const [creating, setCreating] = useState(false)
|
||||
const [version, setVersion] = useState(0) // a new editor after a save (it starts from what was saved)
|
||||
|
||||
if (!data) return <div className="flex justify-center p-8"><Spinner /></div>
|
||||
const plans = data.plans
|
||||
const plan = plans.find(p => p.id === chosen) ?? plans[0] ?? null
|
||||
const byZone = [...groups.map(g => ({ id: g.id, name: g.name, plans: plans.filter(p => p.group_id === g.id) })),
|
||||
{ id: null, name: t('floor.noZone'), plans: plans.filter(p => p.group_id == null || !groups.some(g => g.id === p.group_id)) }].filter(z => z.plans.length)
|
||||
|
||||
return (
|
||||
<div className="p-4 md:p-6 grid gap-4" data-testid="floor-plans">
|
||||
<nav className="grid gap-2" aria-label={t('floor.title')}>
|
||||
<p className="text-label text-ink-2 max-w-4xl">{t('floor.intro')}</p>
|
||||
<div className="flex flex-wrap items-end gap-x-4 gap-y-2">
|
||||
{byZone.map(z => (
|
||||
<div key={z.id ?? 'none'} className="grid gap-1">
|
||||
<span className="text-caption font-semibold text-ink-3 uppercase tracking-wide">{z.name}</span>
|
||||
<div className="flex flex-wrap gap-1.5">
|
||||
{z.plans.map(p => (
|
||||
<button key={p.id} type="button" onClick={() => setChosen(p.id)} aria-current={plan?.id === p.id ? 'true' : undefined} data-testid="floor-plan-item" data-name={p.name}
|
||||
className={cx('px-3 py-1.5 rounded-control border text-label font-medium transition-colors duration-fast text-left',
|
||||
plan?.id === p.id ? 'border-accent bg-accent-soft text-accent-ink' : 'border-line bg-surface hover:bg-surface-2 text-ink')}>
|
||||
{p.name}
|
||||
<span className="ml-2 text-caption text-ink-3 font-normal">{t('floor.tableCount', { count: data.tables.filter(x => x.plan_id === p.id).length })}</span>
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
<Button size="sm" variant="secondary" icon={<Plus size={16} />} onClick={() => setCreating(true)} data-testid="floor-new">{t('floor.new')}</Button>
|
||||
</div>
|
||||
</nav>
|
||||
<main className="min-w-0">
|
||||
{plan ? (
|
||||
<FloorEditor key={`${plan.id}:${version}`} plan={plan} data={data} groups={groups}
|
||||
onSaved={async () => { await refetch(); setVersion(v => v + 1) }}
|
||||
onDeleted={async () => { setChosen(null); await refetch(); setVersion(v => v + 1) }} />
|
||||
) : <EmptyState title={t('floor.none')} description={t('floor.noneHint')} action={<Button icon={<Plus size={16} />} onClick={() => setCreating(true)}>{t('floor.new')}</Button>} />}
|
||||
</main>
|
||||
{creating && <NewPlan groups={groups} onClose={() => setCreating(false)} onCreate={async body => {
|
||||
try {
|
||||
const p = await api.createPlan(body)
|
||||
setCreating(false)
|
||||
await refetch()
|
||||
setChosen(p.id)
|
||||
setVersion(v => v + 1)
|
||||
} catch (err) { toast.error(errorMessage(err, t)) }
|
||||
}} />}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
function NewPlan({ groups, onClose, onCreate }) {
|
||||
const { t } = useTranslation('manager')
|
||||
const [name, setName] = useState('')
|
||||
const [zone, setZone] = useState(groups[0]?.id ?? null)
|
||||
const [size, setSize] = useState('medium')
|
||||
return (
|
||||
<Dialog open size="sm" title={t('floor.new')} onClose={onClose}
|
||||
footer={<>
|
||||
<Button variant="secondary" onClick={onClose}>{t('floor.cancel')}</Button>
|
||||
<Button disabled={!name.trim()} data-testid="floor-create"
|
||||
onClick={() => onCreate({ name: name.trim(), group_id: zone, width: SIZES[size][0], height: SIZES[size][1], grid_on: true, grid_size: 40 })}>{t('floor.create')}</Button>
|
||||
</>}>
|
||||
<div className="grid gap-3">
|
||||
<Field label={t('floor.name')}><Input value={name} autoFocus maxLength={60} onChange={e => setName(e.target.value)} placeholder={t('floor.namePlaceholder')} data-testid="floor-new-name" /></Field>
|
||||
<Field label={t('floor.zone')} hint={t('floor.zoneHint')}>
|
||||
<Select value={zone ?? ''} onChange={e => setZone(e.target.value === '' ? null : Number(e.target.value))} data-testid="floor-new-zone"
|
||||
options={[...groups.map(g => ({ value: g.id, label: g.name })), { value: '', label: t('floor.noZone') }]} />
|
||||
</Field>
|
||||
<Field label={t('floor.size')} hint={t('floor.sizeHint')}>
|
||||
<Select value={size} onChange={e => setSize(e.target.value)}
|
||||
options={Object.keys(SIZES).map(k => ({ value: k, label: t(`floor.sizes.${k}`, { w: SIZES[k][0], h: SIZES[k][1] }) }))} />
|
||||
</Field>
|
||||
</div>
|
||||
</Dialog>
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user