1import { Controller } from "@hotwired/stimulus" 2 3// Live free/busy status for kiosk push buttons on person profile. 4export default class extends Controller { 5 static targets = ["row", "liveBadge"] 6 static values = { url: String } 7 8 connect() { 9 this.refresh() 10 this.timer = setInterval(() => this.refresh(), 3000) 11 } 12 13 disconnect() { 14 if (this.timer) clearInterval(this.timer) 15 } 16 17 refresh() { 18 if (!this.urlValue) return 19 fetch(this.urlValue, { headers: { Accept: "application/json" }, cache: "no-store", credentials: "same-origin" }) 20 .then((r) => r.json()) 21 .then((data) => this.apply(data.terminals || [])) 22 .catch(() => { 23 if (this.hasLiveBadgeTarget) { 24 this.liveBadgeTarget.textContent = "Status offline" 25 this.liveBadgeTarget.className = "badge bg-warning text-dark" 26 } 27 }) 28 } 29 30 apply(terminals) { 31 if (this.hasLiveBadgeTarget) { 32 this.liveBadgeTarget.textContent = "Live · " + new Date().toLocaleTimeString() 33 this.liveBadgeTarget.className = "badge bg-success" 34 } 35 36 const byId = {} 37 terminals.forEach((t) => { byId[String(t.payment_terminal_id)] = t }) 38 39 this.rowTargets.forEach((row) => { 40 const id = row.dataset.terminalId 41 const t = byId[String(id)] 42 if (!t) return 43 44 const stateEl = row.querySelector('[data-role="state"]') 45 const detailEl = row.querySelector('[data-role="detail"]') 46 const pushBtn = row.querySelector('[data-role="push"]') 47 48 if (stateEl) { 49 stateEl.textContent = String(t.state || "").toUpperCase() 50 stateEl.className = "badge " + (t.state === "free" ? "bg-success" : (t.state === "busy" ? "bg-danger" : "bg-secondary")) 51 } 52 if (detailEl) detailEl.textContent = t.detail || "" 53 if (pushBtn) { 54 pushBtn.disabled = !t.free 55 pushBtn.className = "btn btn-sm " + (t.free ? "btn-primary" : "btn-outline-secondary") 56 } 57 }) 58 } 59};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.