"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, FormField, FormItem, FormLabel, FormMessage } from "@/components/ui/form"; import { useQuery } from "@tanstack/react-query"; import { getPlants, PlantResponse } from "@/api/plant"; import { Loader2 } from "lucide-react"; import { Cropland } from "@/types"; // Update schema to reflect Cropland fields needed for creation // Removed plantedDate as it's derived from createdAt on backend // Added plantId, landSize, growthStage, priority const cropFormSchema = z.object({ name: z.string().min(2, "Crop name must be at least 2 characters"), plantId: z.string().uuid("Please select a valid plant"), // Changed from name to ID status: z.enum(["planned", "growing", "harvested", "fallow"]), // Added fallow landSize: z.preprocess( (val) => parseFloat(z.string().parse(val)), // Convert string input to number z.number().positive("Land size must be a positive number") ), growthStage: z.string().min(1, "Growth stage is required"), priority: z.preprocess( (val) => parseInt(z.string().parse(val), 10), // Convert string input to number z.number().int().min(0, "Priority must be non-negative") ), // GeoFeature will be handled separately by the map component later }); interface AddCropFormProps { onSubmit: (data: Partial) => Promise; // Expect Partial onCancel: () => void; isSubmitting: boolean; // Receive submitting state } export function AddCropForm({ onSubmit, onCancel, isSubmitting }: AddCropFormProps) { // Fetch plants for the dropdown const { data: plantData, isLoading: isLoadingPlants, isError: isErrorPlants, } = useQuery({ queryKey: ["plants"], queryFn: getPlants, staleTime: 1000 * 60 * 60, // Cache for 1 hour }); const form = useForm>({ resolver: zodResolver(cropFormSchema), defaultValues: { name: "", plantId: "", // Initialize plantId status: "planned", landSize: 0, growthStage: "Planned", priority: 1, }, }); const handleSubmit = (values: z.infer) => { // Submit data shaped like Partial onSubmit({ name: values.name, plantId: values.plantId, status: values.status, landSize: values.landSize, growthStage: values.growthStage, priority: values.priority, // farmId is added in the parent component's mutationFn // geoFeature would be passed separately if using map here }); }; return (
( Cropland Name )} /> {/* Plant Selection */} ( Select Plant )} /> {/* Status Selection */} ( Status )} /> {/* Land Size */} ( Land Size (e.g., Hectares) {/* Use text input for flexibility, validation handles number conversion */} field.onChange(e.target.value)} /> )} /> {/* Growth Stage */} ( Initial Growth Stage )} /> {/* Priority */} ( Priority field.onChange(e.target.value)} /> )} /> {/* TODO: Add GeoFeature input using the map component if needed within this dialog */} {/*
*/}
); }