mirror of
https://git.srhtnin.garden/sarah/srhtnin.github.io.git
synced 2026-10-14 10:48:12 +02:00
75 lines
1.2 KiB
JavaScript
75 lines
1.2 KiB
JavaScript
let Plants = {};
|
|
|
|
let PlantsLoadPromise = null;
|
|
|
|
|
|
async function LoadPlants() {
|
|
if (PlantsLoadPromise !== null) {
|
|
return PlantsLoadPromise;
|
|
}
|
|
|
|
|
|
PlantsLoadPromise =
|
|
LoadPlantsFromApi();
|
|
|
|
|
|
try {
|
|
return await PlantsLoadPromise;
|
|
} catch (Error) {
|
|
PlantsLoadPromise = null;
|
|
|
|
throw Error;
|
|
}
|
|
}
|
|
|
|
|
|
async function LoadPlantsFromApi() {
|
|
const Response =
|
|
await fetch(
|
|
ApiUrl + "/Plants.php",
|
|
{
|
|
method: "GET",
|
|
|
|
headers: {
|
|
"Accept":
|
|
"application/json"
|
|
}
|
|
}
|
|
);
|
|
|
|
|
|
if (!Response.ok) {
|
|
throw new Error(
|
|
"Plants API returned HTTP " +
|
|
Response.status
|
|
);
|
|
}
|
|
|
|
|
|
const Result =
|
|
await Response.json();
|
|
|
|
|
|
if (
|
|
!Result.Success ||
|
|
Result.Plants === null ||
|
|
typeof Result.Plants !==
|
|
"object" ||
|
|
Array.isArray(
|
|
Result.Plants
|
|
)
|
|
) {
|
|
throw new Error(
|
|
Result.Error ??
|
|
"Plants API returned invalid data."
|
|
);
|
|
}
|
|
|
|
|
|
Plants =
|
|
Result.Plants;
|
|
|
|
|
|
return Plants;
|
|
}
|