diff options
Diffstat (limited to 'src/static')
| -rw-r--r-- | src/static/main.js | 77 |
1 files changed, 52 insertions, 25 deletions
diff --git a/src/static/main.js b/src/static/main.js index 22a9048..2aac66f 100644 --- a/src/static/main.js +++ b/src/static/main.js @@ -12,6 +12,7 @@ var polygons = L.featureGroup().addTo(map); var heatmap = L.featureGroup().addTo(map); const CategoryField = document.getElementById('Category'); +const CountryField = document.getElementById('Country'); const LoadingIndicator = document.getElementById('LoadingIndicator'); const SubmitButton = document.getElementById('SubmitButton'); const form = document.getElementById('QueryForm'); @@ -60,13 +61,13 @@ function changeCategory() { SubmitButton.disabled = true; LoadingIndicator.hidden = false; const url = new URL(location); - url.searchParams.set("Category", CategoryField.value); + url.searchParams.set("category", CategoryField.value); history.pushState({}, "", url); markers.clearLayers(); polygons.clearLayers(); - fetchAll(CategoryField.value); + fetchAll(CountryField.value, CategoryField.value); } } @@ -75,25 +76,41 @@ function alterHeatMapParameter(event) { } async function fetchCategories() { - const response = await fetch("categories.json"); - try { - categories = await response.json(); - const CategoriesDataList = document.getElementById('CategoriesDataList'); - categories.forEach(function(item){ - var option = document.createElement('option'); - option.value = item; - option.innerHTML = item; - CategoriesDataList.appendChild(option); - }); - CategoryField.pattern = categories.join('|'); - } catch (error) { - alert(`Failed to fetch categories, are you online?\n${error.message}`) - } + try { + const response = await fetch("categories.json"); + + if (!response.ok) { + // If the response status is not OK, throw an error + throw new Error(`HTTP error! Status: ${response.status}`); + } + CategoryField.pattern = categories.join('|'); + } catch (error) { + alert(`Failed to fetch categories\n${error.message}`) + } + categories = await response.json(); + const CategoriesDataList = document.getElementById('CategoriesDataList'); + categories.forEach(function(item){ + var option = document.createElement('option'); + option.value = item; + option.innerHTML = item; + CategoriesDataList.appendChild(option); + }); + + } async function fetchChains() { - const response = await fetch("chains.json"); - known_store_chains = await response.json(); + try { + const response = await fetch("chains.json"); + known_store_chains = await response.json(); + if (!response.ok) { + // If the response status is not OK, throw an error + throw new Error(`HTTP error! Status: ${response.status}`); + } + } catch (error) { + alert(`Failed to fetch chains\n${error.message}`) + } + icons["Unknown"] = L.icon({ iconUrl: 'icons/Unknown.png', iconSize: [10, 16], @@ -108,21 +125,21 @@ async function fetchChains() { const queryString = window.location.search; const urlParams = new URLSearchParams(queryString); - const urlParam = urlParams.get('Category') + const urlParam = urlParams.get('category') const fieldValue = CategoryField.value // Retains saved value from input field on reload if (urlParam) { - fetchAll(urlParam); + fetchAll(CountryField.value, urlParam); CategoryField.value = urlParam; } else if (fieldValue !== "") { - fetchAll(fieldValue); + fetchAll(CountryField.value, fieldValue); } else { CategoryField.value = DefaultSearchValue; - fetchAll(DefaultSearchValue); + fetchAll(CountryField.value, DefaultSearchValue); } } @@ -170,9 +187,19 @@ function drawHeatmap() { var heat = L.heatLayer(shops_heatmap_format, {radius: parseInt(heatmap_radius_element.value)}).addTo(heatmap); } -async function fetchAll(Category) { - const response = await fetch(`all.json?Category=${Category}`) - shops = await response.json(); +async function fetchAll(country, category) { + let response; + try { + response = await fetch(`all.json?country=${country}&category=${category}`) + if (!response.ok) { + // If the response status is not OK, throw an error + throw new Error(`HTTP error! Status: ${response.status}`); + } + } catch (error) { + alert(`Failed to fetch data\n${error.message}`) + } + + shops = await response.json(); shops.forEach((shop) => addShop(shop)); // Add icons and polygons |
