"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 { useCallback, useEffect } from "react"; // Added useEffect import { Loader2 } from "lucide-react"; import type { Farm } from "@/types"; import GoogleMapWithDrawing, { type ShapeData } from "@/components/google-map-with-drawing"; // Schema for editing - make fields optional if needed, but usually same as create const farmFormSchema = z.object({ name: z.string().min(2, "Farm name must be at least 2 characters"), latitude: z .number({ invalid_type_error: "Latitude must be a number" }) .min(-90, "Invalid latitude") .max(90, "Invalid latitude") .refine((val) => val !== 0, { message: "Please select a location on the map." }), longitude: z .number({ invalid_type_error: "Longitude must be a number" }) .min(-180, "Invalid longitude") .max(180, "Invalid longitude") .refine((val) => val !== 0, { message: "Please select a location on the map." }), type: z.string().min(1, "Please select a farm type"), area: z.string().optional(), }); export interface EditFarmFormProps { initialData: Farm; // Require initial data for editing onSubmit: (data: Partial>) => Promise; // Exclude non-editable fields onCancel: () => void; isSubmitting: boolean; } export function EditFarmForm({ initialData, onSubmit, onCancel, isSubmitting }: EditFarmFormProps) { const form = useForm>({ resolver: zodResolver(farmFormSchema), // Set default values from initialData defaultValues: { name: initialData.name || "", latitude: initialData.lat || 0, longitude: initialData.lon || 0, type: initialData.farmType || "", area: initialData.totalSize || "", }, }); // Update form if initialData changes (e.g., opening dialog for different farms) useEffect(() => { form.reset({ name: initialData.name || "", latitude: initialData.lat || 0, longitude: initialData.lon || 0, type: initialData.farmType || "", area: initialData.totalSize || "", }); }, [initialData, form]); const handleSubmit = async (values: z.infer) => { try { // Shape data for the API update function const farmUpdateData: Partial> = { name: values.name, lat: values.latitude, lon: values.longitude, farmType: values.type, totalSize: values.area, }; await onSubmit(farmUpdateData); // No need to reset form here, dialog closing handles it or parent component does } catch (error) { console.error("Error submitting edit form:", error); // Error handled by mutation's onError } }; // Map handler - same as AddFarmForm const handleShapeDrawn = useCallback( (data: ShapeData) => { if (data.type === "marker") { const { lat, lng } = data.position; form.setValue("latitude", lat, { shouldValidate: true }); form.setValue("longitude", lng, { shouldValidate: true }); } else { console.log(`Shape type '${data.type}' ignored for coordinate update.`); } }, [form] ); return (
{/* Form Section */}
{/* Fields: Name, Lat/Lon, Type, Area - same structure as AddFarmForm */} {/* Farm Name Field */} ( Farm Name This is your farm's display name. )} /> {/* Coordinate Fields (Latitude & Longitude) */}
( Latitude )} /> ( Longitude )} />
{/* Farm Type Selection */} ( Farm Type )} /> {/* Total Area Field */} ( Total Area (optional) The total size of your farm (e.g., "15 rai", "10 hectares"). )} /> {/* Submit and Cancel Buttons */}
{/* Map Section */}
Farm Location (Update marker if needed)
Click the marker tool and place a new marker to update coordinates.
); }