2024-10-27 21:11:45 +00:00
|
|
|
/* Copyright (C) 2024 Manuel Bustillo*/
|
|
|
|
|
2024-12-09 00:36:42 +01:00
|
|
|
'use client'
|
|
|
|
|
|
|
|
import { AbstractApi } from '@/app/api/abstract-api';
|
|
|
|
import { Expense, ExpenseSerializer } from '@/app/lib/expense';
|
|
|
|
import { classNames } from '@/app/ui/components/button';
|
|
|
|
import ExpenseFormDialog from '@/app/ui/components/expense-form-dialog';
|
2024-11-10 21:08:03 +01:00
|
|
|
import ExpensesTable from '@/app/ui/expenses/table';
|
2024-12-09 00:36:42 +01:00
|
|
|
import SkeletonTable from '@/app/ui/guests/skeleton-row';
|
|
|
|
import { Suspense, useEffect, useState } from 'react';
|
|
|
|
|
|
|
|
export default function Page() {
|
|
|
|
const refreshExpenses = () => {
|
|
|
|
new AbstractApi<Expense>().getAll(new ExpenseSerializer(), (expenses: Expense[]) => {
|
|
|
|
setExpenses(expenses);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const [expenses, setExpenses] = useState<Expense[]>([]);
|
|
|
|
const [expenseBeingEdited, setExpenseBeingEdited] = useState<Expense | undefined>(undefined);
|
|
|
|
useEffect(() => { refreshExpenses() }, []);
|
|
|
|
|
2024-08-11 12:34:16 +02:00
|
|
|
return (
|
|
|
|
<div className="w-full">
|
2024-12-09 00:36:42 +01:00
|
|
|
<div className="flex flex-col w-full items-center justify-between">
|
|
|
|
<button onClick={() => setExpenseBeingEdited({})} className={classNames('primary')}>Add new</button>
|
|
|
|
<ExpenseFormDialog
|
|
|
|
key={expenseBeingEdited?.id}
|
|
|
|
onCreate={() => { refreshExpenses(); setExpenseBeingEdited(undefined) }}
|
|
|
|
expense={expenseBeingEdited}
|
|
|
|
visible={expenseBeingEdited !== undefined}
|
|
|
|
onHide={() => { setExpenseBeingEdited(undefined) }}
|
|
|
|
/>
|
|
|
|
<Suspense fallback={<SkeletonTable />}>
|
|
|
|
<ExpensesTable
|
|
|
|
expenses={expenses}
|
|
|
|
onUpdate={refreshExpenses}
|
|
|
|
onEdit={(expense) => setExpenseBeingEdited(expense)}
|
|
|
|
/>
|
|
|
|
</Suspense>
|
2024-08-11 12:34:16 +02:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|