Skip to content
Snippets Groups Projects
Commit 67a0b4dc authored by Holzheu Hannah's avatar Holzheu Hannah
Browse files

updated SearchPage and added fetching randomPlants from Endpoint #12

parent c07c3efd
No related branches found
No related tags found
1 merge request!28Closes #40 : Custom useGrowbrosFetcher hook to make calls to the backend.
......@@ -92,6 +92,7 @@ function ButtonAddToGarden({ plantId }: any) {
if (response.status === 201) {
console.log("Pflanze wurde erfolgreich dem Garten hinzugefügt");
} else {
console.log("Unexpected response status:", response.status);
setStatus("Pflanze konnte nicht dem Garten hinzugefügt werden");
}
} catch (error) {
......
import "font-awesome/css/font-awesome.min.css";
import "../stylesheets/Suche.css";
import PlantsOverview from "../components/PlantsOverview";
import { useState } from "react";
import { useState, useEffect } from "react";
import FilterPage from "../components/FilterPage";
function Suche() {
const [randomPlants, setRandomPlants] = useState<plant[] | null>(null);
const [error, setError] = useState<null | String>(null);
const randomPlantsCount: number = 30;
const token: String = "";
const fetchRandomPlants = async () => {
try {
const response = await fetch(
`http://localhost:8080/api/v1/plants/randomPlants/${randomPlantsCount}`,
{
method: "GET",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${token}`,
},
}
);
if (response.status == 200) {
const data = await response.json();
setRandomPlants(data);
console.log("Succesfully fetched data");
} else {
console.log("Unexpected response status:", response.status);
setError("Error fetching data");
}
} catch (error) {
console.error("Error", error);
setError("Error fetching data");
}
};
useEffect(() => {
fetchRandomPlants();
}, []);
return (
<>
<SearchBar />
<PlantsOverview />
<div>
{error ? <div>{error}</div> : <PlantsOverview plants={randomPlants} />}
</div>
</>
);
}
......
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