diff --git a/Frontend/src/pages/UserPanel/Farm/CropTable.jsx b/Frontend/src/pages/UserPanel/Farm/CropTable.jsx new file mode 100644 index 0000000..a9fc8f8 --- /dev/null +++ b/Frontend/src/pages/UserPanel/Farm/CropTable.jsx @@ -0,0 +1,152 @@ +import React, { useState, useEffect } from "react"; + +const CropTable = ({ farmId }) => { + const [crops, setCrops] = useState([]); + const [loading, setLoading] = useState(true); + const [error, setError] = useState(null); + useEffect(() => { + const fetchCrops = async () => { + try { + const response = await fetch( + `http://localhost:8000/api/v1/crop/farm/${farmId}`, + { + credentials: "include", + headers: { + "Content-Type": "application/json", + }, + } + ); + + if (!response.ok) { + throw new Error("Failed to fetch crops"); + } + + const data = await response.json(); + setCrops(data || []); + } catch (err) { + setError(err.message); + } finally { + setLoading(false); + } + }; + + fetchCrops(); + }, [farmId]); + + if (loading) { + return ( +
+ Loading crops... +
+ ); + } + + if (error) { + return ( +
+

Error: {error}

+
+ ); + } + + if (crops.length === 0) { + return ( +
+

No crops found

+

+ Start by adding some crops to your farm +

+
+ ); + } + + return ( +
+
+

Crops Table

+
+
+ + + + + + + + + + + + {crops.map((crop) => ( + + + + + + + + ))} + +
+ Image + + Name + + Growth Stage + + Health Status + + Harvest Date +
+
+ {crop.image ? ( + {crop.name} + ) : ( +
+ No image +
+ )} +
+
+
+ {crop.name} +
+
+ + {crop.growthStage} + + +
+ {crop.healthStatus} +
+
+
+ {new Date(crop.harvestDate).toLocaleDateString()} +
+
+
+
+ ); +}; + +export default CropTable;