From 0c715cb62af7a5248e3e9fee016bdef634d8a611 Mon Sep 17 00:00:00 2001
From: Hannah Holzheu <hh062@hdm-stuttgart.de>
Date: Wed, 13 Dec 2023 19:02:06 +0100
Subject: [PATCH] added logic for buttons to add plants to garden or wishlist

---
 .../src/components/PlantDetails.tsx           | 98 ++++++++++++++++---
 growbros-frontend/src/pages/Suche.tsx         |  1 +
 2 files changed, 88 insertions(+), 11 deletions(-)

diff --git a/growbros-frontend/src/components/PlantDetails.tsx b/growbros-frontend/src/components/PlantDetails.tsx
index 73810bd..1e2a6b7 100644
--- a/growbros-frontend/src/components/PlantDetails.tsx
+++ b/growbros-frontend/src/components/PlantDetails.tsx
@@ -1,6 +1,6 @@
 import "../stylesheets/PlantDetails.css";
 import "font-awesome/css/font-awesome.min.css";
-import { useParams } from "react-router-dom";
+import { useState } from "react";
 
 function PlantDetails(props: any) {
   const plant = props.plant;
@@ -64,27 +64,103 @@ function PlantDetails(props: any) {
             margin: "0 0 40px 0",
           }}
         >
-          <ButtonAddToGarden></ButtonAddToGarden>
-          <ButtonAddToWishlist></ButtonAddToWishlist>
+          <ButtonAddToGarden plantId={plant.id}></ButtonAddToGarden>
+          <ButtonAddToWishlist plantId={plant.id}></ButtonAddToWishlist>
         </div>
       </div>
     </>
   );
 }
 
-function ButtonAddToGarden() {
+function ButtonAddToGarden({ plantId }: any) {
+  const [status, setStatus] = useState<null | String>(null);
+  const token: String = "";
+
+  const handleButtonClick = async () => {
+    try {
+      const response = await fetch(
+        `http://localhost:8080/api/v1/garden/add/${plantId}`,
+        {
+          method: "POST",
+          headers: {
+            "Content-Type": "application/json",
+            Authorization: `Bearer ${token}`,
+          },
+        }
+      );
+      if (response.status === 201) {
+        console.log("Pflanze wurde erfolgreich dem Garten hinzugefügt");
+      } else {
+        setStatus("Pflanze konnte nicht dem Garten hinzugefügt werden");
+      }
+    } catch (error) {
+      console.error("Error", error);
+      setStatus("Pflanze konnte nicht dem Garten hinzugefügt werden");
+    }
+  };
+
   return (
-    <button>
-      <i className="fa fa-leaf"></i>
-    </button>
+    <>
+      {status && (
+        <div
+          className={`status-message ${
+            status.includes("erfolgreich") ? "success-message" : "error-message"
+          }`}
+        >
+          {status}
+        </div>
+      )}
+      <button onClick={handleButtonClick}>
+        <i className="fa fa-leaf"></i>
+      </button>
+    </>
   );
 }
 
-function ButtonAddToWishlist() {
+//TODO css für status message
+function ButtonAddToWishlist({ plantId }: any) {
+  const [status, setStatus] = useState<null | String>(null);
+  const token: String = "";
+
+  const handleButtonClick = async () => {
+    try {
+      const response = await fetch(
+        `http://localhost:8080/api/v1/wishlist/add/${plantId}`,
+        {
+          method: "POST",
+          headers: {
+            "Content-Type": "application/json",
+            Authorization: `Bearer ${token}`,
+          },
+        }
+      );
+      if (response.status === 201) {
+        console.log("Pflanze wurde erfolgreich der Wunschliste hinzugefügt");
+      } else {
+        setStatus("Pflanze konnte nicht der Wunschliste hinzugefügt werden");
+      }
+    } catch (error) {
+      console.error("Error", error);
+      setStatus("Pflanze konnte nicht der Wunschliste hinzugefügt werden");
+    }
+  };
+
   return (
-    <button>
-      <i className="fa fa-heart"></i>
-    </button>
+    <>
+      {status && (
+        <div
+          className={`status-message ${
+            status.includes("erfolgreich") ? "success-message" : "error-message"
+          }`}
+        >
+          {" "}
+          {status}
+        </div>
+      )}
+      <button onClick={handleButtonClick}>
+        <i className="fa fa-heart"></i>
+      </button>
+    </>
   );
 }
 
diff --git a/growbros-frontend/src/pages/Suche.tsx b/growbros-frontend/src/pages/Suche.tsx
index a3f068f..f467c99 100644
--- a/growbros-frontend/src/pages/Suche.tsx
+++ b/growbros-frontend/src/pages/Suche.tsx
@@ -4,6 +4,7 @@ import PlantsOverview from "../components/PlantsOverview";
 import { useState } from "react";
 import FilterPage from "../components/FilterPage";
 
+
 function Suche() {
   return (
     <>
-- 
GitLab