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,4 +1,5 @@
<div class="form-container" id="addActivationCodesForm">
<div class="admin-settings">
<div class="form-container" id="addActivationCodesForm">
<h1>Activation Codes</h1>
<h2>List Activation Codes</h2>
@ -10,15 +11,15 @@
<br>
<table id="codeListTable"></table>
</div>
</div>
<hr>
<hr>
<div class="form-container" id="listUsersForm">
<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>

@ -1,9 +1,9 @@
<!-- 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">
<div class="form-container" id="updateUserProfileForm">
<h1>Update User</h1>
<h2>Profile</h2>
@ -41,7 +41,7 @@
<input type="password" id="changeNewPassword" name="changeNewPassword" required><br>
<button type="button" onclick="changePassword()">Change Password</button>
</div>
</div>
<hr>