Compare commits
2 Commits
f0964d8fb7
...
6389406744
Author | SHA1 | Date |
---|---|---|
Emmy D'Anello | 6389406744 | |
Emmy D'Anello | 41441a7803 |
|
@ -1,14 +1,13 @@
|
||||||
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 TripsFilter from "./TripsFilter"
|
import {useEffect, useState} from "react"
|
||||||
import {useState} from "react"
|
import {Box, Checkbox, FormLabel} from "@mui/material"
|
||||||
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}) {
|
function DateTimeSelector({datetime, setDatetime, realtime, setRealtime}) {
|
||||||
const navigate = useNavigate()
|
const navigate = useNavigate()
|
||||||
|
|
||||||
function onStationSelected(event, station) {
|
function onStationSelected(event, station) {
|
||||||
|
@ -18,14 +17,18 @@ function DateTimeSelector({datetime, setDatetime}) {
|
||||||
|
|
||||||
return <>
|
return <>
|
||||||
<Box component="form" display="flex" alignItems="center" sx={{'& .MuiTextField-root': { m: 1, width: '25ch' },}}>
|
<Box component="form" display="flex" alignItems="center" sx={{'& .MuiTextField-root': { m: 1, width: '25ch' },}}>
|
||||||
<FormLabel>
|
<FormLabel>
|
||||||
Changer la gare recherchée :
|
Changer la gare recherchée :
|
||||||
</FormLabel>
|
</FormLabel>
|
||||||
<AutocompleteStation onChange={onStationSelected} />
|
<AutocompleteStation onChange={onStationSelected} />
|
||||||
<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} />
|
<DateTimePicker name="date" label="Date" onChange={setDatetime} value={datetime} disabled={realtime} readOnly={realtime} />
|
||||||
|
<Checkbox onChange={event => setRealtime(event.target.checked)} checked={realtime} />
|
||||||
|
<FormLabel>
|
||||||
|
Temps réel
|
||||||
|
</FormLabel>
|
||||||
</Box>
|
</Box>
|
||||||
</>
|
</>
|
||||||
}
|
}
|
||||||
|
@ -35,9 +38,18 @@ 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 (datetime.format() !== searchParams.get('time')) {
|
if ((searchParams.get('realtime') === null || searchParams.get('realtime') === "0")
|
||||||
|
&& (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())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -50,11 +62,14 @@ function Station() {
|
||||||
})
|
})
|
||||||
const station = stationQuery.data?.stopTimes[0].place ?? {name: "Chargement…"}
|
const station = stationQuery.data?.stopTimes[0].place ?? {name: "Chargement…"}
|
||||||
|
|
||||||
if (searchParams.get("time") === undefined) {
|
useEffect(() => {
|
||||||
setInterval(() => {
|
if (realtime) {
|
||||||
setDatetime(dayjs())
|
const interval = setInterval(() => {
|
||||||
}, 5000)
|
setDatetime(dayjs())
|
||||||
}
|
}, 5000)
|
||||||
|
return () => clearInterval(interval)
|
||||||
|
}
|
||||||
|
}, [realtime])
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="Station">
|
<div className="Station">
|
||||||
|
@ -63,10 +78,10 @@ function Station() {
|
||||||
</header>
|
</header>
|
||||||
|
|
||||||
<main>
|
<main>
|
||||||
<DateTimeSelector datetime={datetime} setDatetime={setDatetime} />
|
<DateTimeSelector datetime={datetime} setDatetime={setDatetime} realtime={realtime} setRealtime={setRealtime} />
|
||||||
<TripsFilter />
|
{/*<TripsFilter />*/}
|
||||||
<TrainsTable station={station} datetime={datetime} tableType="departures" />
|
<TrainsTable station={station} datetime={datetime} realtime={realtime} tableType="departures" />
|
||||||
<TrainsTable station={station} datetime={datetime} tableType="arrivals" />
|
<TrainsTable station={station} datetime={datetime} realtime={realtime} tableType="arrivals" />
|
||||||
</main>
|
</main>
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
|
|
|
@ -27,12 +27,12 @@ const StyledTableRow = styled(TableRow)(({ theme, tabletype }) => ({
|
||||||
},
|
},
|
||||||
}));
|
}));
|
||||||
|
|
||||||
function TrainsTable({station, datetime, tableType}) {
|
function TrainsTable({station, datetime, realtime, tableType}) {
|
||||||
return <>
|
return <>
|
||||||
<TableContainer>
|
<TableContainer>
|
||||||
<Table>
|
<Table>
|
||||||
<TrainsTableHeader tableType={tableType} />
|
<TrainsTableHeader tableType={tableType} />
|
||||||
<TrainsTableBody station={station} datetime={datetime} tableType={tableType} />
|
<TrainsTableBody station={station} datetime={datetime} realtime={realtime} tableType={tableType} />
|
||||||
</Table>
|
</Table>
|
||||||
</TableContainer>
|
</TableContainer>
|
||||||
</>
|
</>
|
||||||
|
@ -50,7 +50,7 @@ function TrainsTableHeader({tableType}) {
|
||||||
</>
|
</>
|
||||||
}
|
}
|
||||||
|
|
||||||
function TrainsTableBody({station, datetime, tableType}) {
|
function TrainsTableBody({station, datetime, realtime, 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,30 +59,35 @@ function TrainsTableBody({station, datetime, tableType}) {
|
||||||
}, [datetime, tableType])
|
}, [datetime, tableType])
|
||||||
|
|
||||||
const updateTrains = useCallback(() => {
|
const updateTrains = useCallback(() => {
|
||||||
const query_params = new URLSearchParams({
|
const params = {
|
||||||
stopId: station.stopId,
|
stopId: station.stopId,
|
||||||
arriveBy: tableType === "arrivals",
|
arriveBy: tableType === "arrivals",
|
||||||
time: datetime.format(),
|
direction: "LATER",
|
||||||
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])
|
}, [station.stopId, tableType, datetime, realtime])
|
||||||
|
|
||||||
const trainsQuery = useQuery({
|
const trainsQuery = useQuery({
|
||||||
queryKey: ['trains', station.stopId, tableType, datetime],
|
queryKey: ['trains', station.stopId, tableType],
|
||||||
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(() => {
|
||||||
let validTrains = trains?.filter(filterTime) ?? []
|
if (realtime) {
|
||||||
if (trains?.length > 0 && validTrains.length < trains?.length)
|
let validTrains = trains?.filter(filterTime) ?? []
|
||||||
trainsQuery.refetch().then()
|
if ((trains?.length > 0 && validTrains.length < trains?.length))
|
||||||
}, [trains, filterTime, trainsQuery])
|
trainsQuery.refetch().then()
|
||||||
|
}
|
||||||
|
}, [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}>
|
||||||
|
|
Loading…
Reference in New Issue