diff --git a/Frontend/src/components/TotalSpent.jsx b/Frontend/src/components/TotalSpent.jsx index a267b71..eb8828c 100644 --- a/Frontend/src/components/TotalSpent.jsx +++ b/Frontend/src/components/TotalSpent.jsx @@ -1,4 +1,28 @@ +import { useEffect, useState } from "react"; +import { useGetFarmsQuery } from "../store/api/farmApi"; + +const calculateSpend = (farmList) => { + let totalSpend = 0; + for (let i = 0; i < farmList.length; i++) { + if (!farmList[i]) continue; + if (!farmList[i]?.finances) continue; + if (!farmList[i]?.finances?.totalExpenses) continue; + totalSpend += farmList[i]?.finances?.totalExpenses; + } + return totalSpend; +}; + const TotalSpent = () => { + const [totalSpend, setTotalSpend] = useState(0); + const { data: farmList, isLoading, error } = useGetFarmsQuery(); + + useEffect(() => { + if (!isLoading && !error && farmList) { + setTotalSpend(calculateSpend(farmList)); + } + }, [farmList]); + + console.log("My farm list is : ", farmList); return (