From 7dbb38913bf11e8bd0cc4503638a22a7c1b01caf Mon Sep 17 00:00:00 2001 From: bruno Date: Mon, 5 Feb 2024 22:52:44 +0100 Subject: [PATCH] Fix script on open page --- assets/script.js | 32 +++++++++++++++------- lib/script_data.php | 20 +++++++++----- templates/adminActions.html | 41 ++++++++++++++-------------- templates/userActions.html | 54 ++++++++++++++++++------------------- 4 files changed, 84 insertions(+), 63 deletions(-) diff --git a/assets/script.js b/assets/script.js index 2978b7a..5bd7943 100644 --- a/assets/script.js +++ b/assets/script.js @@ -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 diff --git a/lib/script_data.php b/lib/script_data.php index 41d6cc0..eff7baf 100644 --- a/lib/script_data.php +++ b/lib/script_data.php @@ -1,9 +1,17 @@ 0 && count(array_filter(array_keys($phpArray), 'is_string')) === count($phpArray)) { + // Generate JavaScript code to save each array element to local storage + echo ""; + echo "localStorage.setItem('$escapedKey', '$escapedValue');"; + } + echo ""; + } else { + echo ""; + } } \ No newline at end of file diff --git a/templates/adminActions.html b/templates/adminActions.html index af7e945..6725d27 100644 --- a/templates/adminActions.html +++ b/templates/adminActions.html @@ -1,24 +1,25 @@ -
-

Activation Codes

+
+
+

Activation Codes

-

List Activation Codes

-
-

Add Activation Codes

- -
- -
-
+

List Activation Codes

+
+

Add Activation Codes

+ +
+ +
+
+
+ +
+ +
+

List Users

+
+
+
+
- -
- -
-

List Users

-
-
-
-
-
\ No newline at end of file diff --git a/templates/userActions.html b/templates/userActions.html index 7c0cdf1..32446ab 100644 --- a/templates/userActions.html +++ b/templates/userActions.html @@ -1,47 +1,47 @@

+
+
-
+
+

Update User

-
-

Update User

+

Profile

+ +
-

Profile

- -
+ +
- -
+ +
- -
+ +
- -
+ - +

-

+

Email

-

Email

+ +
- -
+ - +

-

+

Password

-

Password

+ +
- -
+ +
- -
- - + +
-