This repository has been archived on 2025-07-20. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
circuitbodge/circuit-laundry-notifier/static/selectSite.js
AKP caa8f92d65
Alter 4 files
Update `selectSite.js`
Update `submit.js`
Update `index.html`
Update `web.py`
2022-11-09 00:13:22 +00:00

69 lines
No EOL
2.1 KiB
JavaScript

const selectSiteBlock = document.getElementById("select_site");
{
const nextButton = document.getElementById("select_site__next_button")
const siteIDInput = document.getElementById("select_site__site_id_input")
siteIDInput.addEventListener("change", () => {
if (siteIDInput.value.trim() === "") {
localStorage.removeItem("site")
} else {
localStorage.setItem("site", siteIDInput.value.trim())
}
})
document.addEventListener("load", () => {
const x = localStorage.getItem("site")
if (x === undefined) {
return
}
siteIDInput.value = x
})
nextButton.addEventListener("click", () => {
hide(errorIndicator)
hide(selectSiteBlock)
if (siteIDInput.value === "") {
showError("Missing site ID")
show(selectSiteBlock)
return
}
show(workingIndicator)
hide(selectSiteBlock)
fetch(`/api/v1/machines/${siteIDInput.value.trim()}`)
.then(async (response) => {
hide(workingIndicator)
const responseBody = await response.json()
if (!response.ok) {
showError(responseBody.message)
show(selectSiteBlock)
return
}
let numberInUse = 0
for (let i = 0; i < responseBody.machines.length; i += 1) {
const machineObject = responseBody.machines[i]
if (machineObject["state"] === "IN_USE") {
addMachineToSelection(machineObject)
numberInUse += 1
}
}
setSiteName(responseBody.name)
if (numberInUse === 0) {
showError(`No machines in use at ${responseBody.name}. To select a different site, refresh the page.`)
return
}
state.siteID = siteIDInput.value.trim()
hide(selectSiteBlock)
show(selectMachineBlock)
})
})
}