From f0964d8fb79fae6b9ac5780460a32ec9bb4c35a2 Mon Sep 17 00:00:00 2001 From: Emmy D'Anello Date: Mon, 11 Nov 2024 22:28:05 +0100 Subject: [PATCH] Better datetime handle --- src/Station.js | 7 +++++-- src/TrainsTable.js | 4 ++-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/Station.js b/src/Station.js index 0098194..65c4c6b 100644 --- a/src/Station.js +++ b/src/Station.js @@ -26,7 +26,6 @@ function DateTimeSelector({datetime, setDatetime}) { Modifier la date et l'heure de recherche : - } @@ -36,7 +35,11 @@ function Station() { let {theme, stationId} = useParams() // eslint-disable-next-line no-unused-vars let [searchParams, setSearchParams] = useSearchParams() - const [datetime, setDatetime] = useState(dayjs()) + const [datetime, setDatetime] = useState(dayjs(searchParams.get('time') || undefined)) + if (datetime.format() !== searchParams.get('time')) { + searchParams.set('time', datetime.format()) + window.history.replaceState({}, '', '?' + searchParams.toString()) + } useQueryClient() const stationQuery = useQuery({ diff --git a/src/TrainsTable.js b/src/TrainsTable.js index 463e02e..c4efcae 100644 --- a/src/TrainsTable.js +++ b/src/TrainsTable.js @@ -69,10 +69,10 @@ function TrainsTableBody({station, datetime, tableType}) { .then(response => response.json()) .then(data => data.stopTimes) .then(data => [...data]) - }, [station.stopId, datetime, tableType]) + }, [station.stopId, tableType, datetime]) const trainsQuery = useQuery({ - queryKey: ['trains', station.stopId, tableType], + queryKey: ['trains', station.stopId, tableType, datetime], queryFn: updateTrains, enabled: !!station.stopId, })