2024-11-10 21:08:03 +01:00
|
|
|
/* Copyright (C) 2024 Manuel Bustillo*/
|
|
|
|
|
|
|
|
'use client'
|
|
|
|
|
2024-11-17 16:41:53 +01:00
|
|
|
import { loadExpenses, updateExpense } from '@/app/api/expenses';
|
2024-11-10 21:08:03 +01:00
|
|
|
import { Expense } from '@/app/lib/definitions';
|
2024-11-17 16:41:53 +01:00
|
|
|
import { useState } from "react";
|
2024-11-11 08:10:33 +01:00
|
|
|
import InlineTextField from "../components/form/inlineTextField";
|
2024-11-17 16:41:53 +01:00
|
|
|
import TableOfContents from "../components/table-of-contents";
|
2024-11-10 21:08:03 +01:00
|
|
|
|
|
|
|
export default function ExpensesTable() {
|
2024-11-17 16:41:53 +01:00
|
|
|
const [expenses, setExpenses] = useState<Array<Expense>>([]);
|
|
|
|
const [expensesLoaded, setExpensesLoaded] = useState(false);
|
2024-11-11 08:10:33 +01:00
|
|
|
|
2024-11-17 16:41:53 +01:00
|
|
|
function refreshExpenses() {
|
|
|
|
loadExpenses((expenses) => {
|
|
|
|
setExpenses(expenses);
|
|
|
|
setExpensesLoaded(true);
|
|
|
|
});
|
|
|
|
}
|
2024-11-10 21:08:03 +01:00
|
|
|
|
2024-11-17 16:41:53 +01:00
|
|
|
!expensesLoaded && refreshExpenses();
|
2024-11-10 21:08:03 +01:00
|
|
|
|
2024-11-17 16:41:53 +01:00
|
|
|
return (
|
|
|
|
<TableOfContents
|
|
|
|
headers={['Name', 'Amount (€)', 'Pricing Type']}
|
|
|
|
caption='Expenses'
|
|
|
|
elements={expenses}
|
|
|
|
rowRender={(expense) => (
|
|
|
|
<tr key={expense.id} className="bg-white border-b odd:bg-white odd:dark:bg-gray-900 even:bg-gray-50 even:dark:bg-gray-800">
|
|
|
|
<th scope="row" className="px-6 py-4 font-medium text-gray-900 whitespace-nowrap dark:text-white">
|
|
|
|
<InlineTextField initialValue={expense.name} onChange={(value) => { expense.name = value; updateExpense(expense) }} />
|
|
|
|
</th>
|
|
|
|
<td className="px-6 py-4">
|
|
|
|
<InlineTextField initialValue={expense.amount.toString()} onChange={(value) => { expense.amount = parseFloat(value); updateExpense(expense) }} />
|
|
|
|
</td>
|
|
|
|
<td>
|
|
|
|
{expense.pricingType}
|
|
|
|
</td>
|
|
|
|
</tr>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
);
|
2024-11-10 21:08:03 +01:00
|
|
|
}
|