feat: enhance form validation in AddTransactionForm and improve error handling in transactions API

This commit is contained in:
GitHub Copilot
2025-04-24 12:11:23 -04:00
parent f67980a35e
commit e7934a5a9c
3 changed files with 63 additions and 39 deletions

View File

@@ -153,7 +153,7 @@
// Check required fields
const description = formData.get('description') as string;
const amount = formData.get('amount');
const amount = formData.get('amount') as string;
const date = formData.get('date');
if (!description || description.trim().length < 2) {
@@ -162,10 +162,22 @@
if (!amount) {
errors.push('Amount is required');
} else {
const amountNum = parseFloat(amount);
if (isNaN(amountNum)) {
errors.push('Amount must be a valid number');
} else if (amountNum === 0) {
errors.push('Amount cannot be zero');
}
}
if (!date) {
errors.push('Date is required');
} else {
const dateObj = new Date(date as string);
if (isNaN(dateObj.getTime())) {
errors.push('Invalid date format');
}
}
return errors.length > 0 ? errors : null;
@@ -226,7 +238,7 @@
amount: amount
};
const transactionId = formData.get('id');
const transactionId = txnIdInput.value;
const method = transactionId ? 'PUT' : 'POST';
const url = transactionId
? `/api/transactions/${transactionId}`
@@ -250,14 +262,17 @@
// Update UI
const eventName = isEditMode ? 'transactionUpdated' : 'transactionCreated';
const event = new CustomEvent(eventName, {
document.dispatchEvent(new CustomEvent(eventName, {
detail: { transaction: savedTransaction }
});
document.dispatchEvent(event);
}));
// Clear and collapse form
clearForm();
form.classList.add('collapsed');
if (toggleBtn) {
toggleBtn.setAttribute('aria-expanded', 'false');
toggleBtn.textContent = 'Add Transaction +';
}
} catch (error) {
showError(error instanceof Error ? error.message : 'An unexpected error occurred');