From 2230058826308b529f78a20acbb0c6080cdbfae1 Mon Sep 17 00:00:00 2001 From: Emmy D'Anello Date: Sun, 28 Jan 2024 20:48:44 +0100 Subject: [PATCH] Restore time display --- sncf-station/src/Station.js | 36 ++++++++++++++++++++++++++++++--- sncf-station/src/TrainsTable.js | 18 +++++++++++------ sncf/api/views.py | 12 +++++++---- 3 files changed, 53 insertions(+), 13 deletions(-) diff --git a/sncf-station/src/Station.js b/sncf-station/src/Station.js index bd7d9c8..6628226 100644 --- a/sncf-station/src/Station.js +++ b/sncf-station/src/Station.js @@ -1,11 +1,40 @@ import './sncf.scss' -import {useParams} from "react-router-dom" +import {useParams, useSearchParams} from "react-router-dom" import TrainsTable from "./TrainsTable" import {useEffect, useState} from "react"; +function DateTimeSelector({date, time}) { + return <> +
+
+
+ Modifier la date et l'heure de recherche +
+ +
+ +
+ +
+ +
+
+ +
+
+
+ +} + function Station() { let {stopId} = useParams() let [stop, setStop] = useState({'name': "Chargement…"}) + let [searchParams, setSearchParams] = useSearchParams() + const now = new Date() + let dateNow = now.toISOString().split('T')[0] + let timeNow = now.toTimeString().split(' ')[0].substring(0, 5) + let [date, setDate] = useState(searchParams.get('date') || dateNow) + let [time, setTime] = useState(searchParams.get('time') || timeNow) useEffect(() => { fetch(`http://localhost:8000/api/gtfs/stop/${stopId}/`) @@ -22,8 +51,9 @@ function Station() {
- - + + +
) diff --git a/sncf-station/src/TrainsTable.js b/sncf-station/src/TrainsTable.js index 374aeb2..17c6ab5 100644 --- a/sncf-station/src/TrainsTable.js +++ b/sncf-station/src/TrainsTable.js @@ -1,12 +1,12 @@ import {useEffect, useState} from "react"; -function TrainsTable({stop, tableType}) { +function TrainsTable({stop, date, time, tableType}) { let tableClass = tableType === "departures" ? "table-departures" : "table-arrivals" tableClass = `table table-striped ${tableClass}` return <> - +
} @@ -21,19 +21,19 @@ function TrainsTableHeader() { } -function TrainsTableBody({stop, tableType}) { +function TrainsTableBody({stop, date, time, tableType}) { const [trains, setTrains] = useState([]) useEffect(() => { if (stop.id !== undefined) { - fetch(`http://localhost:8000/api/station/next_${tableType}/?stop_id=${stop.id}&format=json`) + fetch(`http://localhost:8000/api/station/next_${tableType}/?stop_id=${stop.id}&date=${date}&time=${time}&format=json`) .then(response => response.json()) .then(data => data.results) .then(data => { setTrains(data) }) } - }, [stop, tableType]) + }, [stop, tableType, date, time]) let table_rows = trains.map((train) => ) @@ -127,7 +127,7 @@ function TrainRow({train, tableType}) {
- {train.departure_time} + {getDisplayTime(train, tableType)}
@@ -182,4 +182,10 @@ function getTextColor(train, route) { } } +function getDisplayTime(train, tableType) { + let time = tableType === "departures" ? train.departure_time : train.arrival_time + let day_split = time.split(' ') + return day_split[day_split.length - 1].substring(0, 5) +} + export default TrainsTable; \ No newline at end of file diff --git a/sncf/api/views.py b/sncf/api/views.py index a395992..9306265 100644 --- a/sncf/api/views.py +++ b/sncf/api/views.py @@ -45,8 +45,8 @@ class StopTimeViewSet(viewsets.ReadOnlyModelViewSet): serializer_class = StopTimeSerializer filter_backends = [DjangoFilterBackend, OrderingFilter] filterset_fields = '__all__' - ordering_fields = ['arrival_time', 'departure_time', 'stop_sequence',] - ordering = ['stop_sequence',] + ordering_fields = ['arrival_time', 'departure_time', 'stop_sequence', ] + ordering = ['stop_sequence', ] class CalendarViewSet(viewsets.ReadOnlyModelViewSet): @@ -88,7 +88,9 @@ class NextDeparturesViewSet(viewsets.ReadOnlyModelViewSet): stop_name = self.request.query_params.get('stop_name', None) query_date = date.fromisoformat(self.request.query_params.get('date', now.date().isoformat())) query_time = self.request.query_params.get('time', now.time().isoformat(timespec='seconds')) - query_time = timedelta(seconds=int(query_time[:2]) * 3600 + int(query_time[3:5]) * 60 + int(query_time[6:])) + query_time = timedelta(seconds=int(query_time[:2]) * 3600 + + int(query_time[3:5]) * 60 + + (int(query_time[6:]) if len(query_time) > 6 else 0)) yesterday = query_date - timedelta(days=1) time_yesterday = query_time + timedelta(days=1) @@ -144,7 +146,9 @@ class NextArrivalsViewSet(viewsets.ReadOnlyModelViewSet): stop_name = self.request.query_params.get('stop_name', None) query_date = date.fromisoformat(self.request.query_params.get('date', now.date().isoformat())) query_time = self.request.query_params.get('time', now.time().isoformat(timespec='seconds')) - query_time = timedelta(seconds=int(query_time[:2]) * 3600 + int(query_time[3:5]) * 60 + int(query_time[6:])) + query_time = timedelta(seconds=int(query_time[:2]) * 3600 + + int(query_time[3:5]) * 60 + + (int(query_time[6:]) if len(query_time) > 6 else 0)) yesterday = query_date - timedelta(days=1) time_yesterday = query_time + timedelta(days=1)