mirror of
https://github.com/acedanger/finance.git
synced 2025-12-05 22:50:12 -08:00
Fix: Update button remaining disabled in transaction edit mode
This commit resolves an issue where the Update button in the transaction form would remain disabled when attempting to edit a transaction. The problem was in how the transactionStore was managing state updates during transaction editing. Key changes: - Enhanced startEditingTransaction function in transactionStore.ts to ensure proper reactivity - Added clean copy creation of transaction objects to avoid reference issues - Implemented a state update cycle with null value first to force reactivity - Added a small timeout to ensure state changes are properly detected by components The Transaction form now correctly enables the Update button when in edit mode, regardless of account selection state.
This commit is contained in:
@@ -11,7 +11,7 @@
|
||||
*/
|
||||
|
||||
import type { APIRoute } from 'astro';
|
||||
import { accounts, transactions } from '../../../data/store';
|
||||
import { accountService, transactionService } from '../../../data/db.service';
|
||||
import type { Transaction } from '../../../types';
|
||||
|
||||
/**
|
||||
@@ -43,7 +43,7 @@ export const POST: APIRoute = async ({ request }) => {
|
||||
}
|
||||
|
||||
// Validate account exists
|
||||
const account = accounts.find((a) => a.id === transaction.accountId);
|
||||
const account = await accountService.getById(transaction.accountId);
|
||||
if (!account) {
|
||||
return new Response(JSON.stringify({ error: 'Account not found' }), {
|
||||
status: 404,
|
||||
@@ -51,25 +51,32 @@ export const POST: APIRoute = async ({ request }) => {
|
||||
});
|
||||
}
|
||||
|
||||
// Create new transaction with generated ID
|
||||
const newTransaction: Transaction = {
|
||||
...transaction,
|
||||
id: (transactions.length + 1).toString(), // Simple ID generation for demo
|
||||
};
|
||||
// Convert string date to Date object if needed
|
||||
const transactionDate =
|
||||
typeof transaction.date === 'string' ? new Date(transaction.date) : transaction.date;
|
||||
|
||||
// Update account balance
|
||||
account.balance += transaction.amount;
|
||||
|
||||
// Add to transactions array
|
||||
transactions.push(newTransaction);
|
||||
// Create new transaction with database service
|
||||
// The database service will also update the account balance
|
||||
const newTransaction = await transactionService.create({
|
||||
accountId: transaction.accountId,
|
||||
date: transactionDate,
|
||||
description: transaction.description,
|
||||
amount: transaction.amount,
|
||||
category: transaction.category,
|
||||
status: transaction.status as any,
|
||||
type: transaction.type as any,
|
||||
notes: transaction.notes,
|
||||
tags: transaction.tags,
|
||||
});
|
||||
|
||||
return new Response(JSON.stringify(newTransaction), {
|
||||
status: 201,
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
});
|
||||
} catch (error) {
|
||||
return new Response(JSON.stringify({ error: 'Invalid request body' }), {
|
||||
status: 400,
|
||||
console.error('Error creating transaction:', error);
|
||||
return new Response(JSON.stringify({ error: 'Failed to create transaction' }), {
|
||||
status: 500,
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user