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,3 +1,4 @@
<div class="admin-settings">
<div class="form-container" id="addActivationCodesForm"> <div class="form-container" id="addActivationCodesForm">
<h1>Activation Codes</h1> <h1>Activation Codes</h1>
@ -20,5 +21,5 @@
<br> <br>
<table id="userListTable"></table> <table id="userListTable"></table>
</div> </div>
</div>
<hr> <hr>

@ -1,6 +1,6 @@
<!-- 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"> <div class="form-container" id="updateUserProfileForm">
@ -42,6 +42,6 @@
<button type="button" onclick="changePassword()">Change Password</button> <button type="button" onclick="changePassword()">Change Password</button>
</div> </div>
</div>
<hr> <hr>