adlerka.top/assets/script.js

340 lines
9.6 KiB
JavaScript
Raw Normal View History

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) {
const navbar = document.getElementById("navbar");
const pageArea = document.getElementById("pagearea");
if (data.Navigation) {
navbar.innerHTML = data.Navigation;
}
if (data.Page) {
pageArea.innerHTML = data.Page;
}
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:24:57 +01:00
if (wantsReturn) {
return await response.json();
} else {
const data = await response.json();
handlePageResponse(data);
}
} 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")){
getUserInfo();
}
if(document.getElementById("admin-settings")){
listActivationCodes();
listUsers();
}
}
}
2024-02-06 16:24:57 +01:00
function navigateTo(site, page){
const data = {
action: "getPage",
site: site,
page: page,
};
let pageActionStatus = doPageAction(data, true);
if(pageActionStatus.Status === "Success") {
localStorage.setItem("currentSite", site);
localStorage.setItem("currentPage", page);
onPageLoad();
}
}
function softReload(){
navigateTo(localStorage.getItem("currentSite"), localStorage.getItem("currentPage"));
}
function refreshNavbar(){
const data = {
action: "getNavigation",
};
doPageAction(data);
}
function logout() {
const data = {
action: "logout",
};
doAction('/account', data, "Logout Successful!", "Logout failed.").then(() => {
refreshNavbar();
navigateTo("", localStorage.getItem("defaultPage"));
});
}
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 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-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-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-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-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-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") {
populateUserInfoFields(result.UserInfo);
}
}
//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 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();
}
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();
}
//Admin settings end