Merge pull request 'feature: poll the orders API' (#1) from feature/poll-orders into main
This commit is contained in:
37
app.js
Normal file
37
app.js
Normal file
@@ -0,0 +1,37 @@
|
||||
// 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);
|
||||
Reference in New Issue
Block a user