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

@ -1,9 +1,17 @@
<?php <?php
function generateScriptData($input) :string function generateScriptData($phpArray) {
{ // Check if the array is associative and single-level
// Convert PHP array to JSON string if (is_array($phpArray) && count($phpArray) > 0 && count(array_filter(array_keys($phpArray), 'is_string')) === count($phpArray)) {
$jsonString = json_encode($input); // 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 echo "localStorage.setItem('$escapedKey', '$escapedValue');";
return "<script>let pageData = JSON.parse('$jsonString');</script>"; }
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"> <div class="admin-settings">
<h1>Activation Codes</h1> <div class="form-container" id="addActivationCodesForm">
<h1>Activation Codes</h1>
<h2>List Activation Codes</h2> <h2>List Activation Codes</h2>
<button type="button" onclick="listActivationCodes()">List Activation Codes</button><br> <button type="button" onclick="listActivationCodes()">List Activation Codes</button><br>
<h2>Add Activation Codes</h2> <h2>Add Activation Codes</h2>
<label for="activationCodeCount">Activation Code Count:</label> <label for="activationCodeCount">Activation Code Count:</label>
<input type="text" id="activationCodeCount" name="activationCodeCount" value="1" required><br> <input type="text" id="activationCodeCount" name="activationCodeCount" value="1" required><br>
<button type="button" onclick="addActivationCodes()">Add Activation Codes</button> <button type="button" onclick="addActivationCodes()">Add Activation Codes</button>
<br> <br>
<table id="codeListTable"></table> <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> </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> <hr>

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