From 2d758ee5fcd5f4c52ed76b3b60d7b314f1488f91 Mon Sep 17 00:00:00 2001 From: Zhi Keng He Date: Tue, 7 Nov 2023 23:45:15 -0500 Subject: [PATCH 1/2] adds a redirect for non-standard links --- src/App.svelte | 27 +++++++++++++++++++++++++++ src/assets/boundaries/index.ts | 12 +++++++++++- 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/src/App.svelte b/src/App.svelte index 66246f1..30ff76f 100644 --- a/src/App.svelte +++ b/src/App.svelte @@ -8,9 +8,27 @@ import Map from './components/Map.svelte'; import Sidebar from './components/Sidebar/Sidebar.svelte'; import Controls from './components/Controls.svelte'; + import { onMount } from 'svelte'; + import { layers } from './assets/boundaries/index' const params = new URLSearchParams(window.location.search); + let redirected = false + let redirect_url = '' + + onMount(async()=> { + //redirect if needed + if(params.get('redirect')){ + redirected = true + const boundaryId = params.get('map') ?? 'ss' + const district = params.get('dist') ?? '0' + // @ts-ignore + const url = await layers[boundaryId].redirectUrl(district) + redirect_url = url + //window.location.href = url + } + }) + $: { $selectedDistrict ? params.set('dist', $selectedDistrict) @@ -36,6 +54,14 @@ } +{#if redirected} +
+

You are being redirected...

+ {#if redirect_url} +

If you are stuck, try the following: {redirect_url}

+ {/if} +
+{:else}
+{/if} \ No newline at end of file diff --git a/src/assets/boundaries/index.ts b/src/assets/boundaries/index.ts index b5230ba..f8f0fbd 100644 --- a/src/assets/boundaries/index.ts +++ b/src/assets/boundaries/index.ts @@ -46,6 +46,9 @@ export interface ILayer { /** Url to link to for more info */ formatUrl?: (name: string) => string; + /** Used with the redirect url param, to run a function to find and go to a url */ + redirectUrl?: Function; + /** Formatted display name of district, e.g. transforms 101 to Manhattan - 1 */ formatContent: (name: any) => string; } @@ -169,7 +172,14 @@ export const layers: ILayers = { description_url: 'https://www.nysenate.gov/', sql: `SELECT * FROM all_bounds WHERE id = 'ss'`, icon: '⚖️', - formatUrl: name => `https://www.nysenate.gov/district/${name}`, + formatUrl: name => `/?redirect=true&map=ss&dist=${name}`, + redirectUrl: async (district: string) => { + // docs for api here: https://github.com/nysenate/GeoApi/blob/c92a0ef81ddf1f21b23751e26e1cb2a7021a3ec7/docs/index.rst + const requestURL = `https://4dvj5dcxge.execute-api.us-east-1.amazonaws.com/staging/https://pubgeo.nysenate.gov/api/v2/map/senate?showMembers=true&district=${district}` + const data = await fetch(requestURL).then(r => r.json()) + const memberUrl = data.member.url + return memberUrl + }, formatContent: name => format_default(name) }, nta: { From 21202452af4d5d332f8b00228fca1934b5f396f5 Mon Sep 17 00:00:00 2001 From: Zhi Keng He Date: Tue, 7 Nov 2023 23:45:58 -0500 Subject: [PATCH 2/2] uncomment window redirect --- src/App.svelte | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/App.svelte b/src/App.svelte index 30ff76f..0460ab1 100644 --- a/src/App.svelte +++ b/src/App.svelte @@ -25,7 +25,7 @@ // @ts-ignore const url = await layers[boundaryId].redirectUrl(district) redirect_url = url - //window.location.href = url + window.location.href = url } })