Files
xenia-pos-cloud/connect_frontend/order-app/src/pages/TrackPage.jsx
T
bonaminandClaude Opus 5.5 3459c80611 feat(cloud): table bookings online, with food ordered ahead
Plan phase 3S, step S.6.
- New order types "reservation" (a table only, no items) and "preorder" (a table with food,
  priced at the online_preorder sales point); online_orders.party_size (migration 0005).
- services/ordering.py: the venue's booking settings (ordering.reservations in the snapshot):
  hours (none = any time), time grid, notice, days ahead (≤ 90), party size; res_slots per
  venue day, res_days, res_check (order.service_unavailable, order.party_invalid,
  order.time_invalid). The public menu's ordering summary gains `reservations`
  {enabled, with_food, party_max, days}; GET /api/orders/{site}/reservation-slots?day=.
- Bookings need a phone; a plain booking refuses items (order.items_not_allowed).
- Status steps for bookings: pending_acceptance → accepted | rejected; accepted → delivered
  (arrived) | cancelled (by the venue); ordinary orders unchanged.
- Order app: "Book a table" (people, day, time, optional food at the pre-order prices),
  booking summary at checkout, a booking tracking page (waiting, confirmed, welcome;
  declined / cancelled with the reason). en/el.
- Tests: tests/test_reservations.py (3), order-app logic test.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 19:31:21 +02:00

108 lines
5.5 KiB
React
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// The customer's order tracking (plan phase 3S, step S.4): /order/{site}/track/{token}. The
// token is the order's private code (the address is the customer's to keep). Polls the cloud
// until the order is done or not accepted; shows the steps, what was ordered and for when.
import { useEffect, useState } from 'react'
import { Link, useLocation, useParams } from 'react-router-dom'
import { Check, X } from 'lucide-react'
import { track } from '../api'
import { useT } from '../i18n'
import { BOOKING_TYPES, trackSteps } from '../logic'
const POLL_MS = 8000
export default function TrackPage() {
const { site, token } = useParams()
const { t, money, lang } = useT()
const placed = useLocation().state?.placed
const [order, setOrder] = useState(null)
const [missing, setMissing] = useState(false)
useEffect(() => {
let timer
let alive = true
const poll = async () => {
try {
const o = await track(token)
if (!alive) return
setOrder(o)
if (!trackSteps(o.order_type, o.status).done) timer = setTimeout(poll, POLL_MS)
} catch (err) {
if (!alive) return
if (err?.response?.status === 404) setMissing(true)
else timer = setTimeout(poll, POLL_MS) // offline for a moment: try again
}
}
poll()
return () => { alive = false; clearTimeout(timer) }
}, [token])
if (missing) return <Page><p className="p-6 text-center text-ink-2">{t('track.notFound')}</p></Page>
if (!order) return <Page><p className="p-6 text-center text-ink-2" role="status">{t('loading')}</p></Page>
const { steps, at } = trackSteps(order.order_type, order.status)
const rejected = order.status === 'rejected' || order.status === 'cancelled'
const booked = BOOKING_TYPES.includes(order.order_type) // a table booking (S.6)
const when = order.requested_for
? new Intl.DateTimeFormat(lang === 'el' ? 'el-GR' : 'en-GB', booked
? { weekday: 'long', day: 'numeric', month: 'long', hour: '2-digit', minute: '2-digit', hour12: false }
: { weekday: 'long', hour: '2-digit', minute: '2-digit' }).format(new Date(order.requested_for))
: null
const st = s => t(booked ? `track.booking.${s}` : `track.status.${s}`)
return (
<Page>
<div className="p-4 grid gap-4" data-testid="track" data-status={order.status}>
{placed && <p className="p-3 rounded-control bg-success-soft text-success-ink font-medium">{t(booked ? 'track.booking.placed' : 'track.placed')}</p>}
<header className="grid gap-1">
<h1 className="font-display text-2xl font-semibold" data-testid="track-ref">{t(booked ? 'track.booking.title' : 'track.title', { ref: order.public_ref })}</h1>
<p className="text-ink-2">
{booked ? `${t('book.tableFor', { count: order.party_size ?? 1 })} · ${when}`
: `${t(order.order_type === 'delivery' ? 'delivery' : 'takeaway')} · ${order.requested_for ? t('track.for', { when }) : t('track.asap')}`}
</p>
</header>
{rejected ? (
<div className="p-4 rounded-card bg-danger-soft text-danger-ink grid gap-1" role="status">
<p className="font-semibold flex items-center gap-2"><X size={18} aria-hidden="true" />{booked ? st(order.status) : t('track.rejected')}</p>
{order.rejection_reason && <p>{t('track.reason', { reason: order.rejection_reason })}</p>}
</div>
) : (
<ol className="grid gap-0" aria-label={st(order.status)}>
{steps.map((s, i) => {
const done = i < at || order.status === 'delivered'
const now = i === at && order.status !== 'delivered'
return (
<li key={s} className="flex items-center gap-3 py-2" aria-current={now ? 'step' : undefined}>
<span className={`w-7 h-7 rounded-full flex items-center justify-center shrink-0 text-sm font-semibold
${done ? 'bg-accent text-accent-fg' : now ? 'bg-accent-soft text-accent-ink ring-2 ring-accent' : 'bg-surface-2 text-ink-3'}`}>
{done ? <Check size={15} aria-hidden="true" /> : i + 1}
</span>
<span className={done || now ? 'text-ink font-medium' : 'text-ink-3'}>{st(s)}</span>
</li>
)
})}
</ol>
)}
{(!booked || (order.lines ?? []).length > 0) && <section className="p-4 rounded-card bg-surface border border-line grid gap-1.5">
{(order.lines ?? []).map((l, i) => (
<div key={i} className="flex justify-between gap-3 text-ink-2">
<span>{l.quantity}× {l.name}{l.options?.length ? ` (${l.options.join(', ')})` : ''}</span>
<span>{money(l.unit_price * l.quantity)}</span>
</div>
))}
{order.delivery_fee > 0 && <div className="flex justify-between text-ink-2"><span>{t('fee')}</span><span>{money(order.delivery_fee)}</span></div>}
<div className="flex justify-between text-lg font-semibold pt-1 border-t border-line"><span>{t('total')}</span><span data-testid="track-total">{money(order.total)}</span></div>
{booked && <p className="text-sm text-ink-2">{t('book.foodReady')}</p>}
</section>}
{!trackSteps(order.order_type, order.status).done && <p className="text-sm text-ink-2">{t('track.keep')}</p>}
<Link to={`/${site}`} className="text-center text-accent-ink font-semibold">{t('track.newOrder')}</Link>
</div>
</Page>
)
}
function Page({ children }) {
return <div className="mx-auto max-w-lg min-h-dvh bg-canvas">{children}</div>
}