From 92d929c8e18d6eff7e709622f11270ea14626d4f Mon Sep 17 00:00:00 2001 From: Manuel Bustillo Date: Sun, 17 Nov 2024 16:41:53 +0100 Subject: [PATCH] Extract expenses API --- app/api/expenses.tsx | 38 ++++++++++++++++ app/ui/expenses/table.tsx | 92 ++++++++++++++------------------------- 2 files changed, 70 insertions(+), 60 deletions(-) create mode 100644 app/api/expenses.tsx diff --git a/app/api/expenses.tsx b/app/api/expenses.tsx new file mode 100644 index 0000000..7cfb3a3 --- /dev/null +++ b/app/api/expenses.tsx @@ -0,0 +1,38 @@ +import { Expense } from '@/app/lib/definitions'; +import { getCsrfToken } from '@/app/lib/utils'; + +export function loadExpenses(onLoad?: (expenses: Expense[]) => void) { + fetch("/api/expenses") + .then((response) => response.json()) + .then((data) => { + onLoad && onLoad(data.map((record: any) => { + return ({ + id: record.id, + name: record.name, + amount: record.amount, + pricingType: record.pricing_type + }); + })); + }, (error) => { + return []; + }); +} + +export function updateExpense(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)); +} \ No newline at end of file diff --git a/app/ui/expenses/table.tsx b/app/ui/expenses/table.tsx index 5161cfe..121990c 100644 --- a/app/ui/expenses/table.tsx +++ b/app/ui/expenses/table.tsx @@ -2,71 +2,43 @@ 'use client' -import React, { useState } from "react" +import { loadExpenses, updateExpense } from '@/app/api/expenses'; import { Expense } from '@/app/lib/definitions'; -import TableOfContents from "../components/table-of-contents"; +import { useState } from "react"; import InlineTextField from "../components/form/inlineTextField"; -import { getCsrfToken } from '@/app/lib/utils'; +import TableOfContents from "../components/table-of-contents"; export default function ExpensesTable() { - const [expenses, setExpenses] = useState>([]); + const [expenses, setExpenses] = useState>([]); + const [expensesLoaded, setExpensesLoaded] = useState(false); - 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 refreshExpenses() { + loadExpenses((expenses) => { + setExpenses(expenses); + setExpensesLoaded(true); + }); + } - 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 []; - }); - } + !expensesLoaded && refreshExpenses(); - expenses.length === 0 && loadExpenses(); - - return ( - ( - - - { expense.name = value; handleExpenseUpdate(expense) }} /> - - - { expense.amount = parseFloat(value); handleExpenseUpdate(expense) }} /> - - - {expense.pricingType} - - - )} - /> - ); + return ( + ( + + + { expense.name = value; updateExpense(expense) }} /> + + + { expense.amount = parseFloat(value); updateExpense(expense) }} /> + + + {expense.pricingType} + + + )} + /> + ); } \ No newline at end of file