38 lines
1.1 KiB
JavaScript
38 lines
1.1 KiB
JavaScript
// Kitchen display: poll open orders and render them oldest-first so the
|
|
// pass works the tickets in the order they landed. Kiosk mode, no input.
|
|
const POLL_MS = 2000;
|
|
const root = document.getElementById("orders");
|
|
|
|
function ageClass(placedAt) {
|
|
const mins = (Date.now() - new Date(placedAt).getTime()) / 60000;
|
|
if (mins > 8) return "ticket late";
|
|
if (mins > 4) return "ticket warn";
|
|
return "ticket";
|
|
}
|
|
|
|
function render(orders) {
|
|
orders.sort((a, b) => new Date(a.placed_at) - new Date(b.placed_at));
|
|
root.innerHTML = "";
|
|
for (const o of orders) {
|
|
const el = document.createElement("div");
|
|
el.className = ageClass(o.placed_at);
|
|
el.innerHTML =
|
|
`<h2>#${o.number}</h2>` +
|
|
o.items.map((i) => `<span>${i.qty}x ${i.name}</span>`).join("");
|
|
root.appendChild(el);
|
|
}
|
|
}
|
|
|
|
async function tick() {
|
|
try {
|
|
const res = await fetch("/api/orders?state=open", { cache: "no-store" });
|
|
if (res.ok) render(await res.json());
|
|
} catch (err) {
|
|
// A blip on the pass network is not worth clearing the board over.
|
|
console.warn("orders poll failed", err);
|
|
}
|
|
}
|
|
|
|
tick();
|
|
setInterval(tick, POLL_MS);
|