// 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

{t('track.notFound')}

if (!order) return

{t('loading')}

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 (
{placed &&

{t(booked ? 'track.booking.placed' : 'track.placed')}

}

{t(booked ? 'track.booking.title' : 'track.title', { ref: order.public_ref })}

{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')}`}

{rejected ? (

{order.rejection_reason &&

{t('track.reason', { reason: order.rejection_reason })}

}
) : (
    {steps.map((s, i) => { const done = i < at || order.status === 'delivered' const now = i === at && order.status !== 'delivered' return (
  1. {done ? {st(s)}
  2. ) })}
)} {(!booked || (order.lines ?? []).length > 0) &&
{(order.lines ?? []).map((l, i) => (
{l.quantity}× {l.name}{l.options?.length ? ` (${l.options.join(', ')})` : ''} {money(l.unit_price * l.quantity)}
))} {order.delivery_fee > 0 &&
{t('fee')}{money(order.delivery_fee)}
}
{t('total')}{money(order.total)}
{booked &&

{t('book.foodReady')}

}
} {!trackSteps(order.order_type, order.status).done &&

{t('track.keep')}

} {t('track.newOrder')}
) } function Page({ children }) { return
{children}
}