diff --git a/sncf-station/src/TrainsTable.js b/sncf-station/src/TrainsTable.js index 104b16b..e82b64f 100644 --- a/sncf-station/src/TrainsTable.js +++ b/sncf-station/src/TrainsTable.js @@ -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])