35 lines
1.8 KiB
React
35 lines
1.8 KiB
React
import { useEffect, useState } from "react";
|
|||
|
|
import { Button, Card, Group, Select, TextInput } from "@mantine/core";
|
||
|
|
import { notifications } from "@mantine/notifications";
|
||
|
|
|
||
|
|
// A request to the API; a refusal of the server shows as a notification -> the JSON returned, or null
|
||
|
|
export async function call(path, method = "GET", body) {
|
||
|
|
try {
|
||
|
|
const r = await fetch(path, body === undefined ? { method }
|
||
|
|
: { method, headers: { "Content-Type": "application/json" }, body: JSON.stringify(body) });
|
||
|
|
const json = await r.json().catch(() => ({ error: r.statusText }));
|
||
|
|
if (r.ok) return json;
|
||
|
|
notifications.show({ color: "red", title: "Refused", message: json.error || r.statusText });
|
||
|
|
} catch (e) {
|
||
|
|
notifications.show({ color: "red", title: "Server unreachable", message: e.message });
|
||
|
|
}
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
|
||
|
|
// The station and the range (UTC, To excluded; empty: the whole period), shared by the pages through the address
|
||
|
|
export function Selector({ selection, choose }) {
|
||
|
|
const [stations, setStations] = useState([]), [s, setS] = useState(selection);
|
||
|
|
useEffect(() => { call("/api/stations").then(l => l && setStations(l)); }, []);
|
||
|
|
useEffect(() => setS(selection), [selection.station, selection.start, selection.end]);
|
||
|
|
return (
|
||
|
|
<Card shadow="sm" withBorder>
|
||
|
|
<Group align="flex-end" gap="md">
|
||
|
|
<Select label="Station" placeholder="Choose" data={stations} value={s.station || null} searchable w={200}
|
||
|
|
onChange={v => setS({ ...s, station: v || "" })} />
|
||
|
|
<TextInput label="From (UTC)" type="date" value={s.start} onChange={e => setS({ ...s, start: e.currentTarget.value })} />
|
||
|
|
<TextInput label="To (excluded)" type="date" value={s.end} onChange={e => setS({ ...s, end: e.currentTarget.value })} />
|
||
|
|
<Button onClick={() => choose(s)} disabled={!s.station}>Load</Button>
|
||
|
|
</Group>
|
||
|
|
</Card>
|
||
|
|
);
|
||
|
|
}
|