diff --git a/frontend/src/components/Transaction.jsx b/frontend/src/components/Transaction.jsx new file mode 100644 index 0000000..b3cb3a6 --- /dev/null +++ b/frontend/src/components/Transaction.jsx @@ -0,0 +1,27 @@ +import React, { useEffect, useState } from 'react'; + +const Transaction = ({ transaction }) => { + const [isAmountNegative, setIsAmountNegative] = useState(); + + useEffect(() => { + setIsAmountNegative(transaction.amount < 0); + }, [transaction.amount]); + + return ( +
+ {isAmountNegative ? ( +
+

{transaction.amount}: {transaction.description}

+

{transaction.created_at.substring(0, 10)}

+
+ ) : ( +
+

+{transaction.amount}: {transaction.description}

+

{transaction.created_at.substring(0, 10)}

+
+ )} +
+ ); +}; + +export default Transaction; diff --git a/frontend/src/components/TransactionLog.jsx b/frontend/src/components/TransactionLog.jsx new file mode 100644 index 0000000..87f142c --- /dev/null +++ b/frontend/src/components/TransactionLog.jsx @@ -0,0 +1,112 @@ +import { useEffect, useState } from 'react'; +import axios from 'axios'; +import Transaction from './Transaction'; +import { IoIosArrowForward, IoIosArrowBack } from "react-icons/io"; +import { filterPastWeekTransactions, filterPastMonthTransactions, filterPastYearTransactions } from '../utility/transactionFilters.js'; + +export default function TransactionList() { + const [transactions, setTransactions] = useState([]); + const [currentPage, setCurrentPage] = useState(1); + const [filter, setFilter] = useState(''); + + useEffect(() => { + axios + .get(`http://localhost:4000/transaction/page/${currentPage}`) + .then((response) => { + // setTransactions(response.data.result); + let fetchedTransactions = response.data.result; + + if (filter === 'year') { + fetchedTransactions = filterPastYearTransactions(fetchedTransactions); + } + else if (filter === 'month') { + fetchedTransactions = filterPastMonthTransactions(fetchedTransactions); + } + else if (filter === 'week') { + fetchedTransactions = filterPastWeekTransactions(fetchedTransactions); + } + setTransactions(fetchedTransactions); + }) + .catch((error) => { + console.error('Not logged in'); + window.location.href = '/login'; + }); + }, [currentPage], [filter]); + + //function to create dummy transcation + const createTransaction = async () => { + try { + await axios.post('http://localhost:4000/transaction', { + title: 'tes2t', + amount: -22, + description: 'beer', + }); + setCurrentPage(1); + } catch (error) { + console.error('Error creating transaction:', error); + } + }; + + //function to filter transactions + const filterYear = () => { + if (filter === 'year') { + setFilter(''); + } else { + setFilter('year'); + } + }; + + const filterMonth = () => { + if (filter === 'month') { + setFilter(''); + } else { + setFilter('month'); + } + }; + + const filterWeek = () => { + if (filter === 'week') { + setFilter(''); + } else { + setFilter('week'); + } + }; + + + return ( +
+ +
+
+

Transaction Log

+ + + + +
+
+ +
+
    + {transactions.map((transaction) => ( + + ))} +
+
+ +
+ +

{currentPage}

+ +
+ +
+
+ +
+ ); +} diff --git a/frontend/src/pages/Dashboard.jsx b/frontend/src/pages/Dashboard.jsx index 41913bd..11895cf 100644 --- a/frontend/src/pages/Dashboard.jsx +++ b/frontend/src/pages/Dashboard.jsx @@ -1,9 +1,11 @@ -import CurrentBalance from "../components/CurrentBalance"; +import CurrentBalance from '../components/CurrentBalance'; +import TransactionLog from '../components/TransactionLog'; export default function Dashboard() { return (
+
); -} \ No newline at end of file +}