-
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.
added the api for trip details, created the tripdetailpane with all i…
…nformation
- Loading branch information
1 parent
a86de9c
commit 50456e6
Showing
7 changed files
with
294 additions
and
7 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,109 @@ | ||
<script> | ||
import { onMount, onDestroy } from 'svelte'; | ||
import { faBus } from '@fortawesome/free-solid-svg-icons'; | ||
import { faSquare } from '@fortawesome/free-regular-svg-icons'; | ||
import { FontAwesomeIcon } from '@fortawesome/svelte-fontawesome'; | ||
export let tripId; | ||
export let serviceDate = null; | ||
let tripDetails = null; | ||
let routeInfo = null; | ||
let stopInfo = {}; | ||
let error = null; | ||
let interval; | ||
let currentStopIndex = -1; | ||
function formatTime(seconds) { | ||
const date = new Date(seconds); | ||
return date.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' }); | ||
} | ||
async function loadTripDetails() { | ||
try { | ||
let url = `/api/oba/trip-details/${tripId}?includeTrip=true&includeSchedule=true&includeStatus=true`; | ||
if (serviceDate) { | ||
url += `&serviceDate=${serviceDate}`; | ||
} | ||
const response = await fetch(url); | ||
if (response.ok) { | ||
const data = await response.json(); | ||
tripDetails = data.data.entry; | ||
if (data.data.references && data.data.references.routes) { | ||
routeInfo = data.data.references.routes.find((route) => route.id === tripDetails.routeId); | ||
} | ||
if (data.data.references && data.data.references.stops) { | ||
stopInfo = data.data.references.stops.reduce((acc, stop) => { | ||
acc[stop.id] = stop; | ||
return acc; | ||
}, {}); | ||
} | ||
// Update current stop index | ||
if (tripDetails.status && tripDetails.status.closestStop) { | ||
currentStopIndex = tripDetails.schedule.stopTimes.findIndex( | ||
(stop) => stop.stopId === tripDetails.status.closestStop.stopId | ||
); | ||
} | ||
console.log('Trip details:', tripDetails); | ||
console.log('Route info:', routeInfo); | ||
console.log('Stop info:', stopInfo); | ||
console.log('Current stop index:', currentStopIndex); | ||
} else { | ||
error = 'Unable to fetch trip details'; | ||
} | ||
} catch (err) { | ||
console.error('Error fetching trip details:', err); | ||
error = 'Error fetching trip details'; | ||
} | ||
} | ||
onMount(() => { | ||
loadTripDetails(); | ||
interval = setInterval(loadTripDetails, 30000); | ||
}); | ||
onDestroy(() => { | ||
clearInterval(interval); | ||
}); | ||
</script> | ||
|
||
<div class="trip-details-pane"> | ||
{#if error} | ||
<p>{error}</p> | ||
{:else if tripDetails} | ||
<h2> | ||
{#if routeInfo} | ||
Route {routeInfo.shortName} - | ||
{/if} | ||
</h2> | ||
{#if tripDetails.schedule && tripDetails.schedule.stopTimes && tripDetails.schedule.stopTimes.length > 0} | ||
<div class="relative"> | ||
<div class="absolute bottom-0 left-5 top-0 w-0.5 bg-[#129900]"></div> | ||
{#each tripDetails.schedule.stopTimes as stop, index} | ||
<div class="relative mb-4 flex items-center"> | ||
<div class="relative z-10 flex h-12 w-12 items-center justify-center"> | ||
<FontAwesomeIcon icon={faSquare} class="text-2xl text-[#129900]" /> | ||
{#if index === currentStopIndex} | ||
<FontAwesomeIcon icon={faBus} class="absolute text-lg text-[#129900]" /> | ||
{/if} | ||
</div> | ||
<div class="ml-4 flex w-full items-center justify-between"> | ||
<div class="text-md font-semibold text-[#000000] dark:text-white"> | ||
{stopInfo[stop.stopId] ? stopInfo[stop.stopId].name : stop.stopId} | ||
</div> | ||
<div class="text-sm text-[#86858B]">{formatTime(stop.arrivalTime)}</div> | ||
</div> | ||
</div> | ||
{/each} | ||
</div> | ||
{:else} | ||
<p>No stop times available for this trip.</p> | ||
{/if} | ||
{:else} | ||
<p>Loading trip details...</p> | ||
{/if} | ||
</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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
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 async function GET({ params, url }) { | ||
const { tripId } = params; | ||
const vehicleId = url.searchParams.get('vehicleId'); | ||
const serviceDate = url.searchParams.get('serviceDate'); | ||
|
||
let apiURL = `${baseURL}/api/where/trip-details/${tripId}.json?key=${apiKey}`; | ||
|
||
if (vehicleId) apiURL += `&vehicleId=${vehicleId}`; | ||
if (serviceDate) apiURL += `&serviceDate=${serviceDate}`; | ||
|
||
const response = await fetch(apiURL); | ||
|
||
if (!response.ok) { | ||
error(500, 'Unable to fetch trip-details.'); | ||
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,44 @@ | ||
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 async function GET({ params, url }) { | ||
const { tripId } = params; | ||
|
||
const serviceDate = url.searchParams.get('serviceDate'); | ||
const includeTrip = url.searchParams.get('includeTrip') || 'true'; | ||
const includeSchedule = url.searchParams.get('includeSchedule') || 'true'; | ||
const includeStatus = url.searchParams.get('includeStatus') || 'true'; | ||
const time = url.searchParams.get('time'); | ||
|
||
let apiURL = `${baseURL}/api/where/trip-details/${tripId}.json?key=${apiKey}`; | ||
|
||
if (serviceDate) apiURL += `&serviceDate=${serviceDate}`; | ||
apiURL += `&includeTrip=${includeTrip}`; | ||
apiURL += `&includeSchedule=${includeSchedule}`; | ||
apiURL += `&includeStatus=${includeStatus}`; | ||
if (time) apiURL += `&time=${time}`; | ||
|
||
try { | ||
const response = await fetch(apiURL); | ||
|
||
if (!response.ok) { | ||
throw new Error(`HTTP error! status: ${response.status}`); | ||
} | ||
|
||
const data = await response.json(); | ||
|
||
// Ensure stops are included in the references | ||
if (!data.data.references || !data.data.references.stops) { | ||
// If stops are not included, we might need to fetch them separately | ||
// This is a placeholder for that logic | ||
data.data.references = data.data.references || {}; | ||
data.data.references.stops = []; // Fetch stops here if needed | ||
} | ||
|
||
return json(data); | ||
} catch (err) { | ||
console.error('Error fetching trip details:', err); | ||
throw error(500, 'Unable to fetch trip details.'); | ||
} | ||
} |