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 (
{surtitre}{titre}
{action}
{children}
);
}
// 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 (
);
}