From 9925249bf489ae960189f6daabe59263d1620c89 Mon Sep 17 00:00:00 2001 From: Ariel Costas Guerrero <94913521+arielcostas@users.noreply.github.com> Date: Mon, 9 Sep 2024 18:48:45 +0200 Subject: Favourite stops, local stop list --- src/pages/Home.tsx | 75 ++++++++++++++++++++++-------------------------- src/pages/Stop.tsx | 84 ++++++++++++++++++++++++++++++++++-------------------- 2 files changed, 87 insertions(+), 72 deletions(-) (limited to 'src/pages') diff --git a/src/pages/Home.tsx b/src/pages/Home.tsx index 5070e5f..1f5c319 100644 --- a/src/pages/Home.tsx +++ b/src/pages/Home.tsx @@ -1,46 +1,16 @@ +import { useEffect, useMemo, useState } from "react"; import { Link, useNavigate } from "react-router-dom"; -import useSWR from "swr"; +import { Stop, StopDataProvider } from "../data/stopDataProvider"; -interface Stop { - stopId: number - name: string; - latitude?: number; - longitude?: number; - lines: string[]; -} - -interface CachedStopList { - timestamp: number; - data: Stop[]; -} +const sdp = new StopDataProvider(); export function Home() { - const navigate = useNavigate() - const { data, error, isLoading } = useSWR('home', async () => { - const rawCachedData = localStorage.getItem('cachedStopList'); - if (rawCachedData) { - const parsedData: CachedStopList = JSON.parse(rawCachedData) + const [data, setData] = useState(null) + const navigate = useNavigate(); - // Cache for 12 hours - if (Date.now() - parsedData.timestamp < 1000 * 60 * 60 * 12) { - return parsedData.data - } else { - localStorage.removeItem('cachedStopList') - } - } - - const response = await fetch('/api/ListStops') - const body = await response.json(); - - const cachedData: CachedStopList = { - timestamp: Date.now(), - data: body - } - - localStorage.setItem('cachedStopList', JSON.stringify(cachedData)); - - return body; - }); + useEffect(() => { + sdp.getStops().then((stops: Stop[]) => setData(stops)) + }, []); const handleStopSearch = async (event: React.FormEvent) => { event.preventDefault() @@ -54,12 +24,15 @@ export function Home() { } } - if (isLoading) return

Loading...

- if (error) return

Error

+ const favouritedStops = useMemo(() => { + return data?.filter(stop => stop.favourite) ?? [] + }, [data]) + + if (data === null) return

Loading...

return ( <> -

Home

+

UrbanoVigo Web

@@ -72,6 +45,26 @@ export function Home() { +

Paradas favoritas

+ + {favouritedStops?.length == 1 && ( +

+ Accede a una parada y márcala como favorita para verla aquí. +

+ )} + +
    + {favouritedStops?.sort((a, b) => a.stopId - b.stopId).map((stop: Stop) => ( +
  • + + ({stop.stopId}) {stop.name} - {stop.lines?.join(', ')} + +
  • + ))} +
+ +

Paradas

+
    {data?.sort((a, b) => a.stopId - b.stopId).map((stop: Stop) => (
  • diff --git a/src/pages/Stop.tsx b/src/pages/Stop.tsx index a5effc1..4fa68cf 100644 --- a/src/pages/Stop.tsx +++ b/src/pages/Stop.tsx @@ -1,5 +1,6 @@ +import { useEffect, useState } from "react"; import { Link, useParams } from "react-router-dom"; -import useSWR from "swr"; +import { StopDataProvider } from "../data/stopDataProvider"; interface StopDetails { stop: { @@ -8,28 +9,33 @@ interface StopDetails { latitude: number; longitude: number; } - estimates: [{ + estimates: { line: string; route: string; minutes: number; meters: number; - }] + }[] } export function Stop(): JSX.Element { + const sdp = new StopDataProvider(); + const [data, setData] = useState(null); + const [favourited, setFavourited] = useState(false); const params = useParams(); - const { data, error, isLoading } = useSWR(`stop-${params.stopId}`, async () => { - let response; + const loadData = () => { + fetch(`/api/GetStopEstimates?id=${params.stopId}`) + .then(r => r.json()) + .then((body: StopDetails) => setData(body)); + }; - try { - response = await fetch(`/api/GetStopEstimates?id=${params.stopId}`) - return response.json() - } catch (error) { - console.error(error) - throw new Error(`Failed to fetch data, status ${response!.status}, body: ${await response!.text()}`) - } - }); + useEffect(() => { + loadData(); + + setFavourited( + sdp.isFavourite(parseInt(params.stopId ?? "")) + ); + }) const absoluteArrivalTime = (minutes: number) => { const now = new Date() @@ -40,9 +46,7 @@ export function Stop(): JSX.Element { }).format(arrival) } - if (isLoading) return

    Loading...

    - if (error) return

    Error: {JSON.stringify(error)}

    - if (data === undefined) return

    No data

    + if (data === null) return

    Cargando datos en tiempo real...

    return ( <> @@ -50,21 +54,31 @@ export function Stop(): JSX.Element {

    {data?.stop.name} ({data?.stop.id})

- - - - - Volver al listado de paradas - +
+ + 🔙 Volver al listado de paradas + + + {!favourited && ( + + )} + + {favourited && ( + + )} + + +
@@ -97,6 +111,14 @@ export function Stop(): JSX.Element { ))} + + {data?.estimates.length === 0 && ( + + + + + + )}
Estimaciones de llegadas
No hay estimaciones disponibles

-- cgit v1.3