import { useState } from 'react'; import { type Category, type Transaction, createTransaction, createCategory } from '../api'; export default function ManualManagement({ categories, onTransactionAdded, onCategoryCreated, }: { categories: Category[]; onTransactionAdded: (t: Transaction) => void; onCategoryCreated: (c: Category) => void; }) { // New transaction form state const [amount, setAmount] = useState(''); const [description, setDescription] = useState(''); const [selectedCategoryId, setSelectedCategoryId] = useState(''); const [txDate, setTxDate] = useState(''); // Category creation form const [newCatName, setNewCatName] = useState(''); const [newCatDesc, setNewCatDesc] = useState(''); async function handleCreate(e: React.FormEvent) { e.preventDefault(); if (!amount) return; const payload = { amount: Number(amount), description: description || undefined, category_ids: selectedCategoryId !== '' ? [Number(selectedCategoryId)] : undefined, date: txDate || undefined, }; try { const created = await createTransaction(payload); onTransactionAdded(created); setAmount(''); setDescription(''); setSelectedCategoryId(''); setTxDate(''); } catch (err: any) { alert(err?.message || 'Failed to create transaction'); } } async function handleCreateCategory(e: React.FormEvent) { e.preventDefault(); if (!newCatName.trim()) return; try { const cat = await createCategory({ name: newCatName.trim(), description: newCatDesc || undefined }); onCategoryCreated(cat); setNewCatName(''); setNewCatDesc(''); } catch (err: any) { alert(err?.message || 'Failed to create category'); } } return ( <>

Add Transaction

setAmount(e.target.value)} required /> setTxDate(e.target.value)} /> setDescription(e.target.value)} />

Categories

setNewCatName(e.target.value)} /> setNewCatDesc(e.target.value)} />
); }