@@ -165,6 +158,8 @@
class="btn btn-primary"
onclick={() => {
if (currentTransaction.budget_id) {
+ loading = true;
+ my_modal_3.close();
fetch(`/api/budget/${currentTransaction.budget_id}/transaction`, {
method: 'POST',
headers: {
@@ -176,10 +171,14 @@
notes: currentTransaction.notes
})
}).then((res) => {
+ loading = false;
+ invalidateAll();
if (res.ok) {
// Optionally, you can refresh the UI or show a success message
+ addToast('Transaction added to budget', 'success');
console.log('Transaction added to budget successfully');
} else {
+ addToast('Failed to add transaction to budget', 'error');
console.error('Failed to add transaction to budget');
}
});
@@ -218,3 +217,7 @@
+
+{#if loading}
+ {@render loadingModal()}
+{/if}
diff --git a/src/routes/api/budget/+server.js b/src/routes/api/budget/+server.js
new file mode 100644
index 0000000..dd3e02c
--- /dev/null
+++ b/src/routes/api/budget/+server.js
@@ -0,0 +1,18 @@
+import { json } from '@sveltejs/kit';
+import { addBudget, deleteBudget } from '$lib/db';
+
+// In-memory store for demonstration (replace with a database in production)
+let budgets = [];
+
+/** @type {import('./$types').RequestHandler} */
+export async function POST({ request }) {
+ const data = await request.json();
+
+ // Basic validation
+ if (!data.name || typeof data.amount !== 'number') {
+ return json({ error: 'Invalid input' }, { status: 400 });
+ }
+ let res = await addBudget(data.name, data.amount, data.notes);
+
+ return json(res, { status: 201 });
+}
diff --git a/src/routes/budget/+server.js b/src/routes/budget/+server.js
deleted file mode 100644
index 9b33ad8..0000000
--- a/src/routes/budget/+server.js
+++ /dev/null
@@ -1,18 +0,0 @@
-import { json } from '@sveltejs/kit';
-import { addBudget, deleteBudget } from '$lib/db';
-
-// In-memory store for demonstration (replace with a database in production)
-let budgets = [];
-
-/** @type {import('./$types').RequestHandler} */
-export async function POST({ request }) {
- const data = await request.json();
-
- // Basic validation
- if (!data.name || typeof data.amount !== 'number') {
- return json({ error: 'Invalid input' }, { status: 400 });
- }
- let res = await addBudget(data.name, data.amount, data.notes);
-
- return json(res, { status: 201 });
-}
diff --git a/src/routes/settings/+page.svelte b/src/routes/settings/+page.svelte
index 5a322a9..d73ca76 100644
--- a/src/routes/settings/+page.svelte
+++ b/src/routes/settings/+page.svelte
@@ -15,7 +15,7 @@
async function saveBudget() {
loading = true;
- let res = await fetch('/budget', {
+ let res = await fetch('/api/budget', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
@@ -36,14 +36,6 @@
loading = false;
}
- async function update() {
- loading = true;
- let res = await fetch('/api/simplefin/update', {
- method: 'POST'
- });
- loading = false;
- }
-
async function deleteBudget(name) {
loading = true;
if (name === toDeleteBudget.name) {
@@ -69,8 +61,6 @@
{@render loadingModal()}
{/if}
-
-