Compare commits

..

No commits in common. "2120275e14ae9dbfa1d79dd9799414c2de1c019a" and "836f2d39ee2f5bc5750d1768590e9d48e76e5d20" have entirely different histories.

View File

@ -10,15 +10,15 @@ import {
Typography Typography
} from "@mui/material" } from "@mui/material"
import {CSSTransition, TransitionGroup} from 'react-transition-group' import {CSSTransition, TransitionGroup} from 'react-transition-group'
import {useQueries, useQuery} from "@tanstack/react-query"; import {useQueries, useQuery, useQueryClient} from "@tanstack/react-query";
import {useCallback, useEffect, useMemo, useRef} from "react"; import {useEffect, useState} from "react";
const StyledTableRow = styled(TableRow)(({ theme, tabletype }) => ({ const StyledTableRow = styled(TableRow)(({ theme, tableType }) => ({
'tbody &:nth-of-type(odd)': { 'tbody &:nth-of-type(odd)': {
backgroundColor: theme.palette.sncf[tabletype].light, backgroundColor: theme.palette.sncf[tableType].light,
}, },
'th, &:nth-of-type(even)': { 'th, &:nth-of-type(even)': {
backgroundColor: theme.palette.sncf[tabletype].dark, backgroundColor: theme.palette.sncf[tableType].dark,
}, },
// hide last border // hide last border
'&:last-child td, &:last-child th': { '&:last-child td, &:last-child th': {
@ -40,7 +40,7 @@ function TrainsTable({stop, date, time, tableType}) {
function TrainsTableHeader({tableType}) { function TrainsTableHeader({tableType}) {
return <> return <>
<TableHead> <TableHead>
<StyledTableRow tabletype={tableType}> <StyledTableRow tableType={tableType}>
<TableCell colSpan="2" fontSize={16} fontWeight="bold">Train</TableCell> <TableCell colSpan="2" fontSize={16} fontWeight="bold">Train</TableCell>
<TableCell fontSize={16} fontWeight="bold">Heure</TableCell> <TableCell fontSize={16} fontWeight="bold">Heure</TableCell>
<TableCell fontSize={16} fontWeight="bold">Destination</TableCell> <TableCell fontSize={16} fontWeight="bold">Destination</TableCell>
@ -50,35 +50,39 @@ function TrainsTableHeader({tableType}) {
} }
function TrainsTableBody({stop, date, time, tableType}) { function TrainsTableBody({stop, date, time, tableType}) {
const filterTime = useCallback((train) => { const queryClient = useQueryClient()
let filterTime = (train) => {
if (train.departure_time === "04:56:00")
return false
if (tableType === "departures") if (tableType === "departures")
return `${train.departure_date}T${train.departure_time_24h}` >= `${date}T${time}` return `${train.departure_date}T${train.departure_time_24h}` >= `${date}T${time}`
else else
return `${train.arrival_date}T${train.arrival_time_24h}` >= `${date}T${time}` return `${train.arrival_date}T${train.arrival_time_24h}` >= `${date}T${time}`
}, [date, time, tableType]) }
const updateTrains = useCallback(() => { function updateTrains() {
return fetch(`/api/station/next_${tableType}/?stop_id=${stop.id}&date=${date}&time=${time}&offset=${0}&limit=${20}`) return fetch(`/api/station/next_${tableType}/?stop_id=${stop.id}&date=${date}&time=${time}&offset=${0}&limit=${20}`)
.then(response => response.json()) .then(response => response.json())
.then(data => data.results) .then(data => data.results)
.then(data => [...data]) .then(data => [...data])
}, [stop.id, date, time, tableType]) }
const trainsQuery = useQuery({ const trainsQuery = useQuery({
queryKey: ['trains', stop.id, tableType], queryKey: ['trains', stop.id, tableType],
queryFn: updateTrains, queryFn: updateTrains,
enabled: !!stop.id, enabled: !!stop.id,
}) })
const trains = useMemo(() => trainsQuery.data ?? [], [trainsQuery.data]) const trains = trainsQuery.data ?? []
useEffect(() => { useEffect(() => {
let validTrains = trains?.filter(filterTime) ?? [] let validTrains = trains?.filter(filterTime) ?? []
if (trains?.length > 0 && validTrains.length < trains?.length) console.log(validTrains.length)
trainsQuery.refetch().then() if (trains?.length > 0 && validTrains.length <= trains?.length)
}, [trains, filterTime, trainsQuery]) queryClient.invalidateQueries({queryKey: ['trains', stop.id, tableType]})
}, [stop.id, tableType, date, time])
const nullRef = useRef(null) let table_rows = trains.map((train) => <CSSTransition key={train.id} timeout={500} classNames="shrink">
let table_rows = trains.map((train) => <CSSTransition key={train.id} timeout={500} classNames="shrink" nodeRef={nullRef}>
<TrainRow train={train} tableType={tableType} /> <TrainRow train={train} tableType={tableType} />
</CSSTransition>) </CSSTransition>)
@ -139,7 +143,7 @@ function TrainRow({train, tableType}) {
let stopsNames = stops.filter(stopsFilter).map(stopTime => stopTime?.stop.name ?? "").join(", ") ?? "" let stopsNames = stops.filter(stopsFilter).map(stopTime => stopTime?.stop.name ?? "").join(", ") ?? ""
return <> return <>
<StyledTableRow tabletype={tableType}> <StyledTableRow tableType={tableType}>
<TableCell> <TableCell>
<div> <div>
<Box display="flex" <Box display="flex"