From dc20c2ba8377a90a3170642c9b3df6cd5166ad72 Mon Sep 17 00:00:00 2001
From: Ariel Costas Guerrero <94913521+arielcostas@users.noreply.github.com>
Date: Sun, 25 Aug 2024 23:12:48 +0200
Subject: Initial commit

---
 src/pages/Stop.tsx | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 69 insertions(+)
 create mode 100644 src/pages/Stop.tsx

(limited to 'src/pages/Stop.tsx')

diff --git a/src/pages/Stop.tsx b/src/pages/Stop.tsx
new file mode 100644
index 0000000..2ee077c
--- /dev/null
+++ b/src/pages/Stop.tsx
@@ -0,0 +1,69 @@
+import { useParams } from "react-router-dom";
+import useSWR from "swr";
+
+interface StopDetails {
+	stop: {
+		id: number;
+		name: string;
+		latitude: number;
+		longitude: number;
+	}
+	estimates: [{
+		line: string;
+		route: string;
+		minutes: number;
+		meters: number;
+	}]
+}
+
+export function Stop(): JSX.Element {
+	const params = useParams();
+
+	const { data, error, isLoading } = useSWR<StopDetails>('home', async () => {
+		let response;
+
+		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()}`)
+		}
+	});
+
+	if (isLoading) return <h1>Loading...</h1>
+	if (error) return <h1>Error: {JSON.stringify(error)}</h1>
+	if (data === undefined) return <h1>No data</h1>
+
+	return (
+		<>
+			<h1>{data?.stop.name} ({data?.stop.id})</h1>
+
+			<table>
+				<caption>Estimaciones de llegadas</caption>
+
+				<thead>
+					<tr>
+						<th>Línea</th>
+						<th>Ruta</th>
+						<th>Minutos</th>
+						<th>Metros</th>
+					</tr>
+				</thead>
+
+				<tbody>
+					{data.estimates
+						.sort((a, b) => a.minutes - b.minutes)
+						.map((estimate, idx) => (
+							<tr key={idx}>
+								<td>{estimate.line}</td>
+								<td>{estimate.route}</td>
+								<td>{estimate.minutes}</td>
+								<td>{estimate.meters}</td>
+							</tr>
+						))}
+				</tbody>
+			</table>
+		</>
+	)
+}
-- 
cgit v1.3

