"use client"; import { zodResolver } from "@hookform/resolvers/zod"; import { useForm } from "react-hook-form"; import * as z from "zod"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select"; import { Form, FormControl, FormDescription, FormField, FormItem, FormLabel, FormMessage } from "@/components/ui/form"; import { useState } from "react"; import { Loader2 } from "lucide-react"; import type { Farm } from "@/types"; const farmFormSchema = z.object({ name: z.string().min(2, "Farm name must be at least 2 characters"), location: z.string().min(2, "Location must be at least 2 characters"), type: z.string().min(1, "Please select a farm type"), area: z.string().optional(), }); export interface AddFarmFormProps { onSubmit: (data: Partial) => Promise; onCancel: () => void; } export function AddFarmForm({ onSubmit, onCancel }: AddFarmFormProps) { const [isSubmitting, setIsSubmitting] = useState(false); const form = useForm>({ resolver: zodResolver(farmFormSchema), defaultValues: { name: "", location: "", type: "", area: "", }, }); const handleSubmit = async (values: z.infer) => { try { setIsSubmitting(true); await onSubmit(values); form.reset(); } catch (error) { console.error("Error submitting form:", error); } finally { setIsSubmitting(false); } }; return (
( Farm Name This is your farm's display name. )} /> ( Location City, region or specific address )} /> ( Farm Type )} /> ( Total Area (optional) The total size of your farm )} />
); }