Display trains that are near a station

This commit is contained in:
2024-05-11 20:52:22 +02:00
parent 735191947d
commit 070849c427
12 changed files with 175 additions and 165 deletions

View File

@ -26,12 +26,12 @@ const StyledTableRow = styled(TableRow)(({ theme, tabletype }) => ({
},
}));
function TrainsTable({stop, date, time, tableType}) {
function TrainsTable({station, date, time, tableType}) {
return <>
<TableContainer>
<Table>
<TrainsTableHeader tableType={tableType} />
<TrainsTableBody stop={stop} date={date} time={time} tableType={tableType} />
<TrainsTableBody station={station} date={date} time={time} tableType={tableType} />
</Table>
</TableContainer>
</>
@ -49,7 +49,7 @@ function TrainsTableHeader({tableType}) {
</>
}
function TrainsTableBody({stop, date, time, tableType}) {
function TrainsTableBody({station, date, time, tableType}) {
const filterTime = useCallback((train) => {
if (tableType === "departures")
return `${train.departure_date}T${train.departure_time_24h}` >= `${date}T${time}`
@ -58,16 +58,16 @@ function TrainsTableBody({stop, date, time, tableType}) {
}, [date, time, tableType])
const updateTrains = useCallback(() => {
return fetch(`/api/station/next_${tableType}/?stop_id=${stop.id}&date=${date}&time=${time}&offset=${0}&limit=${20}`)
return fetch(`/api/station/next_${tableType}/?station_slug=${station.slug}&date=${date}&time=${time}&offset=${0}&limit=${20}`)
.then(response => response.json())
.then(data => data.results)
.then(data => [...data])
}, [stop.id, date, time, tableType])
}, [station.id, date, time, tableType])
const trainsQuery = useQuery({
queryKey: ['trains', stop.id, tableType],
queryKey: ['trains', station.id, tableType],
queryFn: updateTrains,
enabled: !!stop.id,
enabled: !!station.id,
})
const trains = useMemo(() => trainsQuery.data ?? [], [trainsQuery.data])
@ -114,7 +114,7 @@ function TrainRow({train, tableType, date, time}) {
const stopTimesQuery = useQuery({
queryKey: ['stop_times', trip.id],
queryFn: () => fetch(`/api/gtfs/stop_time/?trip=${trip.id}&order=stop_sequence&limit=1000`)
queryFn: () => fetch(`/api/gtfs/stop_time/?${new URLSearchParams({trip: trip.id, order: 'stop_sequence', limit: 1000})}`)
.then(response => response.json())
.then(data => data.results),
enabled: !!trip.id,