mirror of
https://github.com/acedanger/finance.git
synced 2025-12-05 22:50:12 -08:00
refactor: Reorganize imports and improve code consistency across components and API routes
This commit is contained in:
@@ -1,18 +1,16 @@
|
||||
import React, { useState, useEffect } from 'react';
|
||||
import { useStore } from '@nanostores/react';
|
||||
import type { Transaction } from '../types';
|
||||
import type React from 'react';
|
||||
import { useEffect, useState } from 'react';
|
||||
// Import store atoms and actions
|
||||
import {
|
||||
currentAccountId as currentAccountIdStore,
|
||||
transactionToEdit as transactionToEditStore,
|
||||
cancelEditingTransaction,
|
||||
currentAccountId as currentAccountIdStore,
|
||||
transactionSaved,
|
||||
transactionToEdit as transactionToEditStore,
|
||||
} from '../stores/transactionStore';
|
||||
import type { Transaction } from '../types';
|
||||
|
||||
// Remove props that now come from the store
|
||||
interface AddTransactionFormProps {}
|
||||
|
||||
export default function AddTransactionForm({}: AddTransactionFormProps) {
|
||||
export default function AddTransactionForm() {
|
||||
// --- Read state from store ---
|
||||
const currentAccountId = useStore(currentAccountIdStore);
|
||||
const transactionToEdit = useStore(transactionToEditStore);
|
||||
@@ -44,7 +42,7 @@ export default function AddTransactionForm({}: AddTransactionFormProps) {
|
||||
try {
|
||||
const dateObj = new Date(transactionToEdit.date);
|
||||
// Check if date is valid before formatting
|
||||
if (!isNaN(dateObj.getTime())) {
|
||||
if (!Number.isNaN(dateObj.getTime())) {
|
||||
// Directly format the date object (usually interpreted as UTC midnight)
|
||||
// into the YYYY-MM-DD format required by the input.
|
||||
// No timezone adjustment needed here.
|
||||
@@ -87,8 +85,8 @@ export default function AddTransactionForm({}: AddTransactionFormProps) {
|
||||
if (!amount) {
|
||||
errors.push('Amount is required');
|
||||
} else {
|
||||
const amountNum = parseFloat(amount);
|
||||
if (isNaN(amountNum)) {
|
||||
const amountNum = Number.parseFloat(amount);
|
||||
if (Number.isNaN(amountNum)) {
|
||||
errors.push('Amount must be a valid number');
|
||||
} else if (amountNum === 0) {
|
||||
errors.push('Amount cannot be zero');
|
||||
@@ -98,8 +96,8 @@ export default function AddTransactionForm({}: AddTransactionFormProps) {
|
||||
errors.push('Date is required');
|
||||
} else {
|
||||
try {
|
||||
const dateObj = new Date(date + 'T00:00:00'); // Treat input as local date
|
||||
if (isNaN(dateObj.getTime())) {
|
||||
const dateObj = new Date(`${date}T00:00:00`); // Treat input as local date
|
||||
if (Number.isNaN(dateObj.getTime())) {
|
||||
errors.push('Invalid date format');
|
||||
}
|
||||
} catch (e) {
|
||||
@@ -134,7 +132,7 @@ export default function AddTransactionForm({}: AddTransactionFormProps) {
|
||||
accountId: currentAccountId,
|
||||
date: date, // Send as YYYY-MM-DD string
|
||||
description: description.trim(),
|
||||
amount: parseFloat(amount),
|
||||
amount: Number.parseFloat(amount),
|
||||
};
|
||||
|
||||
const method = editingId ? 'PUT' : 'POST';
|
||||
|
||||
Reference in New Issue
Block a user