Skip to content

Commit bf4285c

Browse files
committed
Add error handling for ID param
1 parent 2a71ec0 commit bf4285c

File tree

1 file changed

+31
-17
lines changed

1 file changed

+31
-17
lines changed

site/assets/js/map.js

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@ const BASE_URL = "https://data.opentimes.org/times/version=0.0.1/mode=auto/year=
55
const BIG_STATES = ["06", "36"];
66
const ZOOM_THRESHOLDS = [6, 8];
77

8-
const protocol = new pmtiles.Protocol();
9-
maplibregl.addProtocol("pmtiles", protocol.tile);
10-
118
class ColorScale {
129
constructor(zoomLower, zoomUpper) {
1310
this.scaleContainer = this.createScaleContainer();
@@ -143,6 +140,9 @@ class Map {
143140
}
144141

145142
async init() {
143+
const protocol = new pmtiles.Protocol();
144+
maplibregl.addProtocol("pmtiles", protocol.tile);
145+
146146
this.map = new maplibregl.Map({
147147
style: "https://tiles.openfreemap.org/styles/positron",
148148
center: [-74.0, 40.75],
@@ -396,6 +396,9 @@ class ParquetProcessor {
396396

397397
async updateMapOnQuery(map, urls, id) {
398398
const results = [];
399+
if (!this.validIdInput(id)) {
400+
return results;
401+
}
399402

400403
const dataPromises = urls.map(async (url) => {
401404
const metadata = await this.fetchAndCacheMetadata(url);
@@ -438,6 +441,15 @@ class ParquetProcessor {
438441
await Promise.all(dataPromises);
439442
return results;
440443
}
444+
445+
validIdInput(id) {
446+
if (id && /^\d{11}$/.test(id)) {
447+
return true;
448+
} else {
449+
console.warn("Invalid ID input. Please enter a valid 11-digit tract ID.")
450+
return false;
451+
}
452+
}
441453
}
442454

443455
function debounce(func, wait) {
@@ -449,26 +461,28 @@ function debounce(func, wait) {
449461
}
450462

451463
(async () => {
452-
let idParam = new URLSearchParams(window.location.search).get("id");
453464
const spinner = new Spinner();
454-
const colorScale = new ColorScale(ZOOM_THRESHOLDS[0], ZOOM_THRESHOLDS[1]);
455-
const processor = new ParquetProcessor(BASE_URL);
456-
457465
spinner.show();
458466

467+
const colorScale = new ColorScale(ZOOM_THRESHOLDS[0], ZOOM_THRESHOLDS[1]);
468+
const processor = new ParquetProcessor(BASE_URL);
459469
const map = new Map(colorScale, spinner, processor);
460470
colorScale.draw(map.map);
461471

462-
// Load the previous map click if there was one
463-
if (idParam) {
464-
await processor.runQuery(map, idParam.substring(0, 2), idParam);
465-
}
472+
// Wait for the map to fully load before running a query
473+
map.map.on("load", async () => {
474+
// Load the previous map click if there was one
475+
const idParam = new URLSearchParams(window.location.search).get("id");
476+
if (idParam) {
477+
await processor.runQuery(map, idParam.substring(0, 2), idParam);
478+
}
466479

467-
// Remove the hash if map is at starting location
468-
if (window.location.hash === "#10/40.75/-74") {
469-
console.log(window.location.hash);
470-
window.history.replaceState({}, "", window.location.pathname + window.location.search);
471-
}
480+
// Remove the hash if map is at starting location
481+
if (window.location.hash === "#10/40.75/-74") {
482+
console.log(window.location.hash);
483+
window.history.replaceState({}, "", window.location.pathname + window.location.search);
484+
}
472485

473-
spinner.hide();
486+
spinner.hide();
487+
});
474488
})();

0 commit comments

Comments
 (0)