feat(waiter): a table's earlier orders (platform-v2 step 4.7)
- GET /api/tables/{id}/history: the table's closed / cancelled orders, newest first,
for the waiters of its zone (ordering.orders.open), never older than the history
module's history_days (free tier 31, D-6), module.disabled when history is off;
each order with times, total (cancelled items left out), people and items.
- Waiter: a History tab on the table (and "This table's earlier orders" on a free
table); orders expand to their items; the last answer is kept on the phone and
shown offline, marked as saved; the plan's day limit is shown.
- Tests: test_table_history.py (4), waiter e2e +2.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,90 @@
|
||||
import { useEffect, useState } from 'react'
|
||||
import { useFormat, useTranslation } from '@xenia/i18n'
|
||||
import client from '../api/client'
|
||||
import { apiError } from '../api/errors'
|
||||
import db from '../db/posdb'
|
||||
|
||||
// A table's earlier orders (plan step 4.7): GET /api/tables/{id}/history, never older than
|
||||
// the venue's `history` module allows. The last answer is kept on the phone (`kv`
|
||||
// history:<table id>) and shown while offline, marked as saved.
|
||||
const CACHE = id => `history:${id}`
|
||||
|
||||
export default function TableHistory({ tableId }) {
|
||||
const { t } = useTranslation('waiter')
|
||||
const { formatTime, formatDate, formatMoney } = useFormat()
|
||||
const [state, setState] = useState({ loading: true, data: null, cached: false, error: '' })
|
||||
const [open, setOpen] = useState(null)
|
||||
|
||||
useEffect(() => {
|
||||
let alive = true
|
||||
client.get(`/api/tables/${tableId}/history`)
|
||||
.then(async r => {
|
||||
try { await db.kv.put({ key: CACHE(tableId), value: { ...r.data, saved_at: new Date().toISOString() } }) } catch { /* storage full: still shown */ }
|
||||
if (alive) setState({ loading: false, data: r.data, cached: false, error: '' })
|
||||
})
|
||||
.catch(async err => {
|
||||
const saved = await db.kv.get(CACHE(tableId)).then(row => row?.value ?? null).catch(() => null)
|
||||
if (alive) setState({ loading: false, data: saved, cached: !!saved, error: saved ? '' : apiError(err, t('history.failed')) })
|
||||
})
|
||||
return () => { alive = false }
|
||||
}, [tableId]) // eslint-disable-line react-hooks/exhaustive-deps
|
||||
|
||||
if (state.loading) return <p style={{ textAlign: 'center', color: 'var(--muted)', padding: 24 }}>{t('common.loading')}</p>
|
||||
if (state.error) return <p data-testid="history-error" style={{ textAlign: 'center', color: 'var(--danger)', padding: 24, fontSize: 13 }}>{state.error}</p>
|
||||
const orders = state.data?.orders ?? []
|
||||
const days = state.data?.history_days
|
||||
|
||||
return (
|
||||
<div data-testid="table-history" style={{ display: 'flex', flexDirection: 'column', gap: 8, padding: '10px 12px 24px' }}>
|
||||
{state.cached && (
|
||||
<div data-testid="history-cached" style={{ fontSize: 12, color: 'var(--muted)', textAlign: 'center' }}>
|
||||
{t('history.cached', { time: formatTime(state.data.saved_at) })}
|
||||
</div>
|
||||
)}
|
||||
{orders.length === 0 && (
|
||||
<p style={{ textAlign: 'center', color: 'var(--muted)', padding: 24, fontSize: 14 }}>{t('history.none')}</p>
|
||||
)}
|
||||
{orders.map(o => {
|
||||
const expanded = open === o.id
|
||||
return (
|
||||
<div key={o.id} data-testid="history-order" style={{ background: 'var(--bg2)', border: '1px solid var(--border)', borderRadius: 12, overflow: 'hidden' }}>
|
||||
<button onClick={() => setOpen(expanded ? null : o.id)} style={{
|
||||
width: '100%', display: 'flex', alignItems: 'center', gap: 10, padding: '12px 14px',
|
||||
background: 'none', border: 'none', cursor: 'pointer', textAlign: 'left', color: 'var(--text)',
|
||||
}}>
|
||||
<div style={{ flex: 1, minWidth: 0 }}>
|
||||
<div style={{ fontSize: 14, fontWeight: 700 }}>
|
||||
{formatDate(o.opened_at, 'dateShort')} · {formatTime(o.opened_at)}{o.closed_at ? `–${formatTime(o.closed_at)}` : ''}
|
||||
{o.status === 'cancelled' && <span style={{ marginLeft: 8, fontSize: 11, color: 'var(--danger)' }}>{t('history.cancelled')}</span>}
|
||||
</div>
|
||||
<div style={{ fontSize: 12, color: 'var(--muted)', marginTop: 2 }}>
|
||||
#{o.id} · {t('orders.items', { count: o.items.length })}{o.people.length ? ` · ${o.people.join(', ')}` : ''}
|
||||
</div>
|
||||
</div>
|
||||
<span style={{ fontSize: 15, fontWeight: 800, fontVariantNumeric: 'tabular-nums' }}>{formatMoney(o.total)}</span>
|
||||
</button>
|
||||
{expanded && (
|
||||
<div style={{ borderTop: '1px solid var(--border)', padding: '8px 14px 12px', display: 'flex', flexDirection: 'column', gap: 6 }}>
|
||||
{o.items.map(i => (
|
||||
<div key={i.id} style={{ display: 'flex', gap: 8, fontSize: 13, opacity: i.status === 'cancelled' ? 0.5 : 1 }}>
|
||||
<span style={{ fontWeight: 700, minWidth: 28 }}>×{i.quantity}</span>
|
||||
<span style={{ flex: 1, textDecoration: i.status === 'cancelled' ? 'line-through' : 'none' }}>
|
||||
{i.name}
|
||||
{i.options.length > 0 && <span style={{ color: 'var(--muted)' }}> · {i.options.join(', ')}</span>}
|
||||
</span>
|
||||
<span style={{ fontVariantNumeric: 'tabular-nums' }}>{formatMoney(i.total)}</span>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
})}
|
||||
{days != null && (
|
||||
<div data-testid="history-limit" style={{ fontSize: 11, color: 'var(--muted)', textAlign: 'center', marginTop: 4 }}>
|
||||
{t('history.limit', { count: days })}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user