Only update trains table when needed
This commit is contained in:
parent
90036a0d3f
commit
0c7d039106
|
@ -53,11 +53,20 @@ function TrainsTableBody({stop, date, time, tableType}) {
|
|||
|
||||
useEffect(() => {
|
||||
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(data => data.results)
|
||||
.then(data => {
|
||||
setTrains(data)
|
||||
setTrains(trains => [...validTrains, ...data])
|
||||
})
|
||||
}
|
||||
}, [stop, tableType, date, time])
|
||||
|
|
Loading…
Reference in New Issue