2024-11-17 16:00:30 +00:00
|
|
|
/* Copyright (C) 2024 Manuel Bustillo*/
|
|
|
|
|
2024-11-17 16:41:53 +01:00
|
|
|
import { Expense } from '@/app/lib/definitions';
|
2024-12-01 18:02:25 +01:00
|
|
|
import { getCsrfToken, getSlug } from '@/app/lib/utils';
|
2024-11-17 16:41:53 +01:00
|
|
|
|
|
|
|
export function loadExpenses(onLoad?: (expenses: Expense[]) => void) {
|
2024-12-01 18:02:25 +01:00
|
|
|
fetch(`/api/${getSlug()}/expenses`)
|
2024-11-17 16:41:53 +01:00
|
|
|
.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) {
|
2024-12-01 18:02:25 +01:00
|
|
|
fetch(`/api/${getSlug()}/expenses/${expense.id}`,
|
2024-11-17 16:41:53 +01:00
|
|
|
{
|
|
|
|
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));
|
|
|
|
}
|