53 lines
2.4 KiB
React
53 lines
2.4 KiB
React
import { useEffect, useState } from "react";
|
|
import { Button, Card, Group, Select, Text, TextInput } from "@mantine/core";
|
|
import { notifications } from "@mantine/notifications";
|
|
|
|
// Une requête à l'API ; un refus du serveur s'affiche en notification -> le JSON rendu, ou null
|
|
export async function appel(chemin, methode = "GET", corps) {
|
|
try {
|
|
const r = await fetch(chemin, corps === undefined ? { method: methode }
|
|
: { method: methode, headers: { "Content-Type": "application/json" }, body: JSON.stringify(corps) });
|
|
const json = await r.json().catch(() => ({ erreur: r.statusText }));
|
|
if (r.ok) return json;
|
|
notifications.show({ color: "red", title: "Refusé", message: json.erreur || r.statusText });
|
|
} catch (e) {
|
|
notifications.show({ color: "red", title: "Serveur injoignable", message: e.message });
|
|
}
|
|
return null;
|
|
}
|
|
|
|
export const poster = async (chemin, corps) => (await appel(chemin, "POST", corps)) !== null;
|
|
|
|
export function Carte({ surtitre, titre, action, children, ...reste }) {
|
|
return (
|
|
<Card shadow="sm" padding={0} withBorder {...reste}>
|
|
<Group px="lg" py="md" justify="space-between" wrap="nowrap" style={{ borderBottom: "1px solid var(--mantine-color-default-border)" }}>
|
|
<div>
|
|
<Text size="xs" fw={700} tt="uppercase" c="dimmed">{surtitre}</Text>
|
|
<Text fw={500}>{titre}</Text>
|
|
</div>
|
|
{action}
|
|
</Group>
|
|
{children}
|
|
</Card>
|
|
);
|
|
}
|
|
|
|
// La station et la plage (UTC, Au exclue ; vides : toute la période), partagées par Images et Data via l'adresse
|
|
export function Selecteur({ selection, choisir }) {
|
|
const [stations, setStations] = useState([]), [s, setS] = useState(selection);
|
|
useEffect(() => { appel("/api/stations").then(l => l && setStations(l)); }, []);
|
|
useEffect(() => setS(selection), [selection.station, selection.du, selection.au]);
|
|
return (
|
|
<Card shadow="sm" withBorder>
|
|
<Group align="flex-end" gap="md">
|
|
<Select label="Station" placeholder="Choisir" data={stations} value={s.station || null} searchable w={200}
|
|
onChange={v => setS({ ...s, station: v || "" })} />
|
|
<TextInput label="Du (UTC)" type="date" value={s.du} onChange={e => setS({ ...s, du: e.currentTarget.value })} />
|
|
<TextInput label="Au (exclu)" type="date" value={s.au} onChange={e => setS({ ...s, au: e.currentTarget.value })} />
|
|
<Button onClick={() => choisir(s)} disabled={!s.station}>Charger</Button>
|
|
</Group>
|
|
</Card>
|
|
);
|
|
}
|