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
2023-03-05 01:32:53 +00:00

66 lines
No EOL
2 KiB
JavaScript

const selectSiteBlock = document.getElementById("select_site");
const siteIDInput = document.getElementById("select_site__site_id_input")
{
const nextButton = document.getElementById("select_site__next_button")
siteIDInput.addEventListener("change", () => {
if (siteIDInput.value.trim() === "") {
localStorage.removeItem("site")
} else {
localStorage.setItem("site", siteIDInput.value.trim())
}
})
document.addEventListener("load", () => {
})
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}.`)
show(selectSiteBlock)
return
}
state.siteID = siteIDInput.value.trim()
hide(selectSiteBlock)
show(selectMachineBlock)
})
})
}