-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
1 parent
1dfdc5a
commit 919568e
Showing
8 changed files
with
87 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} |
18 changes: 2 additions & 16 deletions
18
src/routes/api/oba/arrivals-and-departures-for-stop/[id]/+server.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,10 @@ | ||
<script context="module"> | ||
import StopPane from '../../../components/oba/StopPane.svelte'; | ||
export async function load({ params }) { | ||
const stopID = params.stopID; | ||
const response = await fetch(`/api/oba/stops/${stopID}`); | ||
if (!response.ok) { | ||
return { | ||
status: response.status, | ||
error: new Error('Failed to fetch stop data') | ||
}; | ||
} | ||
const stopData = await response.json(); | ||
return { | ||
props: { | ||
stopData | ||
} | ||
}; | ||
} | ||
</script> | ||
|
||
<script> | ||
export let stopData; | ||
import StopPane from '$components/oba/StopPane.svelte'; | ||
export let data; | ||
const stop = data.stopData.entry; | ||
const arrivalsAndDeparturesResponse = data.arrivalsAndDeparturesResponse; | ||
</script> | ||
|
||
<StopPane {stopData} /> | ||
<div class='pt-20 px-8 max-w-5xl mx-auto'> | ||
<StopPane {stop} {arrivalsAndDeparturesResponse} /> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters