From 94fa49d8529c5e1c36ad971702fe04cd0489a333 Mon Sep 17 00:00:00 2001 From: LiamB94 <126518915+LiamB94@users.noreply.github.com> Date: Mon, 19 Aug 2024 12:26:14 +1200 Subject: [PATCH] Financial metrics implementation (#111) * financial metric button added with no functionality * encapsulating all changes for original framework * implementation added on metrics, untested --- frontend/src/components/FinancialMetrics.jsx | 51 +++++++++++++++++++ .../context/TransactionContextProvider.jsx | 33 ++++++++++++ frontend/src/pages/Dashboard.jsx | 7 ++- 3 files changed, 87 insertions(+), 4 deletions(-) create mode 100644 frontend/src/components/FinancialMetrics.jsx diff --git a/frontend/src/components/FinancialMetrics.jsx b/frontend/src/components/FinancialMetrics.jsx new file mode 100644 index 0000000..b52b646 --- /dev/null +++ b/frontend/src/components/FinancialMetrics.jsx @@ -0,0 +1,51 @@ +import React, { useState, useContext } from 'react'; +import TransactionContext from '../context/TransactionContext'; + +export default function FinancialMetrics() { + const [showMetrics, setShowMetrics] = useState(false); + const { balance, currency, convertCurrency, transactions, monthlyMetrics } = useContext(TransactionContext); + const [convertedBalance, setConvertedBalance] = useState(0); + + // Function to toggle the visibility of the modal + const handleClick = () => { + setShowMetrics(!showMetrics); + }; + + // Calculate and set the converted balance when currency or balance changes + React.useEffect(() => { + const updateConvertedBalance = async () => { + const converted = await convertCurrency(currency, 'NZD', balance); // Convert balance to current currency + setConvertedBalance(converted); + }; + updateConvertedBalance(); + }, [currency, balance, convertCurrency]); + + return ( +
+ + + {/* Conditionally render the modal */} + {showMetrics && ( +
+
+

Financial Metrics

+

Current Balance: {convertedBalance} {currency}

+

Spending per Month: {monthlyMetrics.monthlySpending} {currency}

+

Income per Month: {monthlyMetrics.monthlyIncome} {currency}

+

% Income Spent: {monthlyMetrics.percentageSpent.toFixed(2)}%

+

% Income Saved: {monthlyMetrics.percentageSaved.toFixed(2)}%

+ +
+
+ )} +
+ ); +} diff --git a/frontend/src/context/TransactionContextProvider.jsx b/frontend/src/context/TransactionContextProvider.jsx index 5da722e..d862522 100644 --- a/frontend/src/context/TransactionContextProvider.jsx +++ b/frontend/src/context/TransactionContextProvider.jsx @@ -126,6 +126,38 @@ export function TransactionContextProvider({ children }) { } }; + // calculates metrics for the user + const calculateMetrics = (transactions) => { + const now = new Date(); + const currentMonth = now.getMonth(); + const currentYear = now.getFullYear(); + + let monthlySpending = 0; + let monthlyIncome = 0; + + transactions.forEach(transaction => { + const transactionDate = new Date(transaction.date); + const isCurrentMonth = transactionDate.getMonth() === currentMonth && transactionDate.getFullYear() === currentYear; + if (isCurrentMonth) { + if (transaction.amount < 0) { + monthlySpending += Math.abs(transaction.amount); // Spending is negative + } else { + monthlyIncome += transaction.amount; // Income is positive + } + } + }); + + const percentageSpent = monthlyIncome > 0 ? (monthlySpending / monthlyIncome) * 100 : 0; + const percentageSaved = 100 - percentageSpent; + + return { + monthlySpending, + monthlyIncome, + percentageSpent, + percentageSaved, + }; + }; + //function for handling the selection of transactions for deletion const handleSelect = (transactionId, isSelected) => { setSelectedTransactions((prev) => @@ -154,6 +186,7 @@ export function TransactionContextProvider({ children }) { convertCurrency, // returns a promise that resolves to the converted amount handleSelect, // function to handle the selection of transactions requestUiUpdate, // call this function to request a UI update of the transactions if it is not done automatically + monthlyMetrics: calculateMetrics(transactions) // }; return ( diff --git a/frontend/src/pages/Dashboard.jsx b/frontend/src/pages/Dashboard.jsx index 9a32dc3..2c1e0df 100644 --- a/frontend/src/pages/Dashboard.jsx +++ b/frontend/src/pages/Dashboard.jsx @@ -9,6 +9,7 @@ import SavingsTracker from "../components/SavingsTracker"; import Banner from "../components/Banner"; import FintrackLogo from "../assets/images/FintrackLogo.png"; +import FinancialMetrics from "../components/FinancialMetrics"; /* * When adding your new component: @@ -21,7 +22,6 @@ import FintrackLogo from "../assets/images/FintrackLogo.png"; export default function Dashboard() { return ( <> -
@@ -40,6 +40,7 @@ export default function Dashboard() {
+
@@ -48,9 +49,7 @@ export default function Dashboard() { -
-

INCOME STATS

-
+ );