Only update trains table when needed

This commit is contained in:
Emmy D'Anello 2024-01-29 22:17:18 +01:00
parent 90036a0d3f
commit 0c7d039106
Signed by: ynerant
GPG Key ID: 3A75C55819C8CF85
1 changed files with 11 additions and 2 deletions

View File

@ -53,11 +53,20 @@ function TrainsTableBody({stop, date, time, tableType}) {
useEffect(() => { useEffect(() => {
if (stop.id !== undefined) { if (stop.id !== undefined) {
fetch(`http://localhost:8000/api/station/next_${tableType}/?stop_id=${stop.id}&date=${date}&time=${time}&format=json`) let validTrains = trains.filter(train => {
if (tableType === "departures")
return `${train.departure_date}T${train.departure_time_24h}` >= `${date}T${time}`
else
return `${train.arrival_date}T${train.arrival_time_24h}` >= `${date}T${time}`
})
if (trains.length > 0 && validTrains.length === trains.length)
return
console.log(`${trains.length - validTrains.length} trains deleted`)
fetch(`http://localhost:8000/api/station/next_${tableType}/?stop_id=${stop.id}&date=${date}&time=${time}&offset=${validTrains.length}&limit=${20 - validTrains.length}`)
.then(response => response.json()) .then(response => response.json())
.then(data => data.results) .then(data => data.results)
.then(data => { .then(data => {
setTrains(data) setTrains(trains => [...validTrains, ...data])
}) })
} }
}, [stop, tableType, date, time]) }, [stop, tableType, date, time])