forked from Adleraci/adlerka.top
357 lines
11 KiB
JavaScript
357 lines
11 KiB
JavaScript
|
|
function doAccountAction(requestData, successMessage, failureMessage, silent=false) {
|
|
return fetch('/account', {
|
|
method: 'POST',
|
|
body: requestData,
|
|
})
|
|
.then(response => {
|
|
if (!response.ok) {
|
|
throw new Error(`HTTP error! Status: ${response.status}`);
|
|
}
|
|
return response.json();
|
|
})
|
|
.then(data => {
|
|
if(!silent) {
|
|
handleResponse(data, successMessage, failureMessage);
|
|
}
|
|
return data; // Returning the response data for further processing
|
|
})
|
|
.catch((error) => {
|
|
console.error('Error:', error);
|
|
});
|
|
}
|
|
|
|
|
|
|
|
function handlePageResponse(data){
|
|
if(data.Navigation){
|
|
document.getElementById("navbar").innerHTML = data.Navigation;
|
|
}
|
|
if(data.Page){
|
|
document.getElementById("pagearea").innerHTML = data.Page;
|
|
// if(data.PageLocation){
|
|
// history.pushState({}, "", data.PageLocation);
|
|
// }
|
|
}
|
|
}
|
|
|
|
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);
|
|
});
|
|
}
|
|
|
|
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');
|
|
delete_button.textContent = "Delete"
|
|
delete_button.onclick = function (){
|
|
delete_function(line.ID);
|
|
}
|
|
td.appendChild(delete_button);
|
|
dataRow.appendChild(td);
|
|
}
|
|
}
|
|
|
|
tableContainer.appendChild(table);
|
|
}
|
|
|
|
function handleResponse(data, SuccessMessage, failureMessage) {
|
|
const StatusMessageElement = document.getElementById("StatusMessage");
|
|
|
|
if (data.Status === 'Success') {
|
|
StatusMessageElement.innerText = SuccessMessage;
|
|
} else {
|
|
StatusMessageElement.innerText = failureMessage;
|
|
}
|
|
|
|
// Show the status message
|
|
StatusMessageElement.style.display = "block";
|
|
setTimeout(() => {
|
|
// Hide the status message after 3 seconds
|
|
StatusMessageElement.style.opacity = "0";
|
|
setTimeout(() => {
|
|
StatusMessageElement.style.display = "none";
|
|
// Reset opacity for future messages
|
|
StatusMessageElement.style.opacity = "1";
|
|
}, 500);
|
|
}, 3000);
|
|
}
|
|
|
|
function navigateTo(site, page){
|
|
const data = new URLSearchParams();
|
|
data.append("action", "getPage");
|
|
data.append("site", site);
|
|
data.append("page", page);
|
|
doPageAction(data).then((value) => {
|
|
localStorage.setItem("currentSite", site);
|
|
localStorage.setItem("currentPage", page);
|
|
onPageLoad();
|
|
// Expected output: "Success!"
|
|
});
|
|
}
|
|
|
|
function softReload(){
|
|
navigateTo(localStorage.getItem("currentSite"), localStorage.getItem("currentPage"));
|
|
}
|
|
|
|
function refreshNavbar(){
|
|
const data = new URLSearchParams();
|
|
data.append("action", "getNavigation");
|
|
doPageAction(data);
|
|
}
|
|
|
|
function logout() {
|
|
const data = new URLSearchParams();
|
|
data.append("action", "logout");
|
|
|
|
doAccountAction(data, "Logout Successful!", "Logout failed.").then(() => {
|
|
refreshNavbar();
|
|
navigateTo("", localStorage.getItem("defaultPage"));
|
|
// Expected output: "Success!"
|
|
});
|
|
}
|
|
|
|
function initAjax() {
|
|
let links = document.querySelectorAll('.navsite_link, .navpage_link');
|
|
|
|
// Add click event listener to each link
|
|
links.forEach(function (link) {
|
|
link.addEventListener('click', function (e) {
|
|
e.preventDefault();
|
|
|
|
// Get page and site information
|
|
let site = this.dataset.site;
|
|
let page = this.dataset.page;
|
|
|
|
if (site && page) {
|
|
navigateTo(site, page);
|
|
}
|
|
|
|
// You can use this information to update the URL or perform other actions
|
|
});
|
|
});
|
|
onPageLoad();
|
|
}
|
|
document.addEventListener('DOMContentLoaded', initAjax);
|
|
|
|
|
|
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();
|
|
}
|
|
}
|
|
}
|
|
|
|
//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
|