Files
gothermal/faxSrc/assets/htmx/editor.html
T
2024-05-24 11:15:17 +01:00

95 lines
2.7 KiB
HTML

<div class="center">
<form id="fax-form" action="submit" hx-post="/send-fax" hx-swap="innerHTML" hx-target="#req-result">
<fieldset id="fax-message-boundaries">
<legend>FAX</legend>
<textarea name="fax-message" id="fax-message"></textarea>
</fieldset>
<br>
<button id="fax-submit-button" type="button">Submit</button>
<br>
<div id="req-result"></div>
</form>
</div>
<script>
// This function will persist the current editor data
// and send the user back to the login page
function reauth() {
localStorage.setItem("editor-text", document.getElementById("fax-message").value)
window.location.replace(window.location.origin + "/")
}
// this will load the editor data if we have it in storage
function loadStorageData() {
const savedContent = localStorage.getItem('editor-text');
if (savedContent) {
document.getElementById('fax-message').value = savedContent;
}
}
// If the fax is sent successfully we no longer need to cache it
// We will persist in the DB
function resetStorageData() {
localStorage.setItem("editor-text", document.getElementById("fax-message").value)
}
function handleSuccessfulPOST(data) {
console.log(data)
const response = JSON.parse(data)
var elem = document.getElementById("req-result")
if (response["state"] === "success") {
elem.innerHTML = "-> Fax sent successfully <-"
resetStorageData();
} else if (response["state"] === "unauthenticated") {
reauth();
} else {
elem.innerHTML = "-> Fax failed to send <-"
}
}
// Get a reference to the submit button
var submitBtn = document.getElementById("fax-submit-button");
// Add a click event listener to the submit button
submitBtn.addEventListener("click", function () {
// Get the fax message from the textarea
var faxMessage = document.getElementById("fax-message").value;
// Create the JSON object to send
var jsonData = {
fax_message: faxMessage
};
// Configure the fetch request
fetch("/send-fax", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify(jsonData)
})
.then(response => {
// Check if the response is OK
if (response.ok) {
// Request was successful, handle response if needed
return response.text();
} else {
// Request failed, handle error if needed
throw new Error("Error: " + response.statusText);
}
})
.then(data => {
handleSuccessfulPOST(data)
})
.catch(error => {
console.error(error);
});
});
loadStorageData();
</script>