summaryrefslogtreecommitdiff
path: root/frontend/admin/admin.js
diff options
context:
space:
mode:
Diffstat (limited to 'frontend/admin/admin.js')
-rw-r--r--frontend/admin/admin.js110
1 files changed, 110 insertions, 0 deletions
diff --git a/frontend/admin/admin.js b/frontend/admin/admin.js
new file mode 100644
index 0000000..ff1c463
--- /dev/null
+++ b/frontend/admin/admin.js
@@ -0,0 +1,110 @@
+const API_URL = "https://backend.laundryweb.altafcreator.com"
+
+async function login() {
+ const field = document.getElementById("pwfield");
+
+ const response = await fetch(`${API_URL}/admin_login`, {
+ method: "POST",
+ credentials: "include",
+ headers: {
+ "Content-Type": "application/json"
+ },
+ body: `{"password": "${field.value}"}`,
+ });
+
+ if (response.status == 202) window.location.href = "./panel.html";
+ else document.getElementById("passwordFeedback").style.display = "inherit";
+}
+
+async function checkLoginStatus() {
+ const response = await fetch(`${API_URL}/admin_check`, {
+ method: "POST",
+ credentials: "include"
+ });
+
+ return response.status == 202;
+}
+
+async function autoLogin() {
+ if (await checkLoginStatus()) {
+ window.location.href = "./panel.html";
+ }
+}
+
+async function panelLoginCheck() {
+ const msg = document.getElementById("unauthorised");
+ const authDiv = document.getElementById("authorised");
+
+ if (await checkLoginStatus()) {
+ msg.style.display = "none";
+ authDiv.style.display = "inherit";
+ return true;
+ } else {
+ msg.style.display = "inherit";
+ authDiv.style.display = "none";
+ return false;
+ }
+}
+
+async function syncMachineStatus() {
+ const response = await fetch(`${API_URL}/admin_machine_status`, {
+ method: "POST",
+ credentials: "include",
+ });
+ const data = await response.json();
+
+ for (let b = 0; b < data.length; b++) {
+ for (let m = 0; m < data[b].length; m++) {
+ const img = document.getElementById("h"+(b+1).toString()+"m"+(m+1).toString()+"img");
+ const dropdown = document.getElementById("h"+(b+1).toString()+"m"+(m+1).toString());
+
+ if (data[b][m] != "OUTOFSERVICE") {
+ if (m % 2 == 0) {
+ img.src = "/assets/img/dryer_off.png";
+ } else {
+ img.src = "/assets/img/washer_off.png";
+ }
+ dropdown.selectedIndex = 0;
+ } else {
+ if (m % 2 == 0) {
+ img.src = "/assets/img/dryer_down.png";
+ } else {
+ img.src = "/assets/img/washer_down.png";
+ }
+ dropdown.selectedIndex = 1;
+ }
+ }
+ }
+}
+
+async function overrideMachineStatus(block, machine) {
+ const img = document.getElementById("h"+block.toString()+"m"+machine.toString()+"img");
+ const dropdown = document.getElementById("h"+block.toString()+"m"+machine.toString());
+
+ const response = await fetch(`${API_URL}/override_status`, {
+ method: "POST",
+ credentials: "include",
+ headers: {
+ "Content-Type": "application/json"
+ },
+ body: `{"block": ${block}, "machine_id": ${machine}, "disabled": ${dropdown.selectedIndex == 1}}`,
+ });
+
+ if (response.status != 200) {
+ return;
+ }
+
+ if (dropdown.selectedIndex == 1) {
+ if (machine % 2 == 0) {
+ img.src = "/assets/img/washer_down.png";
+ } else {
+ img.src = "/assets/img/dryer_down.png";
+ }
+ } else {
+ if (machine % 2 == 0) {
+ img.src = "/assets/img/washer_off.png";
+ } else {
+ img.src = "/assets/img/dryer_off.png";
+ }
+ }
+}