blob: 2ee077c2dc3a56b5b853cfed001ed36a53e34af6 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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>
</>
)
}
|