Fix async

This commit is contained in:
Bruno Rybársky 2024-02-06 20:19:36 +01:00
parent f06337c2c7
commit dbfae461f3

@ -6,9 +6,9 @@ toggleButton.addEventListener('click', () => {
}) })
var UserInfo; let UserInfo;
function handleResponse(data, successMessage, failureMessage) { async function handleResponse(data, successMessage, failureMessage) {
const statusMessageContainer = document.getElementById("statusMessageContainer"); const statusMessageContainer = document.getElementById("statusMessageContainer");
const statusMessage = document.createElement("div"); const statusMessage = document.createElement("div");
@ -51,13 +51,13 @@ async function doAction(url, requestData, successMessage, failureMessage, silent
}); });
if (!response.ok) { if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`); console.error(`HTTP error! Status: ${response.status}`);
} }
const data = await response.json(); const data = await response.json();
if (!silent) { if (!silent) {
handleResponse(data, successMessage, failureMessage); await handleResponse(data, successMessage, failureMessage);
} }
return data; return data;
@ -66,7 +66,7 @@ async function doAction(url, requestData, successMessage, failureMessage, silent
} }
} }
function handlePageResponse(data) { async function handlePageResponse(data) {
const navbar = document.getElementById("navbar_container"); const navbar = document.getElementById("navbar_container");
const pageArea = document.getElementById("page_container"); const pageArea = document.getElementById("page_container");
@ -82,7 +82,7 @@ function handlePageResponse(data) {
} }
} }
function displayList(data, elementId, deleteFunction) { async function displayList(data, elementId, deleteFunction) {
const tableContainer = document.getElementById(elementId); const tableContainer = document.getElementById(elementId);
tableContainer.innerHTML = ""; // Clear previous content tableContainer.innerHTML = ""; // Clear previous content
@ -130,11 +130,11 @@ async function doPageAction(requestData, wantsReturn = false) {
}); });
if (!response.ok) { if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`); console.error(`HTTP error! Status: ${response.status}`);
} }
const data = await response.json(); const data = await response.json();
handlePageResponse(data); await handlePageResponse(data);
if (wantsReturn) { if (wantsReturn) {
return data; return data;
} }
@ -143,7 +143,7 @@ async function doPageAction(requestData, wantsReturn = false) {
} }
} }
function initAjax() { async function initAjax() {
const links = document.querySelectorAll('.navsite_link, .navpage_link'); const links = document.querySelectorAll('.navsite_link, .navpage_link');
links.forEach(function (link) { links.forEach(function (link) {
@ -156,29 +156,29 @@ function initAjax() {
} }
}); });
}); });
onPageLoad(); await onPageLoad();
} }
document.addEventListener('DOMContentLoaded', initAjax); document.addEventListener('DOMContentLoaded', initAjax);
function onPageLoad(){ async function onPageLoad(){
let currentSite = localStorage.getItem("currentSite"); let currentSite = localStorage.getItem("currentSite");
let currentPage = localStorage.getItem("currentPage"); let currentPage = localStorage.getItem("currentPage");
if(currentSite === "home" && currentPage === "settings"){ if(currentSite === "home" && currentPage === "settings"){
if(document.getElementById("user-settings")){ if(document.getElementById("user-settings")){
populateUserInfoFields(UserInfo); await populateUserInfoFields(UserInfo);
} }
if(document.getElementById("admin-settings")){ if(document.getElementById("admin-settings")){
listActivationCodes(); await listActivationCodes();
listUsers(); await listUsers();
} }
} }
if(currentSite === "home" && currentPage === "account") { if(currentSite === "home" && currentPage === "account") {
showDashboardGreeting(); await showDashboardGreeting();
} }
} }
function navigateTo(site, page){ async function navigateTo(site, page){
const data = { const data = {
action: "getPage", action: "getPage",
site: site, site: site,
@ -191,40 +191,45 @@ function navigateTo(site, page){
}); });
} }
function softReload(){ async function softReload(){
let currentSite = localStorage.getItem("currentSite"); let currentSite = localStorage.getItem("currentSite");
let currentPage = localStorage.getItem("currentPage"); let currentPage = localStorage.getItem("currentPage");
navigateTo(currentSite, currentPage); await navigateTo(currentSite, currentPage);
umami.track("softReload"); umami.track("softReload");
} }
function refreshNavbar(){ async function refreshNavbar(){
const data = { const data = {
action: "getNavigation", action: "getNavigation",
}; };
doPageAction(data); await doPageAction(data);
umami.track("refreshNavbar"); umami.track("refreshNavbar");
} }
function logout() { async function logout() {
const data = { const data = {
action: "logout", action: "logout",
}; };
doAction('/account', data, "Logout Successful!", "Logout failed.").then(() => { doAction('/account', data, "Logout Successful!", "Logout failed.")
refreshNavbar(); .then(async () => {
navigateTo(localStorage.getItem("defaultSite"), localStorage.getItem("defaultPage")); await refreshNavbar();
await navigateTo(localStorage.getItem("defaultSite"), localStorage.getItem("defaultPage"));
umami.track("logout"); umami.track("logout");
})
.catch((error) => {
// Handle errors if needed
console.error("An error occurred during logout:", error);
}); });
} }
function login() { async function login() {
const email = document.getElementById("login_email").value; const email = document.getElementById("login_email").value;
const password = document.getElementById("login_password").value; const password = document.getElementById("login_password").value;
doLogin(email, password); await doLogin(email, password);
getUserInfo(); await getUserInfo();
refreshNavbar(); await refreshNavbar();
softReload(); await softReload();
} }
async function doLogin(email, password) { async function doLogin(email, password) {
@ -238,7 +243,7 @@ async function doLogin(email, password) {
umami.track("login"); umami.track("login");
} }
function register() { async function register() {
const firstName = document.getElementById("register_firstName").value; const firstName = document.getElementById("register_firstName").value;
const lastName = document.getElementById("register_lastName").value; const lastName = document.getElementById("register_lastName").value;
const email = document.getElementById("register_email").value; const email = document.getElementById("register_email").value;
@ -254,7 +259,7 @@ function register() {
activation_token: activationToken, activation_token: activationToken,
}; };
doRegister(data); await doRegister(data);
} }
async function doRegister(requestData) { async function doRegister(requestData) {
@ -264,7 +269,7 @@ async function doRegister(requestData) {
//User settings start //User settings start
function changePassword() { async function changePassword() {
const oldPassword = document.getElementById("changeOldPassword").value; const oldPassword = document.getElementById("changeOldPassword").value;
const newPassword = document.getElementById("changeNewPassword").value; const newPassword = document.getElementById("changeNewPassword").value;
@ -274,7 +279,7 @@ function changePassword() {
new_password: newPassword, new_password: newPassword,
}; };
doChangePassword(data, "Password change Successful!", "Password change failed."); await doChangePassword(data, "Password change Successful!", "Password change failed.");
} }
async function doChangePassword(requestData, successMessage, failureMessage) { async function doChangePassword(requestData, successMessage, failureMessage) {
@ -282,7 +287,7 @@ async function doChangePassword(requestData, successMessage, failureMessage) {
umami.track("passwordChange"); umami.track("passwordChange");
} }
function updateUserProfile() { async function updateUserProfile() {
const firstName = document.getElementById("updateFirstName").value; const firstName = document.getElementById("updateFirstName").value;
const lastName = document.getElementById("updateLastName").value; const lastName = document.getElementById("updateLastName").value;
const nickname = document.getElementById("updateNickname").value; const nickname = document.getElementById("updateNickname").value;
@ -296,11 +301,11 @@ function updateUserProfile() {
minecraft_nick: minecraftNick, minecraft_nick: minecraftNick,
}; };
doAction('/account', data, "Profile update Successful!", "Profile update failed."); await doAction('/account', data, "Profile update Successful!", "Profile update failed.");
umami.track("updateUserProfile"); umami.track("updateUserProfile");
} }
function updateEmail() { async function updateEmail() {
const newEmail = document.getElementById("updateNewEmail").value; const newEmail = document.getElementById("updateNewEmail").value;
const data = { const data = {
@ -308,11 +313,11 @@ function updateEmail() {
email: newEmail, email: newEmail,
}; };
doAction('/account', data, "Email update Successful!", "Email update failed."); await doAction('/account', data, "Email update Successful!", "Email update failed.");
umami.track("updateEmail"); umami.track("updateEmail");
} }
function populateUserInfoFields(userData) { async function populateUserInfoFields(userData) {
document.getElementById("updateFirstName").value = userData.FirstName || ""; document.getElementById("updateFirstName").value = userData.FirstName || "";
document.getElementById("updateLastName").value = userData.LastName || ""; document.getElementById("updateLastName").value = userData.LastName || "";
document.getElementById("updateNickname").value = userData.Nickname || ""; document.getElementById("updateNickname").value = userData.Nickname || "";
@ -320,7 +325,7 @@ function populateUserInfoFields(userData) {
document.getElementById("updateNewEmail").value = userData.Email || ""; document.getElementById("updateNewEmail").value = userData.Email || "";
} }
function getUserInfo() { async function getUserInfo() {
const data = { const data = {
action: "get_user_info", action: "get_user_info",
}; };
@ -336,7 +341,7 @@ function getUserInfo() {
//Admin settings start //Admin settings start
function addActivationCodes() { async function addActivationCodes() {
const count = document.getElementById("activationCodeCount").value; const count = document.getElementById("activationCodeCount").value;
const data = { const data = {
@ -350,7 +355,7 @@ function addActivationCodes() {
}); });
} }
function listUsers() { async function listUsers() {
const data = { const data = {
action: "list_users", action: "list_users",
}; };
@ -363,7 +368,7 @@ function listUsers() {
}); });
} }
function listActivationCodes() { async function listActivationCodes() {
const data = { const data = {
action: "list_activation_codes", action: "list_activation_codes",
}; };
@ -373,25 +378,25 @@ function listActivationCodes() {
}); });
} }
function deleteUser(userId) { async function deleteUser(userId) {
const data = { const data = {
action: "delete_user", action: "delete_user",
user_id: userId, user_id: userId,
}; };
doAction('/account', data, "User deleted Successfully!", "User deletion failed."); await doAction('/account', data, "User deleted Successfully!", "User deletion failed.");
listUsers(); await listUsers();
umami.track("deleteUser"); umami.track("deleteUser");
} }
function deleteActivationCode(activationCode) { async function deleteActivationCode(activationCode) {
const data = { const data = {
action: "delete_activation_code", action: "delete_activation_code",
activation_code: activationCode, activation_code: activationCode,
}; };
doAction('/account', data, "Activation code deleted Successfully!", "Activation code deletion failed."); await doAction('/account', data, "Activation code deleted Successfully!", "Activation code deletion failed.");
listActivationCodes(); await listActivationCodes();
umami.track("deleteActivationCode"); umami.track("deleteActivationCode");
} }
//Admin settings end //Admin settings end