/* Copyright (C) 2024 Manuel Bustillo*/ 'use client' import React, { useState } from "react" import { Expense } from '@/app/lib/definitions'; import TableOfContents from "../components/table-of-contents"; import InlineTextField from "../components/form/inlineTextField"; import { getCsrfToken } from '@/app/lib/utils'; export default function ExpensesTable() { const [expenses, setExpenses] = useState>([]); const handleExpenseUpdate = (expense: Expense) => { fetch(`/api/expenses/${expense.id}`, { method: 'PUT', body: JSON.stringify({ expense: { name: expense.name, amount: expense.amount, pricing_type: expense.pricingType, } }), headers: { 'Content-Type': 'application/json', 'X-CSRF-TOKEN': getCsrfToken(), } }) .catch((error) => console.error(error)); } function loadExpenses() { fetch("/api/expenses") .then((response) => response.json()) .then((data) => { setExpenses(data.map((record: any) => { return ({ id: record.id, name: record.name, amount: record.amount, pricingType: record.pricing_type }); })); }, (error) => { return []; }); } expenses.length === 0 && loadExpenses(); return ( ( { expense.name = value; handleExpenseUpdate(expense) }} /> { expense.amount = parseFloat(value); handleExpenseUpdate(expense) }} /> {expense.pricingType} )} /> ); }