adlerka.top/assets/script.js

397 lines
11 KiB
JavaScript
Raw Normal View History

const toggleButton = document.getElementById("toggle_button")
const navLinks = document.getElementById("navsite_list")
toggleButton.addEventListener('click', () => {
navLinks.classList.toggle("active")
})
2024-02-06 17:04:56 +01:00
let UserInfo;
2024-02-06 19:33:25 +01:00
function handleResponse(data, successMessage, failureMessage) {
const statusMessageContainer = document.getElementById("statusMessageContainer");
const statusMessage = document.createElement("div");
statusMessage.classList.add("status-message");
if (data.Status === 'Success') {
statusMessage.innerText = successMessage;
statusMessage.classList.add("success");
} else {
statusMessage.innerText = failureMessage;
statusMessage.classList.add("failure");
}
statusMessageContainer.appendChild(statusMessage);
// Automatically remove the message after 3 seconds
setTimeout(() => {
statusMessage.style.opacity = "0";
setTimeout(() => {
statusMessage.remove();
}, 500);
}, 3000);
}
2024-02-06 20:00:32 +01:00
async function showDashboardGreeting() {
document.getElementById("welcomeMsg").innerText = `Ahoj, ${UserInfo.FirstName}.`;
}
2024-02-06 16:24:57 +01:00
async function doAction(url, requestData, successMessage, failureMessage, silent = false) {
try {
const params = new URLSearchParams();
2024-02-05 22:15:10 +01:00
2024-02-06 16:24:57 +01:00
for (const key in requestData) {
params.append(key, requestData[key]);
}
const response = await fetch(url, {
method: 'POST',
body: params,
2024-02-03 16:08:26 +01:00
});
2024-02-06 16:24:57 +01:00
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
2024-02-05 21:21:04 +01:00
2024-02-06 16:24:57 +01:00
const data = await response.json();
2024-02-05 21:21:04 +01:00
2024-02-06 16:24:57 +01:00
if (!silent) {
handleResponse(data, successMessage, failureMessage);
}
return data;
} catch (error) {
console.error('Error:', error);
2024-02-05 21:21:04 +01:00
}
}
2024-02-06 16:24:57 +01:00
function handlePageResponse(data) {
2024-02-06 20:00:32 +01:00
const navbar = document.getElementById("navbar_container");
const pageArea = document.getElementById("page_container");
2024-02-06 16:24:57 +01:00
if (data.Navigation) {
navbar.innerHTML = data.Navigation;
}
if (data.Page) {
pageArea.innerHTML = data.Page;
2024-02-06 16:38:23 +01:00
if(data.PageLocation){
history.pushState({}, "", data.PageLocation);
}
2024-02-06 16:24:57 +01:00
}
2024-02-05 21:21:04 +01:00
}
2024-02-06 16:24:57 +01:00
function displayList(data, elementId, deleteFunction) {
const tableContainer = document.getElementById(elementId);
2024-02-04 09:50:04 +01:00
tableContainer.innerHTML = ""; // Clear previous content
const table = document.createElement("table");
table.classList.add("list-table");
const headerRow = table.insertRow(0);
for (const key in data[0]) {
const th = document.createElement("th");
th.appendChild(document.createTextNode(key));
headerRow.appendChild(th);
}
2024-02-06 16:24:57 +01:00
if (typeof deleteFunction === "function") {
2024-02-04 09:50:04 +01:00
const th = document.createElement("th");
th.appendChild(document.createTextNode("Delete"));
headerRow.appendChild(th);
}
for (const line of data) {
const dataRow = table.insertRow();
for (const key in line) {
const td = document.createElement("td");
td.appendChild(document.createTextNode(line[key]));
dataRow.appendChild(td);
}
2024-02-06 16:24:57 +01:00
if (typeof deleteFunction === "function") {
2024-02-04 09:50:04 +01:00
const td = document.createElement("td");
2024-02-06 16:24:57 +01:00
const deleteButton = document.createElement('button');
deleteButton.textContent = "Delete";
deleteButton.onclick = () => deleteFunction(line.ID);
td.appendChild(deleteButton);
2024-02-04 09:50:04 +01:00
dataRow.appendChild(td);
}
}
tableContainer.appendChild(table);
}
2024-02-06 16:24:57 +01:00
async function doPageAction(requestData, wantsReturn = false) {
try {
const response = await fetch('/page', {
method: 'POST',
body: new URLSearchParams(requestData),
});
2024-02-05 21:21:04 +01:00
2024-02-06 16:24:57 +01:00
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
2024-02-03 16:08:26 +01:00
2024-02-06 16:37:23 +01:00
const data = await response.json();
handlePageResponse(data);
2024-02-06 16:24:57 +01:00
if (wantsReturn) {
2024-02-06 16:37:23 +01:00
return data;
2024-02-06 16:24:57 +01:00
}
} catch (error) {
console.error('Error:', error);
}
2024-02-03 16:08:26 +01:00
}
2024-02-05 21:23:21 +01:00
function initAjax() {
2024-02-06 16:24:57 +01:00
const links = document.querySelectorAll('.navsite_link, .navpage_link');
2024-02-05 21:21:04 +01:00
2024-02-05 21:23:21 +01:00
links.forEach(function (link) {
link.addEventListener('click', function (e) {
e.preventDefault();
2024-02-05 21:25:41 +01:00
let site = this.dataset.site;
let page = this.dataset.page;
2024-02-05 21:23:21 +01:00
if (site && page) {
navigateTo(site, page);
}
});
2024-02-05 21:21:04 +01:00
});
2024-02-05 22:52:44 +01:00
onPageLoad();
2024-02-05 21:23:21 +01:00
}
2024-02-05 22:22:52 +01:00
2024-02-06 16:24:57 +01:00
document.addEventListener('DOMContentLoaded', initAjax);
2024-02-05 22:22:52 +01:00
2024-02-05 22:52:44 +01:00
function onPageLoad(){
let currentSite = localStorage.getItem("currentSite");
let currentPage = localStorage.getItem("currentPage");
if(currentSite === "home" && currentPage === "settings"){
if(document.getElementById("user-settings")){
2024-02-06 20:00:32 +01:00
populateUserInfoFields(UserInfo);
2024-02-05 22:52:44 +01:00
}
if(document.getElementById("admin-settings")){
listActivationCodes();
listUsers();
}
}
2024-02-06 20:00:32 +01:00
if(currentSite === "home" && currentPage === "account") {
showDashboardGreeting();
}
2024-02-05 22:52:44 +01:00
}
2024-02-06 16:24:57 +01:00
function navigateTo(site, page){
const data = {
action: "getPage",
site: site,
page: page,
};
2024-02-06 16:47:51 +01:00
doPageAction(data, true).then(() => {
2024-02-06 16:24:57 +01:00
localStorage.setItem("currentSite", site);
localStorage.setItem("currentPage", page);
onPageLoad();
2024-02-06 16:47:51 +01:00
});
2024-02-06 16:24:57 +01:00
}
function softReload(){
2024-02-06 17:04:56 +01:00
let currentSite = localStorage.getItem("currentSite");
let currentPage = localStorage.getItem("currentPage");
navigateTo(currentSite, currentPage);
umami.track("softReload");
2024-02-06 16:24:57 +01:00
}
function refreshNavbar(){
const data = {
action: "getNavigation",
};
doPageAction(data);
2024-02-06 17:04:56 +01:00
umami.track("refreshNavbar");
2024-02-06 16:24:57 +01:00
}
function logout() {
const data = {
action: "logout",
};
doAction('/account', data, "Logout Successful!", "Logout failed.").then(() => {
refreshNavbar();
2024-02-06 20:02:29 +01:00
navigateTo(localStorage.getItem("defaultSite"), localStorage.getItem("defaultPage"));
2024-02-06 17:04:56 +01:00
umami.track("logout");
2024-02-06 16:24:57 +01:00
});
}
2024-02-05 22:22:52 +01:00
function login() {
const email = document.getElementById("login_email").value;
const password = document.getElementById("login_password").value;
doLogin(email, password);
refreshNavbar();
softReload();
2024-02-06 20:00:32 +01:00
getUserInfo();
2024-02-05 22:22:52 +01:00
}
2024-02-06 16:24:57 +01:00
async function doLogin(email, password) {
const data = {
action: "login",
email: email,
password: password,
};
2024-02-05 22:22:52 +01:00
2024-02-06 16:24:57 +01:00
await doAction('/account', data, "Login Successful!", "Login failed. Please check your credentials.");
2024-02-06 17:04:56 +01:00
umami.track("login");
2024-02-05 22:22:52 +01:00
}
function register() {
const firstName = document.getElementById("register_firstName").value;
const lastName = document.getElementById("register_lastName").value;
const email = document.getElementById("register_email").value;
const password = document.getElementById("register_password").value;
const activationToken = document.getElementById("register_activationToken").value;
2024-02-06 16:24:57 +01:00
const data = {
action: "register",
firstname: firstName,
lastname: lastName,
email: email,
password: password,
activation_token: activationToken,
};
2024-02-05 22:22:52 +01:00
doRegister(data);
}
2024-02-06 16:24:57 +01:00
async function doRegister(requestData) {
await doAction('/account', requestData, "Registration Successful!", "Registration failed.");
2024-02-06 17:04:56 +01:00
umami.track("register");
2024-02-05 22:22:52 +01:00
}
//User settings start
function changePassword() {
const oldPassword = document.getElementById("changeOldPassword").value;
const newPassword = document.getElementById("changeNewPassword").value;
2024-02-06 16:24:57 +01:00
const data = {
action: "change_password",
old_password: oldPassword,
new_password: newPassword,
};
2024-02-05 22:22:52 +01:00
doChangePassword(data, "Password change Successful!", "Password change failed.");
}
2024-02-06 16:24:57 +01:00
async function doChangePassword(requestData, successMessage, failureMessage) {
await doAction('/account', requestData, successMessage, failureMessage);
2024-02-06 17:04:56 +01:00
umami.track("passwordChange");
2024-02-05 22:22:52 +01:00
}
function updateUserProfile() {
const firstName = document.getElementById("updateFirstName").value;
const lastName = document.getElementById("updateLastName").value;
const nickname = document.getElementById("updateNickname").value;
const minecraftNick = document.getElementById("updateMinecraftNick").value;
2024-02-06 16:24:57 +01:00
const data = {
action: "update_user_profile",
first_name: firstName,
last_name: lastName,
nickname: nickname,
minecraft_nick: minecraftNick,
};
2024-02-05 22:22:52 +01:00
2024-02-06 16:24:57 +01:00
doAction('/account', data, "Profile update Successful!", "Profile update failed.");
2024-02-06 17:04:56 +01:00
umami.track("updateUserProfile");
2024-02-05 22:22:52 +01:00
}
function updateEmail() {
const newEmail = document.getElementById("updateNewEmail").value;
2024-02-06 16:24:57 +01:00
const data = {
action: "update_user_email",
email: newEmail,
};
2024-02-05 22:22:52 +01:00
2024-02-06 16:24:57 +01:00
doAction('/account', data, "Email update Successful!", "Email update failed.");
2024-02-06 17:04:56 +01:00
umami.track("updateEmail");
2024-02-05 22:22:52 +01:00
}
function populateUserInfoFields(userData) {
document.getElementById("updateFirstName").value = userData.FirstName || "";
document.getElementById("updateLastName").value = userData.LastName || "";
document.getElementById("updateNickname").value = userData.Nickname || "";
document.getElementById("updateMinecraftNick").value = userData.MinecraftNick || "";
document.getElementById("updateNewEmail").value = userData.Email || "";
}
2024-02-06 16:24:57 +01:00
function getUserInfo() {
const data = {
action: "get_user_info",
};
2024-02-05 22:22:52 +01:00
2024-02-06 16:24:57 +01:00
const result = doAction('/account', data, "User info retrieved Successfully!", "User info retrieval failed.", true);
2024-02-05 22:22:52 +01:00
if (result && result.Status === "Success") {
2024-02-06 17:04:56 +01:00
UserInfo = result.UserInfo;
2024-02-05 22:22:52 +01:00
}
}
//User settings end
//Admin settings start
2024-02-06 16:24:57 +01:00
function addActivationCodes() {
2024-02-05 22:22:52 +01:00
const count = document.getElementById("activationCodeCount").value;
2024-02-06 16:24:57 +01:00
const data = {
action: "add_activation_codes",
count: count,
};
2024-02-05 22:22:52 +01:00
2024-02-06 16:24:57 +01:00
const result = doAction('/account', data, "Activation codes added Successfully!", "Activation codes addition failed.");
2024-02-05 22:22:52 +01:00
displayList(result.ActivationCodes, "codeListTable", deleteActivationCode);
2024-02-06 17:04:56 +01:00
umami.track("addActivationCodes");
2024-02-05 22:22:52 +01:00
}
2024-02-06 16:24:57 +01:00
function listUsers() {
const data = {
action: "list_users",
};
2024-02-05 22:22:52 +01:00
2024-02-06 16:24:57 +01:00
const result = doAction('/account', data, "User list retrieved Successfully!", "User list retrieval failed.");
2024-02-05 22:22:52 +01:00
if (result && result.Status === "Success") {
displayList(result.Users, "userListTable", deleteUser);
}
}
2024-02-06 16:24:57 +01:00
function listActivationCodes() {
const data = {
action: "list_activation_codes",
};
2024-02-05 22:22:52 +01:00
2024-02-06 16:24:57 +01:00
const result = doAction('/account', data, "Activation code list retrieved Successfully!", "Activation code list retrieval failed.");
2024-02-05 22:22:52 +01:00
displayList(result.ActivationCodes, "codeListTable", deleteActivationCode);
}
function deleteUser(userId) {
2024-02-06 16:24:57 +01:00
const data = {
action: "delete_user",
user_id: userId,
};
2024-02-05 22:22:52 +01:00
2024-02-06 16:24:57 +01:00
doAction('/account', data, "User deleted Successfully!", "User deletion failed.");
2024-02-05 22:22:52 +01:00
listUsers();
2024-02-06 17:04:56 +01:00
umami.track("deleteUser");
2024-02-05 22:22:52 +01:00
}
function deleteActivationCode(activationCode) {
2024-02-06 16:24:57 +01:00
const data = {
action: "delete_activation_code",
activation_code: activationCode,
};
2024-02-05 22:22:52 +01:00
2024-02-06 16:24:57 +01:00
doAction('/account', data, "Activation code deleted Successfully!", "Activation code deletion failed.");
2024-02-05 22:22:52 +01:00
listActivationCodes();
2024-02-06 17:04:56 +01:00
umami.track("deleteActivationCode");
2024-02-05 22:22:52 +01:00
}
//Admin settings end