93 lines
2.6 KiB
HTML
93 lines
2.6 KiB
HTML
<div class="center">
|
|
<form id="fax-form" action="submit">
|
|
|
|
<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) {
|
|
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> |