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