60 lines
No EOL
1.9 KiB
JavaScript
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)
|
|
})
|
|
})
|
|
} |