Skip to content

Commit fff71f3

Browse files
committed
Stop abusing the gjScheme store for the browse page. #359
1 parent 94c8643 commit fff71f3

File tree

5 files changed

+44
-33
lines changed

5 files changed

+44
-33
lines changed

src/lib/browse/Filters.svelte

+11-13
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
SecondaryButton,
88
Select,
99
} from "lib/govuk";
10-
import { gjScheme } from "stores";
1110
import { onMount } from "svelte";
1211
import type { FeatureUnion } from "types";
13-
import { type Scheme } from "./data";
12+
import type { AllSchemeGJ, Scheme } from "./data";
1413
import InterventionColorSelector from "./InterventionColorSelector.svelte";
1514
15+
export let schemesGj: AllSchemeGJ;
1616
// These are immutable; re-create this component if they change
1717
export let schemes: Map<string, Scheme>;
1818
@@ -83,7 +83,7 @@
8383
return true;
8484
};
8585
schemesToBeShown = new Set(
86-
$gjScheme.features
86+
schemesGj.features
8787
.filter(filterFeatures)
8888
.map((f) => f.properties.scheme_reference!)
8989
);
@@ -106,17 +106,15 @@
106106
}
107107
return true;
108108
};
109-
gjScheme.update((gj) => {
110-
for (let feature of gj.features) {
111-
if (showFeature(feature)) {
112-
delete feature.properties.hide_while_editing;
113-
counts[feature.properties.intervention_type]++;
114-
} else {
115-
feature.properties.hide_while_editing = true;
116-
}
109+
for (let feature of schemesGj.features) {
110+
if (showFeature(feature)) {
111+
delete feature.properties.hide_while_editing;
112+
counts[feature.properties.intervention_type]++;
113+
} else {
114+
feature.properties.hide_while_editing = true;
117115
}
118-
return gj;
119-
});
116+
}
117+
schemesGj = schemesGj;
120118
counts = counts;
121119
}
122120
$: filtersUpdated(filterText, filterAuthority, filterFundingProgramme);

src/lib/browse/InterventionLayer.svelte

+3-2
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,23 @@
1010
} from "lib/maplibre";
1111
import type { FilterSpecification } from "maplibre-gl";
1212
import { colorInterventionsBySchema } from "schemas";
13-
import { gjScheme } from "stores";
1413
import {
1514
CircleLayer,
1615
FillLayer,
1716
GeoJSON,
1817
hoverStateFilter,
1918
LineLayer,
2019
} from "svelte-maplibre";
20+
import type { AllSchemeGJ } from "./data";
2121
import InterventionPopup from "./InterventionPopup.svelte";
2222
23+
export let schemesGj: AllSchemeGJ;
2324
export let showSchemes: boolean;
2425
export let filterText: string;
2526
2627
let colorInterventions = colorInterventionsBySchema("v1");
2728
28-
$: gj = addLineStringEndpoints($gjScheme);
29+
$: gj = addLineStringEndpoints(schemesGj);
2930
3031
// TODO Abusing this property for filtering
3132
const hideWhileEditing: FilterSpecification = [

src/lib/browse/SchemeCard.svelte

+5-4
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
import { CollapsibleCard } from "lib/common";
44
import { SecondaryButton } from "lib/govuk";
55
import { bbox } from "lib/maplibre";
6-
import { gjScheme, map } from "stores";
7-
import type { Scheme } from "./data";
6+
import { map } from "stores";
7+
import type { AllSchemeGJ, Scheme } from "./data";
88
9+
export let schemesGj: AllSchemeGJ;
910
export let scheme: Scheme;
1011
export let authorityNames: Set<string>;
1112
@@ -18,7 +19,7 @@
1819
// TODO Highlight on the map? Or fade everything else?
1920
let gj: FeatureCollection = {
2021
type: "FeatureCollection",
21-
features: $gjScheme.features.filter(
22+
features: schemesGj.features.filter(
2223
(f) => f.properties.scheme_reference == scheme.scheme_reference
2324
),
2425
};
@@ -28,7 +29,7 @@
2829
function editScheme() {
2930
let gj = {
3031
type: "FeatureCollection",
31-
features: $gjScheme.features.filter(
32+
features: schemesGj.features.filter(
3233
(f) => f.properties.scheme_reference == scheme.scheme_reference
3334
),
3435
};

src/lib/browse/data.ts

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
import type { FeatureCollection } from "geojson";
22
import { setPrecision } from "lib/maplibre";
33
import readXlsxFile from "read-excel-file";
4+
import type { FeatureUnion } from "types";
5+
6+
export interface AllSchemeGJ {
7+
type: "FeatureCollection";
8+
features: FeatureUnion[];
9+
schemes: { [name: string]: SchemeData };
10+
}
411

512
// This must be filled out in the input file
613
interface SchemeData {
@@ -19,9 +26,7 @@ export interface Scheme extends SchemeData {
1926
// Modifies this GeoJSON in-place, and returns a dictionary of Schemes, keyed
2027
// (and ordered) by scheme_reference. Each feature (intervention) in the GJ
2128
// links back to one of these schemes by scheme_reference.
22-
export function processInput(
23-
gj: FeatureCollection & { schemes: { [name: string]: SchemeData } }
24-
): Map<string, Scheme> {
29+
export function processInput(gj: AllSchemeGJ): Map<string, Scheme> {
2530
let schemes = new Map();
2631

2732
// Assume the input has a top-level dictionary keyed by scheme_reference
@@ -42,6 +47,7 @@ export function processInput(
4247

4348
// TODO For easy styling, copy one field from scheme to all its features.
4449
// As we have more cases like this, revisit what's most performant.
50+
// @ts-ignore Extend InterventionProps with scheme_reference and this
4551
feature.properties!.funding_programme = scheme.funding_programme;
4652
// Force numeric IDs (skipping 0) for hovering to work
4753
feature.id = id++;

src/pages/BrowseSchemes.svelte

+16-11
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// @ts-ignore no declarations
33
import { initAll } from "govuk-frontend";
44
import AppVersion from "lib/browse/AppVersion.svelte";
5-
import { processInput, type Scheme } from "lib/browse/data";
5+
import { processInput, type AllSchemeGJ, type Scheme } from "lib/browse/data";
66
import Filters from "lib/browse/Filters.svelte";
77
import LayerControls from "lib/browse/LayerControls.svelte";
88
import LoadRemoteSchemeData from "lib/browse/LoadRemoteSchemeData.svelte";
@@ -19,31 +19,35 @@
1919
ZoomOutMap,
2020
} from "lib/common";
2121
import { ErrorMessage } from "lib/govuk";
22-
import { gjScheme, mapStyle } from "stores";
22+
import { mapStyle } from "stores";
2323
import { onMount } from "svelte";
2424
25-
// TODO Remove after the input data is fixed to plumb correct authority names.
26-
let authorityNames: Set<string> = new Set(authorityNamesList);
27-
2825
onMount(() => {
2926
// For govuk components. Must happen here.
3027
initAll();
3128
});
3229
30+
// TODO Remove after the input data is fixed to plumb correct authority names.
31+
let authorityNames: Set<string> = new Set(authorityNamesList);
32+
3333
const params = new URLSearchParams(window.location.search);
3434
mapStyle.set(params.get("style") || "dataviz");
3535
let errorMessage = "";
3636
37+
let schemesGj: AllSchemeGJ = {
38+
type: "FeatureCollection",
39+
features: [],
40+
schemes: {},
41+
};
3742
let schemes: Map<string, Scheme> = new Map();
3843
let schemesToBeShown: Set<string> = new Set();
3944
let filterText = "";
4045
let showSchemes = true;
4146
4247
function loadFile(text: string) {
4348
try {
44-
gjScheme.set(JSON.parse(text));
45-
// @ts-ignore TODO We're abusing the gjScheme store in the browse page for now
46-
schemes = processInput($gjScheme);
49+
schemesGj = JSON.parse(text);
50+
schemes = processInput(schemesGj);
4751
errorMessage = "";
4852
} catch (err) {
4953
errorMessage = `The file you loaded is broken: ${err}`;
@@ -55,7 +59,7 @@
5559
<div slot="sidebar" class="govuk-prose">
5660
<div style="display: flex; justify-content: space-between">
5761
<h1>Browse schemes</h1>
58-
<ZoomOutMap boundaryGeojson={$gjScheme} />
62+
<ZoomOutMap boundaryGeojson={schemesGj} />
5963
</div>
6064
<AppVersion />
6165
<LoggedIn />
@@ -67,6 +71,7 @@
6771

6872
{#if schemes.size > 0}
6973
<Filters
74+
bind:schemesGj
7075
{schemes}
7176
bind:schemesToBeShown
7277
bind:filterText
@@ -77,14 +82,14 @@
7782
<ul>
7883
{#each schemes.values() as scheme}
7984
{#if schemesToBeShown.has(scheme.scheme_reference)}
80-
<SchemeCard {scheme} {authorityNames} />
85+
<SchemeCard {schemesGj} {scheme} {authorityNames} />
8186
{/if}
8287
{/each}
8388
</ul>
8489
</div>
8590
<div slot="main">
8691
<MapLibreMap style={$mapStyle} startBounds={[-5.96, 49.89, 2.31, 55.94]}>
87-
<InterventionLayer {filterText} {showSchemes} />
92+
<InterventionLayer {schemesGj} {filterText} {showSchemes} />
8893
<div class="top-right">
8994
<LayerControls />
9095
</div>

0 commit comments

Comments
 (0)