Cofem/assets/script.js
2024-02-04 09:26:17 +01:00

52 lines
1.5 KiB
JavaScript

function doAccountAction(requestData, successMessage, failureMessage, silent=false) {
return fetch('/account', {
method: 'POST',
body: requestData,
})
.then(response => {
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
return response.json();
})
.then(data => {
if(!silent) {
handleResponse(data, successMessage, failureMessage);
}
return data; // Returning the response data for further processing
})
.catch((error) => {
console.error('Error:', error);
});
}
function handleResponse(data, SuccessMessage, failureMessage) {
const StatusMessageElement = document.getElementById("StatusMessage");
if (data.Status === 'Success') {
StatusMessageElement.innerText = SuccessMessage;
} else {
StatusMessageElement.innerText = failureMessage;
}
// Show the status message
StatusMessageElement.style.display = "block";
setTimeout(() => {
// Hide the status message after 3 seconds
StatusMessageElement.style.opacity = 0;
setTimeout(() => {
StatusMessageElement.style.display = "none";
// Reset opacity for future messages
StatusMessageElement.style.opacity = 1;
}, 500);
}, 3000);
}
function logout() {
const data = new URLSearchParams();
data.append("action", "logout");
doAccountAction(data, "Logout Successful!", "Logout failed.");
}