import { useState, useEffect } from 'react'; import ModalErrorDisplay from '../common/ModalErrorDisplay'; import './PlannableForm.css'; const PlannableForm = ({ item, tripId, calendarSlots, onSubmit, onCancel }) => { const [formData, setFormData] = useState({ name: '', type: 'attraction', address: '', notes: '', calendar_slot_id: null }); const [errors, setErrors] = useState({}); const [submitting, setSubmitting] = useState(false); const [submitError, setSubmitError] = useState(null); useEffect(() => { if (item) { setFormData({ name: item.name || '', type: item.type || 'attraction', address: item.address || '', notes: item.notes || '', calendar_slot_id: item.calendar_slot_id || null }); } }, [item]); const handleChange = (e) => { const { name, value } = e.target; setFormData(prev => ({ ...prev, [name]: value === '' ? null : value })); // Clear error for this field if (errors[name]) { setErrors(prev => ({ ...prev, [name]: null })); } // Clear submit error when user starts typing if (submitError) { setSubmitError(null); } }; const validate = () => { const newErrors = {}; if (!formData.name || formData.name.trim() === '') { newErrors.name = 'Name is required'; } if (!formData.type) { newErrors.type = 'Type is required'; } return newErrors; }; const handleSubmit = async (e) => { e.preventDefault(); const newErrors = validate(); if (Object.keys(newErrors).length > 0) { setErrors(newErrors); return; } setSubmitting(true); setSubmitError(null); try { await onSubmit(formData); } catch (err) { console.error('Form submission error:', err); setSubmitError(err.message || 'Failed to save item. Please try again.'); } finally { setSubmitting(false); } }; return (

{item ? 'Edit Item' : 'Add New Item'}

setSubmitError(null)} />
{errors.name && {errors.name}}
{errors.type && {errors.type}}