Newer
Older
import DropDownFilter from "../components/DropDownFilter";

Karsch Lukas
committed
import {useEffect, useState} from "react";
import useGrowbrosFetcher from "../utils/useGrowbrosFetcher";

Karsch Lukas
committed
import {Plant} from "../utils/schemas";
import {IBackendConnector} from "../utils/IBackendConnector";
import PlantsOverview from "../components/PlantsOverview";
function Garten() {
Blersch Lara
committed
const [plants, setPlantsInGarden] = useState<Plant[]>([]);
const [error, setError] = useState<object>();
Blersch Lara
committed
const [currentSort, setCurrentSort] = useState<string>("")
Blersch Lara
committed
const bc: IBackendConnector = useGrowbrosFetcher();
Blersch Lara
committed
useEffect(() => {
Blersch Lara
committed
(async () => {
const result = await bc.getGardenEntries(currentSort);
Blersch Lara
committed
if (result.err) {
setError(result.err);
} else {
setPlantsInGarden(result.value!);
}
})();
}, [currentSort]);
Blersch Lara
committed
const handleClearGarden = async () => {
if (confirm("Möchtest du wirklich alle Pflanzen aus deinem Garten entfernen?" + "\n" +
"Diese Aktion kann nicht rückgängig gemacht werden.")) {
const result = await bc.clearGarden();
if (result.err) {
setError(result.err);
} else {
setError(undefined);
setPlantsInGarden([]);
}
Blersch Lara
committed
}
}
const handleSortOptions = async (selectedOption: string) => {
switch (selectedOption) {
case "neueste zuerst":
setCurrentSort("createdAt");
break;
case "als nächstes anpflanzbar":
setCurrentSort("plantDate");
break;
case "als nächstes erntbar":
setCurrentSort("harvestDate");
break;
Blersch Lara
committed
case "alphabetisch sortiert":
Blersch Lara
committed
default:
setCurrentSort("");
}
}
Blersch Lara
committed
return (
<>
<div style={{padding: "20px"}}>
<h2>Dein Garten</h2>
<DropDownFilter
filterOnChange={handleSortOptions}
topic={"Sortierung der Pflanzen im Garten"}
options={[
Blersch Lara
committed
"alphabetisch sortiert",
Blersch Lara
committed
"neueste zuerst",
"als nächstes anpflanzbar",
"als nächstes erntbar",
]}
/>
<button onClick={handleClearGarden}>Garten leeren</button>
</div>
{error && (
<div style={{padding: "20px"}}>Es ist ein Fehler aufgetreten...</div>
)}
<PlantsOverview plants={plants} quickActions={["REMOVE_FROM_GARDEN"]}/>
Blersch Lara
committed
</>
);
}
Blersch Lara
committed
export default Garten;