Compare commits

..

No commits in common. "6389406744cb51166b8b46443ab3f661acead5e8" and "f0964d8fb79fae6b9ac5780460a32ec9bb4c35a2" have entirely different histories.

2 changed files with 34 additions and 54 deletions

View File

@ -1,13 +1,14 @@
import {useNavigate, useParams, useSearchParams} from "react-router-dom" import {useNavigate, useParams, useSearchParams} from "react-router-dom"
import TrainsTable from "./TrainsTable" import TrainsTable from "./TrainsTable"
import {useEffect, useState} from "react" import TripsFilter from "./TripsFilter"
import {Box, Checkbox, FormLabel} from "@mui/material" import {useState} from "react"
import {Box, Button, FormLabel} from "@mui/material"
import {DateTimePicker} from "@mui/x-date-pickers" import {DateTimePicker} from "@mui/x-date-pickers"
import dayjs from "dayjs" import dayjs from "dayjs"
import {useQuery, useQueryClient} from "@tanstack/react-query" import {useQuery, useQueryClient} from "@tanstack/react-query"
import AutocompleteStation from "./AutocompleteStation" import AutocompleteStation from "./AutocompleteStation"
function DateTimeSelector({datetime, setDatetime, realtime, setRealtime}) { function DateTimeSelector({datetime, setDatetime}) {
const navigate = useNavigate() const navigate = useNavigate()
function onStationSelected(event, station) { function onStationSelected(event, station) {
@ -24,11 +25,7 @@ function DateTimeSelector({datetime, setDatetime, realtime, setRealtime}) {
<FormLabel> <FormLabel>
Modifier la date et l'heure de recherche : Modifier la date et l'heure de recherche :
</FormLabel> </FormLabel>
<DateTimePicker name="date" label="Date" onChange={setDatetime} value={datetime} disabled={realtime} readOnly={realtime} /> <DateTimePicker name="date" label="Date" onChange={setDatetime} value={datetime} />
<Checkbox onChange={event => setRealtime(event.target.checked)} checked={realtime} />
<FormLabel>
Temps réel
</FormLabel>
</Box> </Box>
</> </>
} }
@ -38,18 +35,9 @@ function Station() {
let {theme, stationId} = useParams() let {theme, stationId} = useParams()
// eslint-disable-next-line no-unused-vars // eslint-disable-next-line no-unused-vars
let [searchParams, setSearchParams] = useSearchParams() let [searchParams, setSearchParams] = useSearchParams()
const [realtime, setRealtime] = useState(searchParams.get('realtime') === "1" || false)
const [datetime, setDatetime] = useState(dayjs(searchParams.get('time') || undefined)) const [datetime, setDatetime] = useState(dayjs(searchParams.get('time') || undefined))
if ((searchParams.get('realtime') === null || searchParams.get('realtime') === "0") if (datetime.format() !== searchParams.get('time')) {
&& (searchParams.get('time') === null || realtime)) {
searchParams.set('realtime', "1")
searchParams.delete("time")
setRealtime(true)
window.history.replaceState({}, '', '?' + searchParams.toString())
}
else if (datetime.format() !== searchParams.get('time') && !realtime) {
searchParams.set('time', datetime.format()) searchParams.set('time', datetime.format())
searchParams.set('realtime', "0")
window.history.replaceState({}, '', '?' + searchParams.toString()) window.history.replaceState({}, '', '?' + searchParams.toString())
} }
@ -62,14 +50,11 @@ function Station() {
}) })
const station = stationQuery.data?.stopTimes[0].place ?? {name: "Chargement…"} const station = stationQuery.data?.stopTimes[0].place ?? {name: "Chargement…"}
useEffect(() => { if (searchParams.get("time") === undefined) {
if (realtime) { setInterval(() => {
const interval = setInterval(() => {
setDatetime(dayjs()) setDatetime(dayjs())
}, 5000) }, 5000)
return () => clearInterval(interval)
} }
}, [realtime])
return ( return (
<div className="Station"> <div className="Station">
@ -78,10 +63,10 @@ function Station() {
</header> </header>
<main> <main>
<DateTimeSelector datetime={datetime} setDatetime={setDatetime} realtime={realtime} setRealtime={setRealtime} /> <DateTimeSelector datetime={datetime} setDatetime={setDatetime} />
{/*<TripsFilter />*/} <TripsFilter />
<TrainsTable station={station} datetime={datetime} realtime={realtime} tableType="departures" /> <TrainsTable station={station} datetime={datetime} tableType="departures" />
<TrainsTable station={station} datetime={datetime} realtime={realtime} tableType="arrivals" /> <TrainsTable station={station} datetime={datetime} tableType="arrivals" />
</main> </main>
</div> </div>
) )

View File

@ -27,12 +27,12 @@ const StyledTableRow = styled(TableRow)(({ theme, tabletype }) => ({
}, },
})); }));
function TrainsTable({station, datetime, realtime, tableType}) { function TrainsTable({station, datetime, tableType}) {
return <> return <>
<TableContainer> <TableContainer>
<Table> <Table>
<TrainsTableHeader tableType={tableType} /> <TrainsTableHeader tableType={tableType} />
<TrainsTableBody station={station} datetime={datetime} realtime={realtime} tableType={tableType} /> <TrainsTableBody station={station} datetime={datetime} tableType={tableType} />
</Table> </Table>
</TableContainer> </TableContainer>
</> </>
@ -50,7 +50,7 @@ function TrainsTableHeader({tableType}) {
</> </>
} }
function TrainsTableBody({station, datetime, realtime, tableType}) { function TrainsTableBody({station, datetime, tableType}) {
const filterTime = useCallback((train) => { const filterTime = useCallback((train) => {
if (tableType === "departures") if (tableType === "departures")
return dayjs(train.place.departure) >= datetime return dayjs(train.place.departure) >= datetime
@ -59,35 +59,30 @@ function TrainsTableBody({station, datetime, realtime, tableType}) {
}, [datetime, tableType]) }, [datetime, tableType])
const updateTrains = useCallback(() => { const updateTrains = useCallback(() => {
const params = { const query_params = new URLSearchParams({
stopId: station.stopId, stopId: station.stopId,
arriveBy: tableType === "arrivals", arriveBy: tableType === "arrivals",
direction: "LATER", time: datetime.format(),
n: 20, n: 20,
} }).toString()
if (!realtime)
params['time'] = datetime.format()
const query_params = new URLSearchParams(params).toString()
return fetch(`${process.env.REACT_APP_MOTIS_SERVER}/api/v1/stoptimes?${query_params}`) return fetch(`${process.env.REACT_APP_MOTIS_SERVER}/api/v1/stoptimes?${query_params}`)
.then(response => response.json()) .then(response => response.json())
.then(data => data.stopTimes) .then(data => data.stopTimes)
.then(data => [...data]) .then(data => [...data])
}, [station.stopId, tableType, datetime, realtime]) }, [station.stopId, tableType, datetime])
const trainsQuery = useQuery({ const trainsQuery = useQuery({
queryKey: ['trains', station.stopId, tableType], queryKey: ['trains', station.stopId, tableType, datetime],
queryFn: updateTrains, queryFn: updateTrains,
enabled: !!station.stopId, enabled: !!station.stopId,
}) })
const trains = useMemo(() => trainsQuery.data ?? [], [trainsQuery.data]) const trains = useMemo(() => trainsQuery.data ?? [], [trainsQuery.data])
useEffect(() => { useEffect(() => {
if (realtime) {
let validTrains = trains?.filter(filterTime) ?? [] let validTrains = trains?.filter(filterTime) ?? []
if ((trains?.length > 0 && validTrains.length < trains?.length)) if (trains?.length > 0 && validTrains.length < trains?.length)
trainsQuery.refetch().then() trainsQuery.refetch().then()
} }, [trains, filterTime, trainsQuery])
}, [trains, filterTime, trainsQuery, realtime])
const nullRef = useRef(null) const nullRef = useRef(null)
let table_rows = trains.map((train) => <CSSTransition key={train.id} timeout={500} classNames="shrink" nodeRef={nullRef}> let table_rows = trains.map((train) => <CSSTransition key={train.id} timeout={500} classNames="shrink" nodeRef={nullRef}>