async function checkOnline() { if (!navigator.onLine) { console.log("Offline (no network connection)"); return false; } try { const response = await fetch( "https://www.google.com/favicon.ico?" + new Date().getTime(), { mode: "no-cors", }, ); return true; } catch (error) { console.log("Connected to network but no internet access"); return false; } } function showDashboard() { const offline = document.querySelector(".offline"); offline.remove(); document.querySelector(".dashboard").style.display = "flex"; } const btnOffline = document.querySelector("#btn-offline-add"); const divActions = document.querySelector("#actions"); let visible = true; setInterval(async () => { const isOnline = await checkOnline(); //const isOnline = false; // REMOVE!!! if (!isOnline) { if (visible) { console.log("OFFLINE!!!"); Array.from(divActions.children).forEach( (el) => (el.style.display = "none"), ); visible = false; btnOffline.style.display = "block"; document.querySelector(".hd-left").addEventListener("click", () => { showDashboard(); }); } if (localStorage.getItem("refuelOfflineData")) { btnOffline.textContent = "Sync data"; btnOffline.setAttribute("disabled", "disabled"); } } if (isOnline && !visible) { console.log("BACK ONLINE!!!"); visible = true; btnOffline.removeAttribute("disabled", "disabled"); // TODO: show buttons back, add sync button instead of record creation // If user is in a process of adding new offline refuel record, let him finish // Clear the local storage on each login } //}, 5000); }, 1000); window.onload = async () => { const rawData = await fetch("/api/v1/vehicles/get", { method: "GET", credentials: "include", }); const data = await rawData.json(); console.log("Fetched:", data); localStorage.setItem("vehicles", JSON.stringify(data)); console.log(JSON.parse(localStorage.getItem("vehicles"))); }; btnOffline.addEventListener("click", async (e) => { e.preventDefault(); if (btnOffline.textContent == "Sync data") { if (!visible) { alert("You're still offline. Try again later"); return; } try { let data = localStorage.getItem("refuelOfflineData"); if (!data) { console.error("No offline data found"); alert("No offline data found"); return; } data = JSON.parse(data); const formData = new FormData(); for (const key in data) { if (data.hasOwnProperty(key)) { formData.append(key, data[key]); } } const res = await fetch("/refuel/create", { method: "POST", body: formData, credentials: "include", }); if (!res.ok) { throw new Error(`Server error: ${res.statusText}`); } localStorage.removeItem("refuelOfflineData"); location.reload(); } catch (err) { console.error(err); alert("Something went wrong"); location.reload(); } return; } document.querySelector("section.dashboard").style.display = "none"; try { vehicles = localStorage.getItem("vehicles"); if (vehicles === null) throw new Error("No data was saved locally"); vehicles = JSON.parse(vehicles); } catch (err) { console.error(err); const offline = document.createElement("div"); offline.classList.add("offline"); offline.innerHTML = `
No data was saved locally, please try again later
You can create an fuel record locally on your device and sync it later