diff --git a/frontend/src/main.ts b/frontend/src/main.ts index c1e0b61..f0e0ac5 100644 --- a/frontend/src/main.ts +++ b/frontend/src/main.ts @@ -5,6 +5,8 @@ import { Size } from "./size"; import { Tooltip } from "./Tooltip"; import { loadReviews } from "./review"; +const domSelect = (query: string) => document.querySelector(query); + const tooltip = new Tooltip(document.getElementById("tooltip")!); type Square = { @@ -43,9 +45,9 @@ let currentBoundary: Array | null = null; async function fetchAndDisplayZipCode(coords: Coordinate) { if (currentBoundary && coords.longitude > currentBoundary[0] && - coords.latitude > currentBoundary[1] && + coords.latitude > currentBoundary[1] && coords.longitude < currentBoundary[2] && - coords.latitude < currentBoundary[3] + coords.latitude < currentBoundary[3] ) return; const response = await fetchZipCode(coords); @@ -53,7 +55,7 @@ async function fetchAndDisplayZipCode(coords: Coordinate) { currentBoundary = response.bbox; displayZipCode( - document.querySelector("#zip-code")!, + domSelect("#zip-code")!, response.nr, response.navn, response.visueltcenter ? { longitude: response.visueltcenter[0], latitude: response.visueltcenter[1] } : null, @@ -104,18 +106,18 @@ function displayZipCode( const position = convertCoordinateToPixels(center, mapSize); const rect = document.getElementById("map")!.getBoundingClientRect(); dot.style.display = "block"; - dot.style.left = position.x + rect.left + "px"; - dot.style.top = position.y + rect.top + document.documentElement.scrollTop + "px"; + dot.style.left = `${position.x + rect.left}px`; + dot.style.top = `${position.y + rect.top + document.documentElement.scrollTop}px`; // Draw boundary const bottomleft = convertCoordinateToPixels({ longitude: boundary.x1, latitude: boundary.y1 }, mapSize); - const topright = convertCoordinateToPixels({ longitude: boundary.x2, latitude: boundary.y2 }, mapSize); + const topright = convertCoordinateToPixels({ longitude: boundary.x2, latitude: boundary.y2 }, mapSize); boundaryElem.style.display = "block"; - boundaryElem.style.left = bottomleft.x + rect.left + "px"; - boundaryElem.style.top = topright.y + rect.top + document.documentElement.scrollTop + "px"; - boundaryElem.style.width = topright.x - bottomleft.x + "px"; - boundaryElem.style.height = bottomleft.y - topright.y + "px"; + boundaryElem.style.left = `${bottomleft.x + rect.left}px`; + boundaryElem.style.top = `${topright.y + rect.top + document.documentElement.scrollTop}px`; + boundaryElem.style.width = `${topright.x - bottomleft.x}px`; + boundaryElem.style.height = `${bottomleft.y - topright.y}px`; } function setupMap( @@ -123,7 +125,7 @@ function setupMap( coordsElement: HTMLParagraphElement, zipCodeElement: HTMLParagraphElement, ) { - const mapImg = document.querySelector("#map")!; + const mapImg = domSelect("#map")!; const fetcher = new Throttler(200); mapImg.addEventListener('mousemove', async (event: MouseEvent) => { @@ -163,9 +165,9 @@ function setupMap( function setupSearchBar(zipCodeElement: HTMLParagraphElement) { const searchBar = - document.querySelector("#search-bar")!; + domSelect("#search-bar")!; const searchInput = - document.querySelector("#search-input")!; + domSelect("#search-input")!; searchInput.addEventListener("keydown", (event) => { if (event.key.length === 1 && !"0123456789".includes(event.key)) @@ -177,11 +179,9 @@ function setupSearchBar(zipCodeElement: HTMLParagraphElement) { const inputValue = searchInput.value; if (!/^\d+$/.test(inputValue)) return; - const data = await ( - await fetch( - `https://api.dataforsyningen.dk/postnumre?nr=${inputValue}`, - ) - ).json(); + const data = await fetch( + `https://api.dataforsyningen.dk/postnumre?nr=${inputValue}`, + ).then((response) => response.json()) displayZipCode( zipCodeElement, @@ -199,34 +199,37 @@ function pageRedirects() { const mapRedirect = document.getElementById("map-redirect")! const mainElement = document.getElementById("main")! - + reviewRedirect.addEventListener("click", () => { - mainElement.innerHTML = `

Anmeldelser

-
${loadReviews()}
` + mainElement.innerHTML = /*html*/` +

Anmeldelser

+
${loadReviews()}
+ `; const dropdown = document.getElementById("dropdown")!; dropdown.classList.remove("enabled"); - + }); mapRedirect.addEventListener("click", () => { - mainElement.innerHTML = - ` - -
-
-
-

Postnummer ikke fundet

-

-

-
` + mainElement.innerHTML = /*html*/` + + +
+
+
+

Postnummer ikke fundet

+

+

+
+ `; const [mousePositionElement, coordsElement, zipCodeElement] = [ "#mouse-position", "#coords", "#zip-code", - ].map((id) => document.querySelector(id)!); + ].map((id) => domSelect(id)!); setupSearchBar(zipCodeElement); setupMap(mousePositionElement, coordsElement, zipCodeElement); const dropdown = document.getElementById("dropdown")!; @@ -240,11 +243,9 @@ function main() { location.href = "https://mozilla.org/firefox"; } - const [mousePositionElement, coordsElement, zipCodeElement] = [ - "#mouse-position", - "#coords", - "#zip-code", - ].map((id) => document.querySelector(id)!); + const mousePositionElement = domSelect("#mouse-position")!; + const coordsElement = domSelect("#coords")!; + const zipCodeElement = domSelect("#zip-code")!; setupSearchBar(zipCodeElement); setupMap(mousePositionElement, coordsElement, zipCodeElement);