From 705a95b3f52aa5346d8f87f1dd02c4b1f628187b Mon Sep 17 00:00:00 2001 From: Pete Y Date: Thu, 13 Jul 2023 16:41:17 +0100 Subject: [PATCH] Handling wrong authorities --- src/lib/common/ErrorMessage.svelte | 8 ++++++++ src/lib/common/data_getter.ts | 18 ++++++++++++++++++ src/pages/App.svelte | 11 ++++++++++- src/pages/ChooseArea.svelte | 12 ++++++++---- 4 files changed, 44 insertions(+), 5 deletions(-) create mode 100644 src/lib/common/ErrorMessage.svelte create mode 100644 src/lib/common/data_getter.ts diff --git a/src/lib/common/ErrorMessage.svelte b/src/lib/common/ErrorMessage.svelte new file mode 100644 index 000000000..fd44d6d54 --- /dev/null +++ b/src/lib/common/ErrorMessage.svelte @@ -0,0 +1,8 @@ + + +

+ Error: + {errorMessage} +

diff --git a/src/lib/common/data_getter.ts b/src/lib/common/data_getter.ts new file mode 100644 index 000000000..c10187ef2 --- /dev/null +++ b/src/lib/common/data_getter.ts @@ -0,0 +1,18 @@ +import type { FeatureCollection } from "geojson"; +import authoritiesUrl from "../../../assets/authorities.geojson?url"; + +export async function getAuthoritiesData() { + const resp = await fetch(authoritiesUrl); + const body = await resp.text(); + const json: FeatureCollection = JSON.parse(body); + return Promise.resolve(json); +} + +export async function getAuthoritiesNameSet() { + const json: FeatureCollection = await getAuthoritiesData(); + const authoritySet: Set = new Set(); + for (let feature of json.features) { + authoritySet.add(feature.properties!.name); + } + return Promise.resolve(authoritySet); +} diff --git a/src/pages/App.svelte b/src/pages/App.svelte index b63cd6385..a39c45606 100644 --- a/src/pages/App.svelte +++ b/src/pages/App.svelte @@ -5,6 +5,7 @@ import authoritiesUrl from "../../assets/authorities.geojson?url"; import BaselayerSwitcher from "../lib/BaselayerSwitcher.svelte"; import BoundaryLayer from "../lib/BoundaryLayer.svelte"; + import { getAuthoritiesNameSet } from "../lib/common/data_getter"; import Layout from "../lib/common/Layout.svelte"; import HoverLayer from "../lib/draw/HoverLayer.svelte"; import InterventionLayer from "../lib/draw/InterventionLayer.svelte"; @@ -26,10 +27,10 @@ let showInstructions = false; const params = new URLSearchParams(window.location.search); - // TODO Add validation and some kind of error page let authorityName: string = params.get("authority")!; let style: string = params.get("style") || "streets"; let schema: Schema = (params.get("schema") as Schema) || "v1"; + checkAuthorityValid(authorityName); // The version numbers here are arbitrary, not necessarily related to the // app's version. The version of the code deployed has to match the data, and @@ -82,6 +83,14 @@ ); return geojson; } + + async function checkAuthorityValid(authorityName: string): Promise { + let authortiesNameSet = await getAuthoritiesNameSet(); + + if (!authortiesNameSet.has(authorityName)) { + window.location.href = `/?error=Authority name not found: ${authorityName}`; + } + } diff --git a/src/pages/ChooseArea.svelte b/src/pages/ChooseArea.svelte index a9faf22da..8b842f6b6 100644 --- a/src/pages/ChooseArea.svelte +++ b/src/pages/ChooseArea.svelte @@ -4,18 +4,21 @@ import { initAll } from "govuk-frontend"; import { Map } from "maplibre-gl"; import { onMount } from "svelte"; + import ErrorMessage from "../lib/common/ErrorMessage.svelte"; import DefaultButton from "../lib/govuk/DefaultButton.svelte"; import FormElement from "../lib/govuk/FormElement.svelte"; import Radio from "../lib/govuk/Radio.svelte"; import SecondaryButton from "../lib/govuk/SecondaryButton.svelte"; import "maplibre-gl/dist/maplibre-gl.css"; - import authoritiesUrl from "../../assets/authorities.geojson?url"; + import { getAuthoritiesData } from "../lib/common/data_getter"; import FileInput from "../lib/common/FileInput.svelte"; import About from "../lib/sidebar/About.svelte"; import { bbox } from "../maplibre_helpers"; import type { Schema } from "../types"; let showAbout = false; + const params = new URLSearchParams(window.location.search); + let errorMessage: string = params.get("error")!; let inputValue: string; let dataList: HTMLDataListElement; @@ -36,9 +39,7 @@ // For govuk components. Must happen here. initAll(); - const resp = await fetch(authoritiesUrl); - const body = await resp.text(); - const json: FeatureCollection = JSON.parse(body); + const json: FeatureCollection = await getAuthoritiesData(); for (let feature of json.features) { let option = document.createElement("option"); option.value = feature.properties!.name; @@ -157,6 +158,9 @@ (showAbout = !showAbout)} >About + {#if errorMessage} + + {/if}