- Floor editor: one toolbar over the map and the panels (grid on / off, grid resolution Ultra high ... Very low, edit the shape, Fit width / Whole plan, undo, save), panels top-aligned and sticky. - The floor's shape: a '+' on the edge under the pointer, press to add a corner (and drag it); resting on a corner shows a bubble to remove it (never below 3). - Areas: resize handles on the sides and corners, a colour (pastel, see-through; floor_items.color, migration 0041), the name repeated in the corner when tables cover it (live maps too). - Labels can be clicked again after they're placed (transparent fill, not none). - Management -> Tables: <Page> with the tabs above (?tab=), the list as a container, toolbar buttons one height. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
189 lines
14 KiB
React
189 lines
14 KiB
React
// Management → Tables → List (plan step 6.14b; was the v1 TablesConfigTab, kept in 6.8a next to
|
|
// the floor plans): every table by zone, for typing and changes in bulk. Zones as tabs (drag to
|
|
// reorder; a zone's name, prefix and colour; add many tables at once with the zone's prefix,
|
|
// numbered on from the last), a table's name (up to 6 characters, empty = numbered by the server),
|
|
// zone and seats; names used twice stand out; turn a table off (kept, not used) or delete it (not
|
|
// while it has an open order); pick several to move them to a zone, rename them in a series, or
|
|
// delete them.
|
|
import { useState } from 'react'
|
|
import { useMutation, useQueryClient } from '@tanstack/react-query'
|
|
import toast from 'react-hot-toast'
|
|
import { GripVertical, Pencil, Plus, Power, Trash2, X } from 'lucide-react'
|
|
import { errorMessage } from '@xenia/api'
|
|
import { useTranslation } from '@xenia/i18n'
|
|
import { Button, Dialog, EmptyState, IconButton, Spinner, cx } from '@xenia/ui'
|
|
import client from '../../api/client'
|
|
import { useLiveQuery } from '../../live/useLiveQuery'
|
|
import { Pill } from '../overview/parts'
|
|
import { BatchDialog, MoveDialog, RenameDialog, TableDialog, ZoneDialog } from './TableDialogs'
|
|
import { duplicateNames, reorder, tablesOf } from './tablesLogic'
|
|
|
|
const LIVE = ['table_list_changed'] // routers/tables.py
|
|
|
|
export default function TablesListTab() {
|
|
const { t } = useTranslation('manager')
|
|
const qc = useQueryClient()
|
|
const [inactive, setInactive] = useState(false)
|
|
const [tab, setTab] = useState('all')
|
|
const [picking, setPicking] = useState(false)
|
|
const [picked, setPicked] = useState(() => new Set())
|
|
const [dialog, setDialog] = useState(null) // { kind, table | zone | ids }
|
|
const [drag, setDrag] = useState(null) // { id, gap }
|
|
const tablesQ = useLiveQuery({ queryKey: ['tables-all', inactive], events: LIVE, queryFn: () => client.get('/api/tables/', { params: { include_inactive: inactive } }).then(r => r.data) })
|
|
const zonesQ = useLiveQuery({ queryKey: ['table-groups'], events: LIVE, queryFn: () => client.get('/api/tables/groups').then(r => r.data) })
|
|
const tables = tablesQ.data ?? []
|
|
const zones = zonesQ.data ?? []
|
|
const changed = () => { for (const k of ['tables-all', 'tables', 'table-groups']) qc.invalidateQueries({ queryKey: [k] }) }
|
|
const onError = err => toast.error(errorMessage(err, t))
|
|
const close = () => setDialog(null)
|
|
const stopPicking = () => { setPicking(false); setPicked(new Set()) }
|
|
const shown = tablesOf(tables, tab)
|
|
const dupes = duplicateNames(tables)
|
|
const allPicked = shown.length > 0 && shown.every(x => picked.has(x.id))
|
|
const pick = id => setPicked(p => { const n = new Set(p); if (n.has(id)) n.delete(id); else n.add(id); return n })
|
|
const pickAll = () => setPicked(p => { const n = new Set(p); shown.forEach(x => (allPicked ? n.delete(x.id) : n.add(x.id))); return n })
|
|
const name = x => x.label || t('tablesList.numbered', { n: x.number })
|
|
|
|
const update = useMutation({ mutationFn: ({ id, ...body }) => client.put(`/api/tables/${id}`, body), onSuccess: () => { toast.success(t('tablesList.saved')); changed() }, onError })
|
|
const remove = useMutation({
|
|
mutationFn: ({ id, hard }) => client.delete(`/api/tables/${id}`, { params: { hard } }),
|
|
onSuccess: (_r, v) => { toast.success(t(v.hard ? 'tablesList.deleted' : 'tablesList.turnedOff')); close(); changed() }, onError,
|
|
})
|
|
const reorderZones = useMutation({ mutationFn: ids => client.put('/api/tables/groups/reorder', ids), onSuccess: changed, onError })
|
|
/** The same change to many tables, one request each; says how many didn't take. */
|
|
const each = async (ids, fn, done) => {
|
|
const res = await Promise.allSettled(ids.map(fn))
|
|
const failed = res.filter(r => r.status === 'rejected').length
|
|
if (failed) toast.error(t('tablesList.someFailed', { count: failed }))
|
|
else toast.success(done)
|
|
close(); stopPicking(); changed()
|
|
}
|
|
|
|
if (tablesQ.isPending || zonesQ.isPending) return <div className="py-10 flex justify-center"><Spinner /></div>
|
|
const tabs = [{ id: 'all', label: t('tablesList.all') }, ...zones.map(z => ({ id: z.id, label: z.prefix ? `${z.prefix} · ${z.name}` : z.name, color: z.color, zone: z })),
|
|
...(tables.some(x => !x.group_id) ? [{ id: 'none', label: t('tablesList.noZone') }] : [])]
|
|
const count = id => tablesOf(tables, id).length
|
|
const zone = zones.find(z => z.id === tab)
|
|
const dropOn = (e, i) => { e.preventDefault(); const r = e.currentTarget.getBoundingClientRect(); setDrag(d => d && { ...d, gap: e.clientX < r.left + r.width / 2 ? i : i + 1 }) }
|
|
const drop = () => {
|
|
if (drag?.gap != null) {
|
|
const ids = zones.map(z => z.id)
|
|
const next = reorder(ids, drag.id, drag.gap)
|
|
if (next.join() !== ids.join()) reorderZones.mutate(next)
|
|
}
|
|
setDrag(null)
|
|
}
|
|
|
|
return (
|
|
<div className="rounded-card border border-line bg-surface shadow-raised min-w-0" data-testid="tables-list">
|
|
<div className="flex flex-wrap items-center gap-2 border-b border-line px-card py-card-y">
|
|
{picking || picked.size ? (<>
|
|
<Button size="sm" variant="ghost" icon={<X size={14} />} onClick={stopPicking}>{picked.size ? t('tablesList.picked', { count: picked.size }) : t('tablesList.pick')}</Button>
|
|
<Button size="sm" variant="ghost" onClick={pickAll}>{t(allPicked ? 'tablesList.unpickAll' : 'tablesList.pickAll')}</Button>
|
|
{picked.size > 0 && (<>
|
|
<Button size="sm" variant="secondary" onClick={() => setDialog({ kind: 'move' })}>{t('tablesList.moveZone')}</Button>
|
|
<Button size="sm" variant="secondary" onClick={() => setDialog({ kind: 'rename' })} data-testid="tables-rename">{t('tablesList.renameSeries')}</Button>
|
|
<Button size="sm" variant="danger" onClick={() => setDialog({ kind: 'deleteMany' })}>{t('tablesList.delete')}</Button>
|
|
</>)}
|
|
</>) : (
|
|
<span className="text-label text-ink-2">
|
|
{t('tablesList.counts', { tables: tables.length, zones: zones.length })}
|
|
{tables.some(x => !x.is_active) && <> · <span className="text-warning-ink">{t('tablesList.offCount', { count: tables.filter(x => !x.is_active).length })}</span></>}
|
|
</span>
|
|
)}
|
|
<span className="flex-1" />
|
|
<Button variant={inactive ? 'primary' : 'secondary'} aria-pressed={inactive} onClick={() => setInactive(v => !v)}>{t('tablesList.showOff')}</Button>
|
|
{!picking && <Button variant="secondary" onClick={() => setPicking(true)} data-testid="tables-pick">{t('tablesList.pick')}</Button>}
|
|
<Button variant="secondary" icon={<Plus size={16} />} onClick={() => setDialog({ kind: 'zone', zone: {} })} data-testid="zone-new">{t('tablesList.newZone')}</Button>
|
|
<Button icon={<Plus size={16} />} onClick={() => setDialog({ kind: 'table', table: null })} data-testid="table-new">{t('tablesList.newTable')}</Button>
|
|
</div>
|
|
|
|
<div className="flex flex-wrap border-b border-line px-card" role="tablist" aria-label={t('tablesList.zones')}>
|
|
{tabs.map(x => {
|
|
const i = x.zone ? zones.indexOf(x.zone) : -1
|
|
return (
|
|
<button key={x.id} type="button" role="tab" aria-selected={tab === x.id} onClick={() => { setTab(x.id); setPicked(new Set()) }}
|
|
draggable={!!x.zone} onDragStart={x.zone ? () => setDrag({ id: x.id, gap: null }) : undefined}
|
|
onDragOver={x.zone ? e => dropOn(e, i) : undefined} onDrop={x.zone ? drop : undefined} onDragEnd={() => setDrag(null)}
|
|
className={cx('relative flex items-center gap-1.5 px-4 py-2.5 text-label font-medium border-b-2 -mb-px transition-colors',
|
|
tab === x.id ? 'border-accent text-accent-ink' : 'border-transparent text-ink-2 hover:text-ink',
|
|
x.zone && 'cursor-grab', drag?.gap === i && x.zone && 'before:absolute before:left-0 before:top-1 before:bottom-1 before:w-0.5 before:bg-accent',
|
|
drag?.gap === i + 1 && x.zone && i === zones.length - 1 && 'after:absolute after:right-0 after:top-1 after:bottom-1 after:w-0.5 after:bg-accent')}
|
|
data-testid="zone-tab">
|
|
{x.zone && <GripVertical size={12} className="text-ink-3" aria-hidden="true" />}
|
|
{x.color && <span className="w-2 h-2 rounded-full shrink-0" style={{ background: x.color }} aria-hidden="true" />}
|
|
{x.label}<span className="text-caption text-ink-3">({count(x.id)})</span>
|
|
</button>
|
|
)
|
|
})}
|
|
</div>
|
|
|
|
{zone && (
|
|
<div className="flex flex-wrap items-center gap-3 px-card py-2.5 border-b border-line bg-surface-2">
|
|
<span className="font-semibold text-ink text-label">{zone.name}</span>
|
|
{zone.prefix && <Pill>{zone.prefix}</Pill>}
|
|
<Button size="sm" variant="ghost" icon={<Pencil size={13} />} onClick={() => setDialog({ kind: 'zone', zone })}>{t('tablesList.editZone')}</Button>
|
|
<Button size="sm" variant="secondary" icon={<Plus size={14} />} className="ml-auto" onClick={() => setDialog({ kind: 'batch', zone })} data-testid="tables-batch">{t('tablesList.addMany')}</Button>
|
|
</div>
|
|
)}
|
|
|
|
<div className="p-card">
|
|
{!shown.length ? <EmptyState title={t(inactive ? 'tablesList.none' : 'tablesList.noneActive')} /> : (
|
|
<ul className="rounded-card border border-line bg-surface divide-y divide-line overflow-hidden">
|
|
{shown.map((x, i) => {
|
|
const on = picked.has(x.id)
|
|
const busy = x.has_active_order
|
|
return (
|
|
<li key={x.id} className={cx('flex flex-wrap items-center gap-3 px-4 py-2.5', !x.is_active && 'opacity-60', on && 'bg-accent-soft')} data-testid="table-row" data-name={name(x)}>
|
|
{picking || picked.size
|
|
? <input type="checkbox" checked={on} onChange={() => pick(x.id)} aria-label={t('tablesList.pickOne', { name: name(x) })} className="w-4 h-4 accent-accent" />
|
|
: <span className="w-4 text-caption text-ink-3 tabular-nums text-center">{i + 1}</span>}
|
|
<span className="flex-1 min-w-24 font-semibold text-ink">{name(x)}</span>
|
|
{x.label && dupes.has(x.label) && <Pill tone="warning">{t('tablesList.duplicate')}</Pill>}
|
|
{x.seat_count != null && <span className="text-caption text-ink-2">{t('tablesList.seats', { count: x.seat_count })}</span>}
|
|
{tab === 'all' && x.group && <Pill>{x.group.name}</Pill>}
|
|
{!x.is_active && <Pill tone="warning">{t('tablesList.off')}</Pill>}
|
|
{!picking && !picked.size && (
|
|
<span className="flex gap-0.5">
|
|
<IconButton size="sm" variant="ghost" label={t('tablesList.edit', { name: name(x) })} onClick={() => setDialog({ kind: 'table', table: x })}><Pencil size={14} /></IconButton>
|
|
{x.is_active
|
|
? <IconButton size="sm" variant="ghost" disabled={busy} label={busy ? t('tablesList.hasOrder') : t('tablesList.turnOff', { name: name(x) })} onClick={() => setDialog({ kind: 'off', table: x })}><Power size={14} /></IconButton>
|
|
: <IconButton size="sm" variant="ghost" label={t('tablesList.turnOn', { name: name(x) })} onClick={() => update.mutate({ id: x.id, is_active: true })}><Power size={14} /></IconButton>}
|
|
<IconButton size="sm" variant="ghost" disabled={busy} label={busy ? t('tablesList.hasOrder') : t('tablesList.deleteOne', { name: name(x) })} onClick={() => setDialog({ kind: 'delete', table: x })}><Trash2 size={14} /></IconButton>
|
|
</span>
|
|
)}
|
|
</li>
|
|
)
|
|
})}
|
|
</ul>
|
|
)}
|
|
</div>
|
|
|
|
{dialog?.kind === 'table' && <TableDialog table={dialog.table} zones={zones} zoneId={zone?.id} onClose={close} onSaved={() => { close(); changed() }} />}
|
|
{dialog?.kind === 'zone' && <ZoneDialog zone={dialog.zone} onClose={close} onSaved={() => { close(); changed() }} />}
|
|
{dialog?.kind === 'batch' && <BatchDialog zone={dialog.zone} tables={tables} onClose={close} onSaved={() => { close(); changed() }} />}
|
|
{dialog?.kind === 'move' && <MoveDialog count={picked.size} zones={zones} onClose={close}
|
|
onMove={groupId => each([...picked], id => client.put(`/api/tables/${id}`, { group_id: groupId }), t('tablesList.moved', { count: picked.size }))} />}
|
|
{dialog?.kind === 'rename' && <RenameDialog tables={shown.filter(x => picked.has(x.id))} onClose={close}
|
|
onRename={names => { const ids = shown.filter(x => picked.has(x.id)).map(x => x.id); return each(ids, (id, k) => client.put(`/api/tables/${id}`, { label: names[k] }), t('tablesList.renamed', { count: ids.length })) }} />}
|
|
{(dialog?.kind === 'off' || dialog?.kind === 'delete') && (
|
|
<Dialog open size="sm" onClose={close} title={t(dialog.kind === 'off' ? 'tablesList.offTitle' : 'tablesList.deleteTitle', { name: name(dialog.table) })}
|
|
description={t(dialog.kind === 'off' ? 'tablesList.offBody' : 'tablesList.deleteBody')}
|
|
footer={<>
|
|
<Button variant="secondary" onClick={close}>{t('tablesList.back')}</Button>
|
|
<Button variant="danger" loading={remove.isPending} onClick={() => remove.mutate({ id: dialog.table.id, hard: dialog.kind === 'delete' })} data-testid="table-delete-confirm">
|
|
{t(dialog.kind === 'off' ? 'tablesList.turnOffYes' : 'tablesList.deleteYes')}
|
|
</Button>
|
|
</>} />
|
|
)}
|
|
{dialog?.kind === 'deleteMany' && (
|
|
<Dialog open size="sm" onClose={close} title={t('tablesList.deleteManyTitle', { count: picked.size })} description={t('tablesList.deleteManyBody')}
|
|
footer={<>
|
|
<Button variant="secondary" onClick={close}>{t('tablesList.back')}</Button>
|
|
<Button variant="danger" onClick={() => each([...picked], id => client.delete(`/api/tables/${id}`, { params: { hard: true } }), t('tablesList.deletedMany', { count: picked.size }))}>{t('tablesList.deleteYes')}</Button>
|
|
</>} />
|
|
)}
|
|
</div>
|
|
)
|
|
}
|