Skip to content
Snippets Groups Projects
Commit 4d9a9c91 authored by Blersch Lara's avatar Blersch Lara
Browse files

#15 when filter option is selected, the page is reloaded; added button to clear the garden

parent 7fe1162c
No related branches found
No related tags found
1 merge request!41Wishlist frontend
import PlantsOverview from "../components/PlantsOverview"; import {useState, useEffect} from "react";
import PlantCard from "../components/PlantCard.tsx";
import DropDownFilter from "../components/DropDownFilter.tsx";
function Garten() { function Garten() {
return (
<div> const [plants, setPlants] = useState<any[]>([]);
<h2>Dein Garten</h2> const [error, setError] = useState();
<PlantsOverview />
</div> const token: String = "";
);
const [currentSort, setCurrentSort] = useState<string>("")
useEffect(() => {
fetch("http://localhost:8080/api/v1/garden?" + new URLSearchParams({sort: currentSort}),
{
method: 'GET',
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${token}`,
},
})
.then((res) => res.json())
.then((plants) => setPlants(plants))
.catch((err) => setError(err));
}, [setPlants, currentSort]);
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.")) {
useEffect(() => {
fetch("http://localhost:8080/api/v1/garden/remove/all",
{
method: 'GET',
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${token}`,
},
})
.then((res) => res.json())
.then((plants) => setPlants(plants))
.catch((err) => setError(err));
}, [setPlants]);
}
}
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;
default:
setCurrentSort("");
}
}
return (
<>
<div>
<h2>Deine Wunschliste</h2>
<DropDownFilter
topic={"Sortierung der Pflanzen im Garten"}
options={[
"neueste zuerst",
"als nächstes anpflanzbar",
"als nächstes erntbar",
]}
filterOnChange={handleSortOptions}
/>
<button onClick={handleClearGarden}>Garten leeren</button>
</div>
{error ? (
<p>Error fetching data</p>
) : (
<ul style={{display: "flex", gap: "1rem", width: "100%"}}>
{plants?.map((plant) => (
<PlantCard plant={plant} key={plant.id}/>
))}
</ul>
)}
</>
);
} }
export default Garten; export default Garten;
...@@ -28,7 +28,17 @@ function Wunschliste() { ...@@ -28,7 +28,17 @@ function Wunschliste() {
if (confirm("Möchtest du wirklich alle Pflanzen aus deiner Wunschliste entfernen?" + "\n" + if (confirm("Möchtest du wirklich alle Pflanzen aus deiner Wunschliste entfernen?" + "\n" +
"Diese Aktion kann nicht rückgängig gemacht werden.")) { "Diese Aktion kann nicht rückgängig gemacht werden.")) {
useEffect(() => { useEffect(() => {
fetch("http://localhost:8080/api/v1/wishlist/remove/all") fetch("http://localhost:8080/api/v1/wishlist/remove/all",
{
method: 'GET',
headers:
{
"Content-Type":
"application/json",
Authorization:
`Bearer ${token}`,
},
})
.then((res) => res.json()) .then((res) => res.json())
.then((plants) => setPlants(plants)) .then((plants) => setPlants(plants))
.catch((err) => setError(err)); .catch((err) => setError(err));
...@@ -47,7 +57,8 @@ function Wunschliste() { ...@@ -47,7 +57,8 @@ function Wunschliste() {
case "jetzt anpflanzbar": case "jetzt anpflanzbar":
setCurrentSort("currentPlantable"); setCurrentSort("currentPlantable");
break; break;
default: setCurrentSort(""); default:
setCurrentSort("");
} }
} }
...@@ -81,5 +92,4 @@ function Wunschliste() { ...@@ -81,5 +92,4 @@ function Wunschliste() {
} }
export default Wunschliste; export default Wunschliste;
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment