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/submit.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

60 lines
No EOL
1.9 KiB
JavaScript

const submitBlock = document.getElementById("submit")
{
const nextButton = document.getElementById("submit__next_button")
const ntfyTopicInput = document.getElementById("submit__ntfy_topic")
ntfyTopicInput.addEventListener("change", () => {
if (ntfyTopicInput.value.trim() === "") {
localStorage.removeItem("ntfy_topic")
} else {
localStorage.setItem("ntfy_topic", ntfyTopicInput.value.trim())
}
})
document.addEventListener("load", () => {
const x = localStorage.getItem("ntfy_topic")
if (x === undefined) {
return
}
ntfyTopicInput.value = x
})
nextButton.addEventListener("click", () => {
hide(errorIndicator)
hide(submitBlock)
show(workingIndicator)
const formValues = new FormData()
formValues.set("machine_number", state.machine)
{
const v = ntfyTopicInput.value.trim()
if (v !== "") {
formValues.set("ntfy_topic", ntfyTopicInput.value.trim())
}
}
fetch(`/api/v1/machines/${state.siteID}/watch`, {
method: "POST",
body: formValues,
})
.then(async (response) => {
hide(workingIndicator)
const responseBody = await response.json()
if (!response.ok) {
showError(responseBody.message)
show(submitBlock)
return
}
showSuccessHTML(`Successfully registered using ntfy topic <code>${responseBody["ntfy_topic"]}</code>`)
const anchor = document.createElement("a")
anchor.href = `https://ntfy.sh/${responseBody["ntfy_topic"]}`
anchor.innerText = "Click here to go to the ntfy topic"
anchor.target = "_blank"
showSuccessElement(anchor)
})
})
}