Use local cache to optimize display

This commit is contained in:
2024-02-02 00:50:50 +01:00
parent edbc01122d
commit 311a29cf4b
5 changed files with 179 additions and 85 deletions

@ -1,9 +1,10 @@
import {useParams, useSearchParams} from "react-router-dom"
import TrainsTable from "./TrainsTable"
import {useEffect, useState} from "react";
import {Box, Button, FormControl, FormControlLabel, FormGroup, FormLabel} from "@mui/material";
import {DatePicker, DateTimePicker, TimePicker} from "@mui/x-date-pickers";
import {useState} from "react";
import {Box, Button, FormLabel} from "@mui/material";
import {DatePicker, TimePicker} from "@mui/x-date-pickers";
import dayjs from "dayjs";
import {useQuery, useQueryClient} from "@tanstack/react-query";
function DateTimeSelector({date, time}) {
return <>
@ -20,7 +21,6 @@ function DateTimeSelector({date, time}) {
function Station() {
let {stopId} = useParams()
let [stop, setStop] = useState({'name': "Chargement…"})
let [searchParams, _setSearchParams] = useSearchParams()
const now = new Date()
let dateNow = `${now.getFullYear()}-${String(now.getMonth() + 1).padStart(2, '0')}-${String(now.getDate()).padStart(2, '0')}`
@ -28,13 +28,14 @@ function Station() {
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}/`)
.then(response => response.json())
.then(data => {
setStop(data)
})
}, [stopId])
useQueryClient()
const stopQuery = useQuery({
queryKey: ['stop', stopId],
queryFn: () => fetch(`http://localhost:8000/api/gtfs/stop/${stopId}/`)
.then(response => response.json()),
enabled: !!stopId,
})
const stop = stopQuery.data ?? {name: "Chargement…"}
if (time === timeNow) {
setInterval(() => {
@ -43,7 +44,7 @@ function Station() {
let timeNow = `${String(now.getHours()).padStart(2, '0')}:${String(now.getMinutes()).padStart(2, '0')}`
setDate(dateNow)
setTime(timeNow)
}, 60000)
}, 5000)
}
return (