import { useState } from 'react'; interface ListingFormData { title: string; description: string; price: string; quantity: string; category: string; tags: string; city: string; region: string; } interface ListingFormProps { initialData?: Partial; onSubmit: (data: ListingFormData) => Promise; submitLabel?: string; isLoading?: boolean; } const categories = [ { value: 'seeds', label: 'Seeds', icon: '🌰', description: 'Plant seeds for growing' }, { value: 'seedlings', label: 'Seedlings', icon: '🌱', description: 'Young plants ready for transplanting' }, { value: 'mature_plants', label: 'Mature Plants', icon: '🪴', description: 'Fully grown plants' }, { value: 'cuttings', label: 'Cuttings', icon: '✂️', description: 'Plant cuttings for propagation' }, { value: 'produce', label: 'Produce', icon: '🥬', description: 'Fresh fruits and vegetables' }, { value: 'supplies', label: 'Supplies', icon: '🧰', description: 'Gardening tools and supplies' }, ]; export function ListingForm({ initialData = {}, onSubmit, submitLabel = 'Create Listing', isLoading = false, }: ListingFormProps) { const [formData, setFormData] = useState({ title: initialData.title || '', description: initialData.description || '', price: initialData.price || '', quantity: initialData.quantity || '1', category: initialData.category || '', tags: initialData.tags || '', city: initialData.city || '', region: initialData.region || '', }); const [errors, setErrors] = useState>>({}); const handleChange = ( e: React.ChangeEvent ) => { const { name, value } = e.target; setFormData((prev) => ({ ...prev, [name]: value })); // Clear error when field is edited if (errors[name as keyof ListingFormData]) { setErrors((prev) => ({ ...prev, [name]: undefined })); } }; const validate = (): boolean => { const newErrors: Partial> = {}; if (!formData.title.trim()) { newErrors.title = 'Title is required'; } else if (formData.title.length < 10) { newErrors.title = 'Title must be at least 10 characters'; } if (!formData.description.trim()) { newErrors.description = 'Description is required'; } else if (formData.description.length < 20) { newErrors.description = 'Description must be at least 20 characters'; } if (!formData.price) { newErrors.price = 'Price is required'; } else if (parseFloat(formData.price) <= 0) { newErrors.price = 'Price must be greater than 0'; } if (!formData.quantity) { newErrors.quantity = 'Quantity is required'; } else if (parseInt(formData.quantity, 10) < 1) { newErrors.quantity = 'Quantity must be at least 1'; } if (!formData.category) { newErrors.category = 'Category is required'; } setErrors(newErrors); return Object.keys(newErrors).length === 0; }; const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); if (!validate()) { return; } await onSubmit(formData); }; return (
{/* Title */}
{errors.title &&

{errors.title}

}
{/* Description */}