Skip to content

Commit

Permalink
Handling wrong authorities
Browse files Browse the repository at this point in the history
  • Loading branch information
Pete-Y-CS committed Jul 13, 2023
1 parent 3d14558 commit 705a95b
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 5 deletions.
8 changes: 8 additions & 0 deletions src/lib/common/ErrorMessage.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<script lang="ts">
export let errorMessage: string;
</script>

<p class="govuk-error-message">
<span class="govuk-visually-hidden">Error:</span>
{errorMessage}
</p>
18 changes: 18 additions & 0 deletions src/lib/common/data_getter.ts
Original file line number Diff line number Diff line change
@@ -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<string> = new Set();
for (let feature of json.features) {
authoritySet.add(feature.properties!.name);
}
return Promise.resolve(authoritySet);
}
11 changes: 10 additions & 1 deletion src/pages/App.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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
Expand Down Expand Up @@ -82,6 +83,14 @@
);
return geojson;
}
async function checkAuthorityValid(authorityName: string): Promise<void> {
let authortiesNameSet = await getAuthoritiesNameSet();
if (!authortiesNameSet.has(authorityName)) {
window.location.href = `/?error=Authority name not found: ${authorityName}`;
}
}
</script>

<Layout>
Expand Down
12 changes: 8 additions & 4 deletions src/pages/ChooseArea.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -157,6 +158,9 @@
<SecondaryButton on:click={() => (showAbout = !showAbout)}
>About</SecondaryButton
>
{#if errorMessage}
<ErrorMessage bind:errorMessage />
{/if}

<FormElement
label="Select Transport Authority or Local Authority District"
Expand Down

0 comments on commit 705a95b

Please sign in to comment.