import React, { useState } from 'react'; import { format } from 'date-fns'; import './BookingForm.css'; const BookingForm = ({ service, resources, onSubmit, onCancel, loading }) => { const [formData, setFormData] = useState({ resource: resources?.[0]?.id || '', date: '', time: '', }); const [errors, setErrors] = useState({}); const handleChange = (e) => { const { name, value } = e.target; setFormData(prev => ({ ...prev, [name]: value })); // Clear error for this field if (errors[name]) { setErrors(prev => ({ ...prev, [name]: '' })); } }; const validate = () => { const newErrors = {}; if (!formData.resource) { newErrors.resource = 'Please select a resource'; } if (!formData.date) { newErrors.date = 'Please select a date'; } if (!formData.time) { newErrors.time = 'Please select a time'; } setErrors(newErrors); return Object.keys(newErrors).length === 0; }; const handleSubmit = (e) => { e.preventDefault(); if (!validate()) { return; } // Combine date and time into ISO format const startDateTime = new Date(`${formData.date}T${formData.time}`); const endDateTime = new Date(startDateTime.getTime() + service.duration * 60000); const appointmentData = { service: service.id, resource: parseInt(formData.resource), start_time: startDateTime.toISOString(), end_time: endDateTime.toISOString(), }; onSubmit(appointmentData); }; return (
Duration: {service.duration} minutes
Price: ${service.price}