2024-02-05 22:15:10 +01:00
|
|
|
|
2024-02-03 22:44:01 +01:00
|
|
|
function doAccountAction(requestData, successMessage, failureMessage, silent=false) {
|
2024-02-03 17:01:52 +01:00
|
|
|
return fetch('/account', {
|
2024-02-03 16:08:26 +01:00
|
|
|
method: 'POST',
|
|
|
|
body: requestData,
|
|
|
|
})
|
|
|
|
.then(response => {
|
|
|
|
if (!response.ok) {
|
|
|
|
throw new Error(`HTTP error! Status: ${response.status}`);
|
|
|
|
}
|
|
|
|
return response.json();
|
|
|
|
})
|
|
|
|
.then(data => {
|
2024-02-03 22:44:01 +01:00
|
|
|
if(!silent) {
|
|
|
|
handleResponse(data, successMessage, failureMessage);
|
|
|
|
}
|
2024-02-03 16:08:26 +01:00
|
|
|
return data; // Returning the response data for further processing
|
|
|
|
})
|
|
|
|
.catch((error) => {
|
|
|
|
console.error('Error:', error);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-02-05 21:21:04 +01:00
|
|
|
|
|
|
|
|
|
|
|
function handlePageResponse(data){
|
|
|
|
if(data.Navigation){
|
|
|
|
document.getElementById("navbar").innerHTML = data.Navigation;
|
|
|
|
}
|
|
|
|
if(data.Page){
|
|
|
|
document.getElementById("pagearea").innerHTML = data.Page;
|
2024-02-05 22:10:07 +01:00
|
|
|
// if(data.PageLocation){
|
|
|
|
// history.pushState({}, "", data.PageLocation);
|
|
|
|
// }
|
2024-02-05 21:21:04 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function doPageAction(requestData){
|
|
|
|
return fetch('/page', {
|
|
|
|
method: 'POST',
|
|
|
|
body: requestData,
|
|
|
|
})
|
|
|
|
.then(response => {
|
|
|
|
if (!response.ok) {
|
|
|
|
throw new Error(`HTTP error! Status: ${response.status}`);
|
|
|
|
}
|
|
|
|
return response.json();
|
|
|
|
})
|
|
|
|
.then(data => {
|
|
|
|
handlePageResponse(data);
|
|
|
|
})
|
|
|
|
.catch((error) => {
|
|
|
|
console.error('Error:', error);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-02-04 09:50:04 +01:00
|
|
|
function displayList(data, element_id, delete_function=null) {
|
|
|
|
const tableContainer = document.getElementById(element_id);
|
|
|
|
tableContainer.innerHTML = ""; // Clear previous content
|
|
|
|
|
|
|
|
const table = document.createElement("table");
|
|
|
|
table.classList.add("list-table");
|
|
|
|
|
|
|
|
// Create header row
|
|
|
|
const headerRow = table.insertRow(0);
|
|
|
|
for (const key in data[0]) {
|
|
|
|
const th = document.createElement("th");
|
|
|
|
th.appendChild(document.createTextNode(key));
|
|
|
|
headerRow.appendChild(th);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(typeof delete_function === "function") {
|
|
|
|
const th = document.createElement("th");
|
|
|
|
th.appendChild(document.createTextNode("Delete"));
|
|
|
|
headerRow.appendChild(th);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create data rows
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
if(typeof delete_function === "function") {
|
|
|
|
const td = document.createElement("td");
|
|
|
|
let delete_button = document.createElement('button');
|
2024-02-04 09:55:47 +01:00
|
|
|
delete_button.textContent = "Delete"
|
2024-02-04 09:50:04 +01:00
|
|
|
delete_button.onclick = function (){
|
|
|
|
delete_function(line.ID);
|
|
|
|
}
|
|
|
|
td.appendChild(delete_button);
|
|
|
|
dataRow.appendChild(td);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tableContainer.appendChild(table);
|
|
|
|
}
|
|
|
|
|
2024-02-03 16:08:26 +01:00
|
|
|
function handleResponse(data, SuccessMessage, failureMessage) {
|
|
|
|
const StatusMessageElement = document.getElementById("StatusMessage");
|
|
|
|
|
|
|
|
if (data.Status === 'Success') {
|
|
|
|
StatusMessageElement.innerText = SuccessMessage;
|
|
|
|
} else {
|
|
|
|
StatusMessageElement.innerText = failureMessage;
|
|
|
|
}
|
2024-02-04 09:26:17 +01:00
|
|
|
|
|
|
|
// Show the status message
|
|
|
|
StatusMessageElement.style.display = "block";
|
|
|
|
setTimeout(() => {
|
|
|
|
// Hide the status message after 3 seconds
|
2024-02-04 09:50:04 +01:00
|
|
|
StatusMessageElement.style.opacity = "0";
|
2024-02-04 09:26:17 +01:00
|
|
|
setTimeout(() => {
|
|
|
|
StatusMessageElement.style.display = "none";
|
|
|
|
// Reset opacity for future messages
|
2024-02-04 09:50:04 +01:00
|
|
|
StatusMessageElement.style.opacity = "1";
|
2024-02-04 09:26:17 +01:00
|
|
|
}, 500);
|
|
|
|
}, 3000);
|
2024-02-03 16:08:26 +01:00
|
|
|
}
|
|
|
|
|
2024-02-05 21:21:04 +01:00
|
|
|
function navigateTo(site, page){
|
|
|
|
const data = new URLSearchParams();
|
|
|
|
data.append("action", "getPage");
|
|
|
|
data.append("site", site);
|
|
|
|
data.append("page", page);
|
|
|
|
doPageAction(data);
|
2024-02-05 22:52:44 +01:00
|
|
|
localStorage.setItem("currentSite", site);
|
|
|
|
localStorage.setItem("currentPage", page);
|
|
|
|
onPageLoad();
|
2024-02-05 22:15:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function softReload(){
|
2024-02-05 22:52:44 +01:00
|
|
|
navigateTo(localStorage.getItem("currentSite"), localStorage.getItem("currentPage"));
|
2024-02-05 21:21:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function refreshNavbar(){
|
|
|
|
const data = new URLSearchParams();
|
|
|
|
data.append("action", "getNavigation");
|
|
|
|
doPageAction(data);
|
|
|
|
}
|
|
|
|
|
2024-02-03 16:08:26 +01:00
|
|
|
function logout() {
|
|
|
|
const data = new URLSearchParams();
|
|
|
|
data.append("action", "logout");
|
|
|
|
|
2024-02-04 09:50:04 +01:00
|
|
|
doAccountAction(data, "Logout Successful!", "Logout failed.").then(() => {
|
2024-02-05 21:21:04 +01:00
|
|
|
refreshNavbar();
|
2024-02-05 22:52:44 +01:00
|
|
|
navigateTo("", localStorage.getItem("defaultPage"));
|
2024-02-04 09:50:04 +01:00
|
|
|
// Expected output: "Success!"
|
|
|
|
});
|
2024-02-03 16:08:26 +01:00
|
|
|
}
|
|
|
|
|
2024-02-05 21:23:21 +01:00
|
|
|
function initAjax() {
|
2024-02-05 22:02:16 +01:00
|
|
|
let links = document.querySelectorAll('.navsite_link, .navpage_link');
|
2024-02-05 21:21:04 +01:00
|
|
|
|
|
|
|
// Add click event listener to each link
|
2024-02-05 21:23:21 +01:00
|
|
|
links.forEach(function (link) {
|
|
|
|
link.addEventListener('click', function (e) {
|
|
|
|
e.preventDefault();
|
2024-02-05 21:21:04 +01:00
|
|
|
|
2024-02-05 21:23:21 +01:00
|
|
|
// Get page and site information
|
2024-02-05 21:25:41 +01:00
|
|
|
let site = this.dataset.site;
|
|
|
|
let page = this.dataset.page;
|
2024-02-05 21:21:04 +01:00
|
|
|
|
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 21:23:21 +01:00
|
|
|
// You can use this information to update the URL or perform other actions
|
|
|
|
});
|
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
|
|
|
}
|
|
|
|
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");
|
2024-02-05 23:01:26 +01:00
|
|
|
console.log("We arrived on: " + currentSite + ":" + currentPage + ".");
|
2024-02-05 22:52:44 +01:00
|
|
|
if(currentSite === "home" && currentPage === "settings"){
|
|
|
|
if(document.getElementById("user-settings")){
|
|
|
|
getUserInfo();
|
2024-02-05 23:01:26 +01:00
|
|
|
console.log("Running user info");
|
2024-02-05 22:52:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if(currentSite === "home" && currentPage === "settings"){
|
|
|
|
if(document.getElementById("admin-settings")){
|
|
|
|
listActivationCodes();
|
|
|
|
listUsers();
|
2024-02-05 23:01:26 +01:00
|
|
|
console.log("Running admin info");
|
2024-02-05 22:52:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-05 22:22:52 +01:00
|
|
|
//Login
|
|
|
|
|
|
|
|
function login() {
|
|
|
|
const email = document.getElementById("login_email").value;
|
|
|
|
const password = document.getElementById("login_password").value;
|
|
|
|
doLogin(email, password);
|
|
|
|
refreshNavbar();
|
|
|
|
softReload();
|
|
|
|
}
|
|
|
|
|
|
|
|
function doLogin(email, password) {
|
|
|
|
const data = new URLSearchParams();
|
|
|
|
data.append("action", "login");
|
|
|
|
data.append("email", email);
|
|
|
|
data.append("password", password);
|
|
|
|
|
|
|
|
doAccountAction(data, "Login Successful!", "Login failed. Please check your credentials.");
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
const data = new URLSearchParams();
|
|
|
|
data.append("action", "register");
|
|
|
|
data.append("firstname", firstName);
|
|
|
|
data.append("lastname", lastName);
|
|
|
|
data.append("email", email);
|
|
|
|
data.append("password", password);
|
|
|
|
data.append("activation_token", activationToken);
|
|
|
|
|
|
|
|
doRegister(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
function doRegister(requestData) {
|
|
|
|
doAccountAction(requestData, "Registration Successful!", "Registration failed.");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//User settings start
|
|
|
|
|
|
|
|
function changePassword() {
|
|
|
|
const oldPassword = document.getElementById("changeOldPassword").value;
|
|
|
|
const newPassword = document.getElementById("changeNewPassword").value;
|
|
|
|
|
|
|
|
const data = new URLSearchParams();
|
|
|
|
data.append("action", "change_password");
|
|
|
|
data.append("old_password", oldPassword);
|
|
|
|
data.append("new_password", newPassword);
|
|
|
|
|
|
|
|
doChangePassword(data, "Password change Successful!", "Password change failed.");
|
|
|
|
}
|
|
|
|
|
|
|
|
function doChangePassword(requestData, successMessage, failureMessage) {
|
|
|
|
doAccountAction(requestData, successMessage, failureMessage);
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
const data = new URLSearchParams();
|
|
|
|
data.append("action", "update_user_profile");
|
|
|
|
data.append("first_name", firstName);
|
|
|
|
data.append("last_name", lastName);
|
|
|
|
data.append("nickname", nickname);
|
|
|
|
data.append("minecraft_nick", minecraftNick);
|
|
|
|
|
|
|
|
doAccountAction(data, "Profile update Successful!", "Profile update failed.");
|
|
|
|
}
|
|
|
|
|
|
|
|
function updateEmail() {
|
|
|
|
const newEmail = document.getElementById("updateNewEmail").value;
|
|
|
|
|
|
|
|
const data = new URLSearchParams();
|
|
|
|
data.append("action", "update_user_email");
|
|
|
|
data.append("email", newEmail);
|
|
|
|
|
|
|
|
doAccountAction(data, "Email update Successful!", "Email update failed.");
|
|
|
|
}
|
|
|
|
|
|
|
|
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 || "";
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getUserInfo() {
|
|
|
|
const data = new URLSearchParams();
|
|
|
|
data.append("action", "get_user_info");
|
|
|
|
|
|
|
|
const result = await doAccountAction(data, "User info retrieved Successfully!", "User info retrieval failed.", true);
|
|
|
|
|
|
|
|
if (result && result.Status === "Success") {
|
|
|
|
populateUserInfoFields(result.UserInfo);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//User settings end
|
|
|
|
|
|
|
|
//Admin settings start
|
|
|
|
|
|
|
|
async function addActivationCodes() {
|
|
|
|
const count = document.getElementById("activationCodeCount").value;
|
|
|
|
|
|
|
|
const data = new URLSearchParams();
|
|
|
|
data.append("action", "add_activation_codes");
|
|
|
|
data.append("count", count);
|
|
|
|
|
|
|
|
const result = await doAccountAction(data, "Activation codes added Successfully!", "Activation codes addition failed.");
|
|
|
|
|
|
|
|
displayList(result.ActivationCodes, "codeListTable", deleteActivationCode);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function listUsers() {
|
|
|
|
const data = new URLSearchParams();
|
|
|
|
data.append("action", "list_users");
|
|
|
|
|
|
|
|
const result = await doAccountAction(data, "User list retrieved Successfully!", "User list retrieval failed.");
|
|
|
|
|
|
|
|
if (result && result.Status === "Success") {
|
|
|
|
displayList(result.Users, "userListTable", deleteUser);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async function listActivationCodes() {
|
|
|
|
const data = new URLSearchParams();
|
|
|
|
data.append("action", "list_activation_codes");
|
|
|
|
|
|
|
|
const result = await doAccountAction(data, "Activation code list retrieved Successfully!", "Activation code list retrieval failed.");
|
|
|
|
|
|
|
|
displayList(result.ActivationCodes, "codeListTable", deleteActivationCode);
|
|
|
|
}
|
|
|
|
|
|
|
|
function deleteUser(userId) {
|
|
|
|
|
|
|
|
const data = new URLSearchParams();
|
|
|
|
data.append("action", "delete_user");
|
|
|
|
data.append("user_id", userId);
|
|
|
|
|
|
|
|
doAccountAction(data, "User deleted Successfully!", "User deletion failed.");
|
|
|
|
listUsers();
|
|
|
|
}
|
|
|
|
|
|
|
|
function deleteActivationCode(activationCode) {
|
|
|
|
|
|
|
|
const data = new URLSearchParams();
|
|
|
|
data.append("action", "delete_activation_code");
|
|
|
|
data.append("activation_code", activationCode);
|
|
|
|
|
|
|
|
doAccountAction(data, "Activation code deleted Successfully!", "Activation code deletion failed.");
|
|
|
|
listActivationCodes();
|
|
|
|
}
|
|
|
|
//Admin settings end
|