summaryrefslogtreecommitdiff
path: root/frontend/admin/admin.js
blob: b78b1b389937543cd7a0ddef03926d533f3bf3a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
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 = 1; b <= data.length; b++) {
		for (let m = 1; m <= data[b].length; m++) {
			const img = document.getElementById("h"+b.toString()+"m"+m.toString()+"img");
			const dropdown = document.getElementById("h"+b.toString()+"m"+m.toString());

			if (data[b][m] != "OUTOFSERVICE") {
				if (m % 2 == 0) {
					img.src = "/assets/img/washer_down.png";
				} else {
					img.src = "/assets/img/dryer_down.png";
				}
				dropdown.selectedIndex = 1;
			} else {
				if (m % 2 == 0) {
					img.src = "/assets/img/washer_off.png";
				} else {
					img.src = "/assets/img/dryer_off.png";
				}
				dropdown.selectedIndex = 0;
			}
		}
	}
}

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";
		}
	}
}