From bf03fa12f6c536364e33b7e801a5af67664a87c6 Mon Sep 17 00:00:00 2001 From: Atharva Date: Sat, 22 Feb 2025 19:16:35 +0530 Subject: [PATCH] feat:Added forms to add,update,delete the farm --- Frontend/src/main.jsx | 4 + Frontend/src/pages/UserPanel/AddFarm.jsx | 131 +++++++++++++++++++ Frontend/src/pages/UserPanel/Dashboard.jsx | 8 +- Frontend/src/pages/UserPanel/DeleteFarm.jsx | 6 + Frontend/src/pages/UserPanel/UpdateForm.jsx | 137 ++++++++++++++++++++ 5 files changed, 284 insertions(+), 2 deletions(-) create mode 100644 Frontend/src/pages/UserPanel/AddFarm.jsx create mode 100644 Frontend/src/pages/UserPanel/DeleteFarm.jsx create mode 100644 Frontend/src/pages/UserPanel/UpdateForm.jsx diff --git a/Frontend/src/main.jsx b/Frontend/src/main.jsx index 5620b0d..a290234 100644 --- a/Frontend/src/main.jsx +++ b/Frontend/src/main.jsx @@ -23,6 +23,8 @@ import ScheduleMeeting from "./pages/UserPanel/ScheduleMeeting.jsx"; import Support from "./pages/UserPanel/Support.jsx"; import FeedBackAndRatings from "./pages/UserPanel/FeedBackAndRatings.jsx"; import Monitoring from "./pages/UserPanel/Monitoring.jsx"; +import AddFarm from "./pages/UserPanel/AddFarm.jsx"; +import UpdateFarm from "./pages/UserPanel/UpdateForm.jsx"; createRoot(document.getElementById("root")).render( @@ -55,6 +57,8 @@ createRoot(document.getElementById("root")).render( } /> } /> } /> + } /> + } /> diff --git a/Frontend/src/pages/UserPanel/AddFarm.jsx b/Frontend/src/pages/UserPanel/AddFarm.jsx new file mode 100644 index 0000000..ee182ca --- /dev/null +++ b/Frontend/src/pages/UserPanel/AddFarm.jsx @@ -0,0 +1,131 @@ +import React, { useState } from "react"; +import { useNavigate } from "react-router-dom"; + +const AddFarm = () => { + const [farmName, setFarmName] = useState(""); + const [location, setLocation] = useState(""); + const [waterContent, setWaterContent] = useState(""); + const [soilType, setSoilType] = useState(""); + const [error, setError] = useState(null); + const [success, setSuccess] = useState(false); + const navigate = useNavigate(); + const handleSubmit = async (e) => { + e.preventDefault(); + const farmData = { + name: farmName, + location, + waterContent, + soilType, + }; + + try { + const response = await fetch("http://localhost:8000/api/v1/farm", { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify(farmData), + }); + + if (!response.ok) { + throw new Error("Failed to add farm"); + } + + setSuccess(true); + setError(null); + // navigate to the dashboard: + navigate("/dashboard"); + } catch (err) { + setError(err.message); + setSuccess(false); + } + }; + + return ( +
+

Add Farm

+
+
+
+ + setFarmName(e.target.value)} + className="shadow-xs bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white" + required + /> +
+
+ + setLocation(e.target.value)} + className="shadow-xs bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white" + required + /> +
+
+ + setWaterContent(e.target.value)} + className="shadow-xs bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white" + required + /> +
+
+ + setSoilType(e.target.value)} + className="shadow-xs bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white" + required + /> +
+ + {error &&

{error}

} + {success && ( +

+ Farm added successfully! +

+ )} +
+
+
+ ); +}; + +export default AddFarm; diff --git a/Frontend/src/pages/UserPanel/Dashboard.jsx b/Frontend/src/pages/UserPanel/Dashboard.jsx index 58e03ae..658881e 100644 --- a/Frontend/src/pages/UserPanel/Dashboard.jsx +++ b/Frontend/src/pages/UserPanel/Dashboard.jsx @@ -1,15 +1,19 @@ import React from "react"; +import { useNavigate } from "react-router-dom"; import Piechart from "../../components/monitoring charts/Piechart"; import TotalSpent from "../../components/TotalSpent"; import FarmList from "../../components/FarmList"; const Dashboard = () => { + const navigate = useNavigate(); + return (
- +
); }; diff --git a/Frontend/src/pages/UserPanel/DeleteFarm.jsx b/Frontend/src/pages/UserPanel/DeleteFarm.jsx new file mode 100644 index 0000000..ec042b2 --- /dev/null +++ b/Frontend/src/pages/UserPanel/DeleteFarm.jsx @@ -0,0 +1,6 @@ +const DeleteFarm = () => { + return ( + + ) +}; +export default DeleteFarm; diff --git a/Frontend/src/pages/UserPanel/UpdateForm.jsx b/Frontend/src/pages/UserPanel/UpdateForm.jsx new file mode 100644 index 0000000..2c0a277 --- /dev/null +++ b/Frontend/src/pages/UserPanel/UpdateForm.jsx @@ -0,0 +1,137 @@ +import React, { useState } from "react"; +import { useNavigate } from "react-router-dom"; + +const UpdateFarm = () => { + const [farmName, setFarmName] = useState(""); + const [location, setLocation] = useState(""); + const [waterContent, setWaterContent] = useState(""); + const [soilType, setSoilType] = useState(""); + const [error, setError] = useState(null); + const [success, setSuccess] = useState(false); + const navigate = useNavigate(); + const handleSubmit = async (e) => { + e.preventDefault(); + const farmData = { + name: farmName, + location, + waterContent, + soilType, + }; + + try { + const response = await fetch( + "http://localhost:8000/api/v1/farm/67b9b3a1b68365aa35ae0e5f", + { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify(farmData), + } + ); + + if (!response.ok) { + throw new Error("Failed to Update the farm"); + } + + setSuccess(true); + setError(null); + // navigate to the dashboard: + navigate("/dashboard"); + } catch (err) { + setError(err.message); + setSuccess(false); + } + }; + + return ( +
+

+ Update farm +

+ +
+
+
+ + setFarmName(e.target.value)} + className="shadow-xs bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white" + required + /> +
+
+ + setLocation(e.target.value)} + className="shadow-xs bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white" + required + /> +
+
+ + setWaterContent(e.target.value)} + className="shadow-xs bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white" + required + /> +
+
+ + setSoilType(e.target.value)} + className="shadow-xs bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white" + required + /> +
+ + {error &&

{error}

} + {success && ( +

+ Farm Updated successfully! +

+ )} +
+
+
+ ); +}; + +export default UpdateFarm;