From 0c7d03910602d723f4ab54be07ea2804f9f2d815 Mon Sep 17 00:00:00 2001 From: Emmy D'Anello Date: Mon, 29 Jan 2024 22:17:18 +0100 Subject: [PATCH] Only update trains table when needed --- sncf-station/src/TrainsTable.js | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) 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])