Fix script on open page

This commit is contained in:
Bruno Rybársky 2024-02-05 22:52:44 +01:00
parent 2cade060cf
commit 7dbb38913b
4 changed files with 84 additions and 63 deletions

@ -126,12 +126,13 @@ function navigateTo(site, page){
data.append("site", site);
data.append("page", page);
doPageAction(data);
pageData.currentSite = site;
pageData.currentPage = page;
localStorage.setItem("currentSite", site);
localStorage.setItem("currentPage", page);
onPageLoad();
}
function softReload(){
navigateTo(pageData.currentSite, pageData.currentPage);
navigateTo(localStorage.getItem("currentSite"), localStorage.getItem("currentPage"));
}
function refreshNavbar(){
@ -146,7 +147,7 @@ function logout() {
doAccountAction(data, "Logout Successful!", "Logout failed.").then(() => {
refreshNavbar();
navigateTo("", pageData.defaultPage);
navigateTo("", localStorage.getItem("defaultPage"));
// Expected output: "Success!"
});
}
@ -170,10 +171,27 @@ function initAjax() {
// 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(currentSite === "home" && currentPage === "settings"){
if(document.getElementById("admin-settings")){
listActivationCodes();
listUsers();
}
}
}
//Login
function login() {
@ -279,8 +297,6 @@ async function getUserInfo() {
}
}
document.addEventListener('DOMContentLoaded', getUserInfo);
//User settings end
//Admin settings start
@ -337,8 +353,4 @@ function deleteActivationCode(activationCode) {
doAccountAction(data, "Activation code deleted Successfully!", "Activation code deletion failed.");
listActivationCodes();
}
document.addEventListener('DOMContentLoaded', listUsers);
document.addEventListener('DOMContentLoaded', listActivationCodes);
//Admin settings end

@ -1,9 +1,17 @@
<?php
function generateScriptData($input) :string
{
// Convert PHP array to JSON string
$jsonString = json_encode($input);
function generateScriptData($phpArray) {
// Check if the array is associative and single-level
if (is_array($phpArray) && count($phpArray) > 0 && count(array_filter(array_keys($phpArray), 'is_string')) === count($phpArray)) {
// Generate JavaScript code to save each array element to local storage
echo "<script>";
foreach ($phpArray as $key => $value) {
$escapedKey = addslashes($key); // Escape special characters in the key
$escapedValue = addslashes($value); // Escape special characters in the value
// Output JavaScript code with the JSON string
return "<script>let pageData = JSON.parse('$jsonString');</script>";
echo "localStorage.setItem('$escapedKey', '$escapedValue');";
}
echo "</script>";
} else {
echo "<script>console.error('Invalid PHP array. Must be single-level and associative.');</script>";
}
}

@ -1,24 +1,25 @@
<div class="form-container" id="addActivationCodesForm">
<h1>Activation Codes</h1>
<div class="admin-settings">
<div class="form-container" id="addActivationCodesForm">
<h1>Activation Codes</h1>
<h2>List Activation Codes</h2>
<button type="button" onclick="listActivationCodes()">List Activation Codes</button><br>
<h2>Add Activation Codes</h2>
<label for="activationCodeCount">Activation Code Count:</label>
<input type="text" id="activationCodeCount" name="activationCodeCount" value="1" required><br>
<button type="button" onclick="addActivationCodes()">Add Activation Codes</button>
<br>
<table id="codeListTable"></table>
<h2>List Activation Codes</h2>
<button type="button" onclick="listActivationCodes()">List Activation Codes</button><br>
<h2>Add Activation Codes</h2>
<label for="activationCodeCount">Activation Code Count:</label>
<input type="text" id="activationCodeCount" name="activationCodeCount" value="1" required><br>
<button type="button" onclick="addActivationCodes()">Add Activation Codes</button>
<br>
<table id="codeListTable"></table>
</div>
<hr>
<div class="form-container" id="listUsersForm">
<h1>List Users</h1>
<button type="button" onclick="listUsers()">List Users</button><br>
<br>
<table id="userListTable"></table>
</div>
</div>
<hr>
<div class="form-container" id="listUsersForm">
<h1>List Users</h1>
<button type="button" onclick="listUsers()">List Users</button><br>
<br>
<table id="userListTable"></table>
</div>
<hr>

@ -1,47 +1,47 @@
<!-- Centralized Status Message -->
<p id="StatusMessage"></p>
<div id="user-settings">
<button type="button" onclick="logout()">Logout</button><br>
<button type="button" onclick="logout()">Logout</button><br>
<div class="form-container" id="updateUserProfileForm">
<h1>Update User</h1>
<div class="form-container" id="updateUserProfileForm">
<h1>Update User</h1>
<h2>Profile</h2>
<label for="updateFirstName">First Name:</label>
<input type="text" id="updateFirstName" name="updateFirstName" required><br>
<h2>Profile</h2>
<label for="updateFirstName">First Name:</label>
<input type="text" id="updateFirstName" name="updateFirstName" required><br>
<label for="updateLastName">Last Name:</label>
<input type="text" id="updateLastName" name="updateLastName" required><br>
<label for="updateLastName">Last Name:</label>
<input type="text" id="updateLastName" name="updateLastName" required><br>
<label for="updateNickname">Nickname:</label>
<input type="text" id="updateNickname" name="updateNickname" required><br>
<label for="updateNickname">Nickname:</label>
<input type="text" id="updateNickname" name="updateNickname" required><br>
<label for="updateMinecraftNick">Minecraft Nick:</label>
<input type="text" id="updateMinecraftNick" name="updateMinecraftNick" required><br>
<label for="updateMinecraftNick">Minecraft Nick:</label>
<input type="text" id="updateMinecraftNick" name="updateMinecraftNick" required><br>
<button type="button" onclick="updateUserProfile()">Update Profile</button>
<button type="button" onclick="updateUserProfile()">Update Profile</button>
<br><br>
<br><br>
<h2>Email</h2>
<h2>Email</h2>
<label for="updateNewEmail">New Email:</label>
<input type="email" id="updateNewEmail" name="updateNewEmail" required><br>
<label for="updateNewEmail">New Email:</label>
<input type="email" id="updateNewEmail" name="updateNewEmail" required><br>
<button type="button" onclick="updateEmail()">Update Email</button>
<button type="button" onclick="updateEmail()">Update Email</button>
<br><br>
<br><br>
<h2>Password</h2>
<h2>Password</h2>
<label for="changeOldPassword">Old Password:</label>
<input type="password" id="changeOldPassword" name="changeOldPassword" required><br>
<label for="changeOldPassword">Old Password:</label>
<input type="password" id="changeOldPassword" name="changeOldPassword" required><br>
<label for="changeNewPassword">New Password:</label>
<input type="password" id="changeNewPassword" name="changeNewPassword" required><br>
<label for="changeNewPassword">New Password:</label>
<input type="password" id="changeNewPassword" name="changeNewPassword" required><br>
<button type="button" onclick="changePassword()">Change Password</button>
<button type="button" onclick="changePassword()">Change Password</button>
</div>
</div>
<hr>