From 1dfdc5ac832199970b0eef66bc89cdde53140451 Mon Sep 17 00:00:00 2001 From: tarunsinghofficial Date: Thu, 18 Jul 2024 20:26:48 +0530 Subject: [PATCH 1/2] added the feature to change the url onclick, onclose is implemented --- src/components/map/GoogleMap.svelte | 6 +++++ src/components/navigation/ModalPane.svelte | 2 ++ src/routes/api/oba/stop/[stopID]/+server.js | 18 +++++++++++++ src/routes/stops/[stopID]/+page.svelte | 28 +++++++++++++++++++++ 4 files changed, 54 insertions(+) create mode 100644 src/routes/api/oba/stop/[stopID]/+server.js create mode 100644 src/routes/stops/[stopID]/+page.svelte diff --git a/src/components/map/GoogleMap.svelte b/src/components/map/GoogleMap.svelte index 7fe5ae0..2aeb3e6 100644 --- a/src/components/map/GoogleMap.svelte +++ b/src/components/map/GoogleMap.svelte @@ -7,6 +7,7 @@ PUBLIC_OBA_REGION_CENTER_LAT as initialLat, PUBLIC_OBA_REGION_CENTER_LNG as initialLng } from '$env/static/public'; + import { pushState } from '$app/navigation'; import { createMap, loadGoogleMapsLibrary, nightModeStyles } from '$lib/googleMaps'; import LocationButton from '$lib/LocationButton/LocationButton.svelte'; @@ -68,6 +69,7 @@ const container = document.createElement('div'); document.body.appendChild(container); function handleClick() { + pushState(`/stops/${s.id}`); dispatch('stopSelected', { stop: s }); } @@ -156,6 +158,10 @@ } }); }); + + function handleStopSelected(stop) { + dispatch('stopSelected', { stop }); + }
diff --git a/src/components/navigation/ModalPane.svelte b/src/components/navigation/ModalPane.svelte index f3b829e..e81a823 100644 --- a/src/components/navigation/ModalPane.svelte +++ b/src/components/navigation/ModalPane.svelte @@ -4,10 +4,12 @@ import { faX } from '@fortawesome/free-solid-svg-icons'; import { keybinding } from '$lib/keybinding'; import { createEventDispatcher } from 'svelte'; + import { pushState } from '$app/navigation'; const dispatch = createEventDispatcher(); function closePane() { + pushState('/'); dispatch('close'); } diff --git a/src/routes/api/oba/stop/[stopID]/+server.js b/src/routes/api/oba/stop/[stopID]/+server.js new file mode 100644 index 0000000..8a66c36 --- /dev/null +++ b/src/routes/api/oba/stop/[stopID]/+server.js @@ -0,0 +1,18 @@ +import { error, json } from '@sveltejs/kit'; + +import { PUBLIC_OBA_SERVER_URL as baseURL } from '$env/static/public'; +import { PRIVATE_OBA_API_KEY as apiKey } from '$env/static/private'; + +/** @type {import('./$types').RequestHandler} */ +export async function GET({ params }) { + const stopID = params.stopID; + const apiURL = `${baseURL}/api/where/stop/${stopID}.json?key=${apiKey}`; + const response = await fetch(apiURL); + + if (!response.ok) { + return error(500, 'Unable to fetch arrivals-and-departures-for-stop.'); + } + + const data = await response.json(); + return json(data); +} diff --git a/src/routes/stops/[stopID]/+page.svelte b/src/routes/stops/[stopID]/+page.svelte new file mode 100644 index 0000000..c2fa6fa --- /dev/null +++ b/src/routes/stops/[stopID]/+page.svelte @@ -0,0 +1,28 @@ + + + + + From 919568e9313ebb862e43b7faa57bac97378c7163 Mon Sep 17 00:00:00 2001 From: Aaron Brethorst Date: Fri, 19 Jul 2024 16:14:41 -0700 Subject: [PATCH 2/2] Get the Stop Pane to work on a server side rendered page * Extract the logic of the REST API proxy endpoints into JS functions * Server-side render the contents of the stop details page (i.e. click a stop on the map and then refresh the web page) * Only load data from the server in StopPane when it's missing needed data. --- src/components/oba/StopPane.svelte | 38 +++++++++++++------ .../RestAPI/arrivalsAndDeparturesForStop.js | 17 +++++++++ src/lib/RestAPI/stop.js | 16 ++++++++ .../[id]/+server.js | 18 +-------- src/routes/api/oba/stop/[stopID]/+server.js | 16 +------- src/routes/stops/[stopID]/+page.server.js | 16 ++++++++ src/routes/stops/[stopID]/+page.svelte | 32 ++++------------ svelte.config.js | 1 + 8 files changed, 87 insertions(+), 67 deletions(-) create mode 100644 src/lib/RestAPI/arrivalsAndDeparturesForStop.js create mode 100644 src/lib/RestAPI/stop.js create mode 100644 src/routes/stops/[stopID]/+page.server.js diff --git a/src/components/oba/StopPane.svelte b/src/components/oba/StopPane.svelte index 6b5fff7..ca1b373 100644 --- a/src/components/oba/StopPane.svelte +++ b/src/components/oba/StopPane.svelte @@ -2,21 +2,17 @@ import ArrivalDeparture from '../ArrivalDeparture.svelte'; export let stop; + export let arrivalsAndDeparturesResponse = null; let arrivalsAndDepartures; let loading = false; let error; - let routeShortNames = null; async function loadData(stopID) { loading = true; const response = await fetch(`/api/oba/arrivals-and-departures-for-stop/${stopID}`); if (response.ok) { - const json = await response.json(); - arrivalsAndDepartures = json.data.entry; - routeShortNames = json.data.references.routes - .filter((r) => stop.routeIds.includes(r.id)) - .map((r) => r.nullSafeShortName) - .sort(); + arrivalsAndDeparturesResponse = await response.json(); + arrivalsAndDepartures = arrivalsAndDeparturesResponse.data.entry; } else { error = 'Unable to fetch arrival/departure data'; } @@ -24,9 +20,27 @@ loading = false; } - $: (async (s) => { - await loadData(s.id); - })(stop); + $: (async (s, arrDep) => { + // if the arrivalsAndDeparturesResponse is passed in, use that + // instead of loading fresh data. + if (arrDep) { + arrivalsAndDepartures = arrDep.data.entry; + } + else { + await loadData(s.id); + } + })(stop, arrivalsAndDeparturesResponse); + + let _routeShortNames = null; + function routeShortNames() { + if (!_routeShortNames) { + _routeShortNames = arrivalsAndDeparturesResponse.data.references.routes + .filter((r) => stop.routeIds.includes(r.id)) + .map((r) => r.nullSafeShortName) + .sort(); + } + return _routeShortNames; + }
@@ -64,8 +78,8 @@

{stop.name}

Stop #{stop.id}

- {#if routeShortNames} -

Routes: {routeShortNames.join(', ')}

+ {#if routeShortNames()} +

Routes: {routeShortNames().join(', ')}

{/if}
diff --git a/src/lib/RestAPI/arrivalsAndDeparturesForStop.js b/src/lib/RestAPI/arrivalsAndDeparturesForStop.js new file mode 100644 index 0000000..082e385 --- /dev/null +++ b/src/lib/RestAPI/arrivalsAndDeparturesForStop.js @@ -0,0 +1,17 @@ +import { error, json } from '@sveltejs/kit'; + +import { PUBLIC_OBA_SERVER_URL as baseURL } from '$env/static/public'; +import { PRIVATE_OBA_API_KEY as apiKey } from '$env/static/private'; + +export default async function(stopID) { + const apiURL = `${baseURL}/api/where/arrivals-and-departures-for-stop/${stopID}.json?key=${apiKey}`; + const response = await fetch(apiURL); + + if (!response.ok) { + error(500, 'Unable to fetch arrivals-and-departures-for-stop.'); + return; + } + + const data = await response.json(); + return json(data); +} diff --git a/src/lib/RestAPI/stop.js b/src/lib/RestAPI/stop.js new file mode 100644 index 0000000..2fb518a --- /dev/null +++ b/src/lib/RestAPI/stop.js @@ -0,0 +1,16 @@ +import { error, json } from '@sveltejs/kit'; + +import { PUBLIC_OBA_SERVER_URL as baseURL } from '$env/static/public'; +import { PRIVATE_OBA_API_KEY as apiKey } from '$env/static/private'; + +export default async function(stopID) { + const apiURL = `${baseURL}/api/where/stop/${stopID}.json?key=${apiKey}`; + const response = await fetch(apiURL); + + if (!response.ok) { + return error(500, 'Unable to fetch arrivals-and-departures-for-stop.'); + } + + const data = await response.json(); + return json(data); +} diff --git a/src/routes/api/oba/arrivals-and-departures-for-stop/[id]/+server.js b/src/routes/api/oba/arrivals-and-departures-for-stop/[id]/+server.js index 11ed674..9ebfd51 100644 --- a/src/routes/api/oba/arrivals-and-departures-for-stop/[id]/+server.js +++ b/src/routes/api/oba/arrivals-and-departures-for-stop/[id]/+server.js @@ -1,20 +1,6 @@ -import { error, json } from '@sveltejs/kit'; - -import { PUBLIC_OBA_SERVER_URL as baseURL } from '$env/static/public'; - -import { PRIVATE_OBA_API_KEY as apiKey } from '$env/static/private'; +import arrivalDepartureAPI from '$lib/RestAPI/arrivalsAndDeparturesForStop'; /** @type {import('./$types').RequestHandler} */ export async function GET({ params }) { - const stopID = params.id; - const apiURL = `${baseURL}/api/where/arrivals-and-departures-for-stop/${stopID}.json?key=${apiKey}`; - const response = await fetch(apiURL); - - if (!response.ok) { - error(500, 'Unable to fetch arrivals-and-departures-for-stop.'); - return; - } - - const data = await response.json(); - return json(data); + return arrivalDepartureAPI(params.id); } diff --git a/src/routes/api/oba/stop/[stopID]/+server.js b/src/routes/api/oba/stop/[stopID]/+server.js index 8a66c36..aed93b1 100644 --- a/src/routes/api/oba/stop/[stopID]/+server.js +++ b/src/routes/api/oba/stop/[stopID]/+server.js @@ -1,18 +1,6 @@ -import { error, json } from '@sveltejs/kit'; - -import { PUBLIC_OBA_SERVER_URL as baseURL } from '$env/static/public'; -import { PRIVATE_OBA_API_KEY as apiKey } from '$env/static/private'; +import stopAPI from '$lib/RestAPI/stop'; /** @type {import('./$types').RequestHandler} */ export async function GET({ params }) { - const stopID = params.stopID; - const apiURL = `${baseURL}/api/where/stop/${stopID}.json?key=${apiKey}`; - const response = await fetch(apiURL); - - if (!response.ok) { - return error(500, 'Unable to fetch arrivals-and-departures-for-stop.'); - } - - const data = await response.json(); - return json(data); + return stopAPI(params.stopID); } diff --git a/src/routes/stops/[stopID]/+page.server.js b/src/routes/stops/[stopID]/+page.server.js new file mode 100644 index 0000000..c5f189b --- /dev/null +++ b/src/routes/stops/[stopID]/+page.server.js @@ -0,0 +1,16 @@ +import stopAPI from '$lib/RestAPI/stop.js'; +import arrivalDepartureAPI from '$lib/RestAPI/arrivalsAndDeparturesForStop.js'; + +export async function load({ params }) { + const stopID = params.stopID; + const stopResponse = await stopAPI(stopID); + const stopBody = await stopResponse.json(); + const arrivalsAndDeparturesResponse = await arrivalDepartureAPI(stopID) + const arrivalsAndDeparturesResponseJSON = await arrivalsAndDeparturesResponse.json(); + + return { + stopID: params.stopID, + stopData: stopBody.data, + arrivalsAndDeparturesResponse: arrivalsAndDeparturesResponseJSON + } +} diff --git a/src/routes/stops/[stopID]/+page.svelte b/src/routes/stops/[stopID]/+page.svelte index c2fa6fa..9c47e40 100644 --- a/src/routes/stops/[stopID]/+page.svelte +++ b/src/routes/stops/[stopID]/+page.svelte @@ -1,28 +1,10 @@ - - - +
+ +
diff --git a/svelte.config.js b/svelte.config.js index b5d2a9a..8d3bc0e 100644 --- a/svelte.config.js +++ b/svelte.config.js @@ -9,6 +9,7 @@ const config = { // See https://kit.svelte.dev/docs/adapters for more information about adapters. adapter: adapter(), alias: { + $components: './src/components', $images: './src/assets/images' } },