function handleCheckbox() { const extensionInput = document.getElementById("extensionInput"); const directLineInput = document.getElementById("directLineInput"); if (document.getElementById("directLineCheckbox").checked) { extensionInput.style.display = "none"; directLineInput.style.display = "block"; document.getElementById("extension").value = ""; } else { extensionInput.style.display = "block"; directLineInput.style.display = "none"; document.getElementById("directLine").value = ""; } } let backendUrl = "http://127.0.0.1:8090" let locations = JSON.parse("{}"); function submitForm() { let body = JSON.stringify( "{" + "\"name\":\"" + document.getElementById("full_name").value + "\"," + "\"title\":\"" + document.getElementById("title").value + "\"," + "\"email\":\"" + document.getElementById("email").value + "\"," + "\"location\":\"" + document.getElementById("location").value + "\"," + "\"extension\":\"" + document.getElementById("extension").value + "\"," + "\"hasExtension\":\"" + document.getElementById("hasExtension").value + "\"," + "\"cellNumber\":\"" + document.getElementById("cellNumber").value + "\"," + "\"hasCell\":\"" + document.getElementById("hasCell").value + "\"" + "}" ); console.log(body); fetchWithTimeout(backendUrl + "/", { timeout: 5000, method: 'POST', mode: 'cors', headers: { 'Accept': 'image/png', 'Content-Type': 'application/json' }, body: body }).then(response => { console.log(response); }); } function getLocations() { let locationsList = document.getElementById("location"); fetchWithTimeout(backendUrl + "/data/locations", { timeout: 5000, method: 'GET', mode: 'cors', headers: { 'Accept': 'application/json' } }).then(response => { response.text().then(text => { locations = JSON.parse(text); locationsList.innerText = ""; for (let i = 0; i < locations.length; i++) { locationsList.options[locationsList.options.length] = new Option(locations[i].name, locations[i].id); } }) }); } async function checkBackend() { const statusCircle = document.getElementById("statusCircle"); const previousState = statusCircle.classList.contains("connected") ? "connected" : "disconnected"; statusCircle.classList.replace(previousState, "loading"); try { const response = await fetchWithTimeout(backendUrl + "/heartbeat", { timeout: 5000, method: 'GET', mode: 'cors', headers: { 'Accept': 'application/json' } }); if (response.status === 200) { console.log('Backend up with code ', response.status); } else { console.log("Something weird happened: ", response.status); } statusCircle.classList.replace("loading", "connected"); } catch (err) { console.log("Backend down with error ", err); statusCircle.classList.replace("loading", "disconnected"); } getLocations(); } function saveBackendUrl() { let url = document.getElementById("newBackendUrlInput").value; document.getElementById("newBackendUrlInput").value = ""; if (!(url.includes("http://") || url.includes("https://") && url !== "")) { url = "http://" + url; } backendUrl = url; document.cookie = "backendUrl=" + backendUrl; document.getElementById("displayedUrl").innerHTML = "Current Backend Url: " + backendUrl; console.log("Saved new backendUrl ", backendUrl); checkBackend(); } function loadBackendUrl() { let cookieArray = document.cookie.split(";"); for (let i = 0; i < cookieArray.length; i++) { let cookie = cookieArray[i].split("="); if (cookie[0] === "backendUrl") { backendUrl = cookie[1]; document.getElementById("displayedUrl").innerHTML = "Current Backend Url: " + backendUrl; console.log("Set backendUrl to ", backendUrl); } else { console.log("Unable to load backendUrl from cookie, defaulting to 127.0.0.1:8090"); } } } async function fetchWithTimeout(resource, options = {}) { const { timeout = 8000 } = options; const controller = new AbortController(); const id = setTimeout(() => controller.abort(), timeout); const response = await fetch(resource, { ...options, signal: controller.signal }); clearTimeout(id); return response; }