diff --git a/frontend/src/component/TransactionForm.tsx b/frontend/src/component/TransactionForm.tsx new file mode 100644 index 0000000..c661acf --- /dev/null +++ b/frontend/src/component/TransactionForm.tsx @@ -0,0 +1,50 @@ +import React, { useState } from 'react'; +import axios from 'axios'; +import { useAuth } from '../context/AuthContext'; +import { useTranslation } from 'react-i18next'; + +const TransactionForm: React.FC = () => { + const [amount, setAmount] = useState(0); + const [type, setType] = useState(''); + const [currency, setCurrency] = useState(''); + const [category, setCategory] = useState(''); + const [desc, setDesc] = useState(''); + const [date, setDate] = useState(''); + const { token } = useAuth(); + const { t } = useTranslation(); + + const handleAddTransaction = async (e: React.FormEvent) => { + e.preventDefault(); + try { + const headers = { Authorization: `${token}` }; + await axios.post('http://localhost:3000/api/transactions', { type, amount, currency, category, desc, date }, { headers }); + setAmount(0); + setType(''); + setCurrency(''); + setCategory(''); + setDesc(''); + setDate(''); + window.location.reload(); + } catch (error) { + console.error('Error adding transaction:', error); + } + }; + + return ( +
+ + setAmount(Number(e.target.value))} required /> + setCurrency(e.target.value)} required /> + setCategory(e.target.value)} required /> + setDesc(e.target.value)} required /> + setDate(e.target.value)} required /> + +
+ ); +}; + +export default TransactionForm; diff --git a/frontend/src/component/TransactionHistory.tsx b/frontend/src/component/TransactionHistory.tsx new file mode 100644 index 0000000..f91f586 --- /dev/null +++ b/frontend/src/component/TransactionHistory.tsx @@ -0,0 +1,131 @@ +import React, { useState } from 'react'; +import axios from 'axios'; +import { useAuth } from '../context/AuthContext'; +import { useTranslation } from 'react-i18next'; + +interface Transaction { + id: number; + type: string; + amount: number; + currency: string; + category: string; + desc: string; + date: string; +} + +const TransactionHistory: React.FC<{ transactions: Transaction[] }> = ({ transactions }) => { + const { token } = useAuth(); + const [editingTransaction, setEditingTransaction] = useState(null); + const [updatedType, setUpdatedType] = useState(''); + const [updatedAmount, setUpdatedAmount] = useState(0); + const [updatedCurrency, setUpdatedCurrency] = useState(''); + const [updatedCategory, setUpdatedCategory] = useState(''); + const [updatedDesc, setUpdatedDesc] = useState(''); + const [updatedDate, setUpdatedDate] = useState(''); + const { t } = useTranslation(); + + const handleDelete = async (id: number) => { + try { + const headers = { Authorization: `${token}` }; + await axios.delete(`http://localhost:3000/api/transactions/${id}`, { headers }); + window.location.reload(); + } catch (error) { + console.error('Error deleting transaction:', error); + } + }; + + const handleUpdate = async (id: number) => { + try { + const headers = { Authorization: `${token}` }; + await axios.put( + `http://localhost:3000/api/transactions/${id}`, + { type: updatedType, amount: updatedAmount, currency: updatedCurrency, category: updatedCategory, desc: updatedDesc, date: updatedDate }, + { headers } + ); + setEditingTransaction(null); // Hide the update form + window.location.reload(); + } catch (error) { + console.error('Error updating transaction:', error); + } + }; + + return ( +
+ {transactions.map(transaction => ( +
+

{t('transactionHistory.type')}: {transaction.type}

+

{t('transactionHistory.amount')}: {transaction.amount}

+

{t('transactionHistory.currency')}: {transaction.currency}

+

{t('transactionHistory.category')}: {transaction.category}

+

{t('transactionHistory.desc')}: {transaction.desc}

+

{t('transactionHistory.date')}: {transaction.date}

+ + {editingTransaction?.id === transaction.id && ( +
+
{t('transactionHistory.updateTransaction')}
+ setUpdatedType(e.target.value)} + className="block mb-2 p-2 border border-gray-300 rounded" + /> + setUpdatedAmount(Number(e.target.value))} + className="block mb-2 p-2 border border-gray-300 rounded" + /> + setUpdatedCurrency(e.target.value)} + className="block mb-2 p-2 border border-gray-300 rounded" + /> + setUpdatedCategory(e.target.value)} + className="block mb-2 p-2 border border-gray-300 rounded" + /> + setUpdatedDesc(e.target.value)} + className="block mb-2 p-2 border border-gray-300 rounded" + /> + setUpdatedDate(e.target.value)} + className="block mb-2 p-2 border border-gray-300 rounded" + /> + + +
+ )} + + + +
+ ))} +
+ ); +}; + +export default TransactionHistory;