"use client"; import { useState } from "react"; import { useForm } from "react-hook-form"; import { zodResolver } from "@hookform/resolvers/zod"; import { z } from "zod"; import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle, DialogTrigger, } from "@/components/ui/dialog"; import { Form, FormControl, FormDescription, FormField, FormItem, FormLabel, FormMessage, } from "@/components/ui/form"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "@/components/ui/select"; import { Input } from "@/components/ui/input"; import { Button } from "@/components/ui/button"; import { Plus } from "lucide-react"; import { transactionSchema } from "@/lib/validations/financial"; type TransactionInput = z.infer; interface AddTransactionDialogProps { onTransactionAdded?: () => void; } export function AddTransactionDialog({ onTransactionAdded }: AddTransactionDialogProps) { const [open, setOpen] = useState(false); const [isSubmitting, setIsSubmitting] = useState(false); const form = useForm({ resolver: zodResolver(transactionSchema), defaultValues: { description: "", amount: 0, type: "expense", date: new Date().toISOString().split('T')[0], categoryId: "", accountId: "", isRecurring: false, recurringInterval: "monthly", tags: [], notes: "", merchant: "", location: "", }, }); const onSubmit = async (data: any) => { try { setIsSubmitting(true); const response = await fetch("/api/transactions", { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify(data), }); if (response.ok) { form.reset(); setOpen(false); onTransactionAdded?.(); } else { const error = await response.json(); console.error("Failed to add transaction:", error); } } catch (error) { console.error("Error adding transaction:", error); } finally { setIsSubmitting(false); } }; return ( Add Transaction Record a new income or expense transaction.
( Description )} /> ( Amount field.onChange(parseFloat(e.target.value))} /> )} /> ( Type )} /> ( Date )} /> ( Account )} /> ( Category )} /> ( Merchant (Optional) )} /> ( Notes (Optional) )} />
); }