Files
moneymind/src/components/add-transaction-dialog.tsx
Rosario Moscato 5270bbd40a feat: implement MoneyMind personal finance management application
- Complete transformation from boilerplate to full-featured financial app
- Add comprehensive dashboard with KPI cards and interactive charts
- Implement transaction management with predefined expense/income categories
- Create account management system with multiple account types
- Add authentication flow with session management
- Implement analytics overview with demo financial data
- Add budget tracking and goal progress visualization
- Include custom category creation functionality
- Update branding and footer with MoneyMind by RoMoS
- Add shadcn/ui components and Recharts for data visualization

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-09-29 14:41:40 +02:00

265 lines
8.1 KiB
TypeScript

"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<typeof transactionSchema>;
interface AddTransactionDialogProps {
onTransactionAdded?: () => void;
}
export function AddTransactionDialog({ onTransactionAdded }: AddTransactionDialogProps) {
const [open, setOpen] = useState(false);
const [isSubmitting, setIsSubmitting] = useState(false);
const form = useForm<any>({
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 (
<Dialog open={open} onOpenChange={setOpen}>
<DialogTrigger asChild>
<Button>
<Plus className="h-4 w-4 mr-2" />
Add Transaction
</Button>
</DialogTrigger>
<DialogContent className="sm:max-w-[425px]">
<DialogHeader>
<DialogTitle>Add Transaction</DialogTitle>
<DialogDescription>
Record a new income or expense transaction.
</DialogDescription>
</DialogHeader>
<Form {...form}>
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-4">
<FormField
control={form.control}
name="description"
render={({ field }: { field: any }) => (
<FormItem>
<FormLabel>Description</FormLabel>
<FormControl>
<Input placeholder="e.g., Grocery shopping" {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="amount"
render={({ field }: { field: any }) => (
<FormItem>
<FormLabel>Amount</FormLabel>
<FormControl>
<Input
type="number"
step="0.01"
placeholder="0.00"
{...field}
onChange={(e) => field.onChange(parseFloat(e.target.value))}
/>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="type"
render={({ field }: { field: any }) => (
<FormItem>
<FormLabel>Type</FormLabel>
<Select onValueChange={field.onChange} defaultValue={field.value}>
<FormControl>
<SelectTrigger>
<SelectValue placeholder="Select type" />
</SelectTrigger>
</FormControl>
<SelectContent>
<SelectItem value="income">Income</SelectItem>
<SelectItem value="expense">Expense</SelectItem>
</SelectContent>
</Select>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="date"
render={({ field }: { field: any }) => (
<FormItem>
<FormLabel>Date</FormLabel>
<FormControl>
<Input type="date" {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="accountId"
render={({ field }: { field: any }) => (
<FormItem>
<FormLabel>Account</FormLabel>
<Select onValueChange={field.onChange} defaultValue={field.value}>
<FormControl>
<SelectTrigger>
<SelectValue placeholder="Select account" />
</SelectTrigger>
</FormControl>
<SelectContent>
<SelectItem value="">Select account</SelectItem>
</SelectContent>
</Select>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="categoryId"
render={({ field }: { field: any }) => (
<FormItem>
<FormLabel>Category</FormLabel>
<Select onValueChange={field.onChange} defaultValue={field.value}>
<FormControl>
<SelectTrigger>
<SelectValue placeholder="Select category" />
</SelectTrigger>
</FormControl>
<SelectContent>
<SelectItem value="">Select category</SelectItem>
</SelectContent>
</Select>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="merchant"
render={({ field }: { field: any }) => (
<FormItem>
<FormLabel>Merchant (Optional)</FormLabel>
<FormControl>
<Input placeholder="e.g., Amazon, Walmart" {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="notes"
render={({ field }: { field: any }) => (
<FormItem>
<FormLabel>Notes (Optional)</FormLabel>
<FormControl>
<Input placeholder="Additional notes..." {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<div className="flex justify-end space-x-2">
<Button
type="button"
variant="outline"
onClick={() => setOpen(false)}
disabled={isSubmitting}
>
Cancel
</Button>
<Button type="submit" disabled={isSubmitting}>
{isSubmitting ? "Adding..." : "Add Transaction"}
</Button>
</div>
</form>
</Form>
</DialogContent>
</Dialog>
);
}