"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"; import GoogleMapWithDrawing from "@/components/google-map-with-drawing"; const farmFormSchema = z.object({ name: z.string().min(2, "Farm name must be at least 2 characters"), latitude: z.number().min(-90, "Invalid latitude").max(90, "Invalid latitude"), longitude: z.number().min(-180, "Invalid longitude").max(180, "Invalid longitude"), 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: "", latitude: 0, longitude: 0, type: "", area: "", }, }); const handleSubmit = async (values: z.infer) => { try { setIsSubmitting(true); const farmData: Partial = { Name: values.name, Lat: values.latitude, Lon: values.longitude, FarmType: values.type, TotalSize: values.area, }; await onSubmit(farmData); form.reset(); } catch (error) { console.error("Error submitting form:", error); } finally { setIsSubmitting(false); } }; const handleAreaSelected = (coordinates: { lat: number; lng: number }[]) => { if (coordinates.length > 0) { const { lat, lng } = coordinates[0]; form.setValue("latitude", lat); form.setValue("longitude", lng); } }; return (
{/* Form Section */}
( Farm Name This is your farm's display name. )} /> ( Latitude )} /> ( Longitude )} /> ( Farm Type )} /> ( Total Area (optional) The total size of your farm )} />
{/* Map Section */}
Farm Location
); }