mirror of
https://git.srhtnin.garden/sarah/srhtnin.github.io.git
synced 2026-10-13 18:38:11 +02:00
188 lines
3.8 KiB
JavaScript
188 lines
3.8 KiB
JavaScript
let Plants = {};
|
|
|
|
|
|
async function FetchPlantsFromApi() {
|
|
const Response =
|
|
await fetch(
|
|
ApiUrl + "/Plants.php",
|
|
{
|
|
method: "GET",
|
|
|
|
headers: {
|
|
"Accept":
|
|
"application/json"
|
|
},
|
|
|
|
cache: "no-store"
|
|
}
|
|
);
|
|
|
|
|
|
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."
|
|
);
|
|
}
|
|
|
|
|
|
return Result.Plants;
|
|
}
|
|
|
|
function GetPlantImageSources(
|
|
PlantOrKey
|
|
) {
|
|
const PlantKey =
|
|
typeof PlantOrKey === "string"
|
|
? PlantOrKey
|
|
: Object.entries(
|
|
Plants
|
|
).find(
|
|
([, Candidate]) =>
|
|
Candidate ===
|
|
PlantOrKey ||
|
|
Number(Candidate?.Id) ===
|
|
Number(PlantOrKey?.Id)
|
|
)?.[0] ?? null;
|
|
|
|
const Plant =
|
|
typeof PlantOrKey === "string"
|
|
? Plants[PlantOrKey]
|
|
: PlantOrKey;
|
|
|
|
const StaticImages =
|
|
PlantKey !== null &&
|
|
typeof PlantKey === "string" &&
|
|
Array.isArray(
|
|
PlantImages[PlantKey]
|
|
)
|
|
? PlantImages[PlantKey]
|
|
: [];
|
|
|
|
const ApiStages =
|
|
Array.isArray(
|
|
Plant?.ImageStages
|
|
)
|
|
? Plant.ImageStages
|
|
: [];
|
|
|
|
let HighestStage =
|
|
StaticImages.length;
|
|
|
|
for (const ImageStage of ApiStages) {
|
|
const Stage = Number(
|
|
typeof ImageStage === "object" &&
|
|
ImageStage !== null
|
|
? ImageStage.Stage
|
|
: ImageStage
|
|
);
|
|
|
|
if (
|
|
Number.isInteger(Stage) &&
|
|
Stage > HighestStage
|
|
) {
|
|
HighestStage = Stage;
|
|
}
|
|
}
|
|
|
|
const Images =
|
|
Array.from(
|
|
{length: HighestStage},
|
|
(_, Index) =>
|
|
StaticImages[Index] ?? null
|
|
);
|
|
|
|
if (Plant === undefined || Plant === null) {
|
|
return Images;
|
|
}
|
|
|
|
for (const ImageStage of ApiStages) {
|
|
const Stage = Number(
|
|
typeof ImageStage === "object" &&
|
|
ImageStage !== null
|
|
? ImageStage.Stage
|
|
: ImageStage
|
|
);
|
|
|
|
if (
|
|
!Number.isInteger(Stage) ||
|
|
Stage < 1
|
|
) {
|
|
continue;
|
|
}
|
|
|
|
const Revision =
|
|
typeof ImageStage === "object" &&
|
|
ImageStage !== null
|
|
? Number(
|
|
ImageStage.Revision ?? 0
|
|
)
|
|
: 0;
|
|
|
|
const Query =
|
|
new URLSearchParams({
|
|
PlantId: String(Plant.Id),
|
|
Stage: String(Stage),
|
|
v: String(
|
|
Number.isFinite(Revision)
|
|
? Revision
|
|
: 0
|
|
)
|
|
});
|
|
|
|
Images[Stage - 1] =
|
|
ApiUrl +
|
|
"/PlantImage.php?" +
|
|
Query.toString();
|
|
}
|
|
|
|
return Images;
|
|
}
|
|
|
|
|
|
function GetPlantMatureImageSource(
|
|
PlantOrKey
|
|
) {
|
|
const Images =
|
|
GetPlantImageSources(
|
|
PlantOrKey
|
|
);
|
|
|
|
for (
|
|
let Index = Images.length - 1;
|
|
Index >= 0;
|
|
Index--
|
|
) {
|
|
if (
|
|
typeof Images[Index] ===
|
|
"string" &&
|
|
Images[Index].length > 0
|
|
) {
|
|
return Images[Index];
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|