import { useState, useEffect } from 'react'; import { useNavigate } from 'react-router-dom'; import { Coffee, ShoppingCart, Zap, Home as HomeIcon, Car, Utensils, Briefcase, CreditCard, Calendar, MessageSquare } from 'lucide-react'; import { Button } from '@/components/ui/button'; import { cn } from '@/lib/utils'; import { Popover, PopoverContent, PopoverTrigger } from '@/components/ui/popover'; import { Calendar as CalendarComponent } from '@/components/ui/calendar'; import { toast } from 'sonner'; import { v4 as uuidv4 } from 'uuid'; import database from '@/services/database'; import { Textarea } from '@/components/ui/textarea'; const CATEGORIES = [ { id: 'food', name: 'Food', icon: Utensils, color: 'bg-amber-100 text-amber-600' }, { id: 'coffee', name: 'Coffee', icon: Coffee, color: 'bg-brown-100 text-brown-600' }, { id: 'shopping', name: 'Shopping', icon: ShoppingCart, color: 'bg-indigo-100 text-indigo-600' }, { id: 'utilities', name: 'Utilities', icon: Zap, color: 'bg-yellow-100 text-yellow-600' }, { id: 'housing', name: 'Housing', icon: HomeIcon, color: 'bg-blue-100 text-blue-600' }, { id: 'transport', name: 'Transport', icon: Car, color: 'bg-green-100 text-green-600' }, { id: 'salary', name: 'Salary', icon: Briefcase, color: 'bg-emerald-100 text-emerald-600' }, { id: 'other', name: 'Other', icon: CreditCard, color: 'bg-gray-100 text-gray-600' }, ]; const TransactionForm = () => { const navigate = useNavigate(); const [type, setType] = useState<'expense' | 'income'>('expense'); const [title, setTitle] = useState(''); const [amount, setAmount] = useState(''); const [category, setCategory] = useState(''); const [date, setDate] = useState(new Date()); const [note, setNote] = useState(''); const [message, setMessage] = useState(''); const [isDbInitialized, setIsDbInitialized] = useState(false); useEffect(() => { const initDb = async () => { try { await database.initialize(); setIsDbInitialized(true); } catch (error) { console.error('Failed to initialize database:', error); toast.error('Failed to initialize database'); } }; initDb(); }, []); const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); if (!title || !amount || !category) { toast.error('Please fill in all required fields'); return; } if (!isDbInitialized) { toast.error('Database not initialized yet, please try again'); return; } try { const formattedDate = date.toISOString().split('T')[0]; const id = uuidv4(); database.exec( 'INSERT INTO transactions (id, title, amount, type, category, date, note, message) VALUES (?, ?, ?, ?, ?, ?, ?, ?)', [id, title, parseFloat(amount), type, category, formattedDate, note || null, message || null] ); toast.success('Transaction saved successfully'); navigate('/transactions'); } catch (error) { console.error('Error saving transaction:', error); toast.error('Failed to save transaction'); } }; return (
setTitle(e.target.value)} placeholder="What was this for?" className="w-full p-3 rounded-lg border border-border focus:border-primary/50 focus:ring-2 focus:ring-primary/20 transition-all bg-card" />
$ setAmount(e.target.value)} placeholder="0.00" className="w-full p-3 pl-8 rounded-lg border border-border focus:border-primary/50 focus:ring-2 focus:ring-primary/20 transition-all bg-card" />
{CATEGORIES.map((cat) => ( ))}
newDate && setDate(newDate)} initialFocus />