diff options
Diffstat (limited to 'frontend/main.js')
| -rw-r--r-- | frontend/main.js | 63 |
1 files changed, 48 insertions, 15 deletions
diff --git a/frontend/main.js b/frontend/main.js index 5c72bef..cfacc86 100644 --- a/frontend/main.js +++ b/frontend/main.js @@ -17,24 +17,42 @@ if ('serviceWorker' in navigator) { // --- subscribe async function subscribe() { - if (!('serviceWorker' in navigator)) return; + if (!('serviceWorker' in navigator)) { + alert("err: no service worker"); + return; + } + + console.log(await Notification.requestPermission()); const registration = await navigator.serviceWorker.ready; - const subscription = await registration.pushManager.subscribe({ - userVisibleOnly: true, - applicationServerKey: urlBase64ToUint8Array(PUBLIC_VAPID_KEY), - }); - console.log(subscription); + try { + const subscription = await registration.pushManager.subscribe({ + userVisibleOnly: true, + applicationServerKey: urlBase64ToUint8Array(PUBLIC_VAPID_KEY), + }); + + console.log(subscription); + + console.log("sw regis pass, write to db"); + + const db_reply = await fetch(`${API_URL}/notifsubscribe`, { + method: 'POST', + credentials: "include", + body: JSON.stringify(subscription), + headers: { + "Content-Type": "application/json", + }, + }); + + console.log(db_reply) + + return db_reply.ok; + } catch (e) { + console.log("ERR in regis, ", e); + return false; + } - await fetch(`${API_URL}/notifsubscribe`, { - method: 'POST', - credentials: "include", - body: JSON.stringify(subscription), - headers: { - "Content-Type": "application/json", - }, - }) } /// copied from somewhere @@ -99,11 +117,14 @@ async function start() { if (data == "all good bro timer started") { window.location.href = "/timer/"; } + cookieStore.delete("last_used_url"); }); } // --- information loading + cookie setting (from server) async function information(urlParam = null) { + const urlCookie = await cookieStore.get("last_used_url"); + const response = await fetch(`${API_URL}/info`, { credentials: "include", method: "POST", @@ -191,7 +212,18 @@ async function updateMachines() { const end = Date.parse(status[2][i]); const minsLeft = Math.ceil((end - now) / 60000).toString(); machineTxts[i].innerHTML = minsLeft + " min(s) left"; - if (machineDetailImgs[0]) machineDetailTitles[i].innerHTML = minsLeft + " minutes left" + if (machineDetailImgs[0]) machineDetailTitles[i].innerHTML = minsLeft + " minute(s) left" + } else if (status[0][i] == "OUTOFSERVICE") { + if ((i + 1) % 2 == 0) { + machineImgs[i].src = "/assets/img/washer_down.png"; + if (machineDetailImgs[0]) machineDetailImgs[i].src = "/assets/img/washer_down.png"; + } else { + machineImgs[i].src = "/assets/img/dryer_down.png"; + if (machineDetailImgs[0]) machineDetailImgs[i].src = "/assets/img/dryer_down.png"; + } + machineTxts[i].innerHTML = "Down" + if (machineDetailImgs[0]) machineDetailTitles[i].innerHTML = "Out of Service" + if (machineDetailImgs[0]) machineDetailDescs[i].innerHTML = "This machine is currently out of service, and is unavailable to use." } else if (status[0][i] == "FINISHED") { if ((i + 1) % 2 == 0) { machineImgs[i].src = "/assets/img/washer_clothes.png"; @@ -234,6 +266,7 @@ async function startLoadTimers() { const timers = timersData[1]; const container = document.getElementById("timer-container") + if (timers.length > 0) container.innerHTML = ''; const textList = [] const progList = [] |
