"use client" import { useState } from "react" import { useForm } from "react-hook-form" import { CheckCircle, AlertCircle } from "lucide-react" import { FormInput } from "./form-input" import { FormTextarea } from "./form-textarea" import { FormButton } from "./form-button" import { WebhookClient } from "@/lib/webhook-client" interface ContactFormData { firstName: string lastName: string email: string phone: string company: string message: string source?: string page?: string } interface ContactFormProps { onSubmit?: (data: ContactFormData) => void className?: string } export function ContactForm({ onSubmit, className }: ContactFormProps) { const [isSubmitting, setIsSubmitting] = useState(false) const [isSubmitted, setIsSubmitted] = useState(false) const [submitError, setSubmitError] = useState(null) const { register, handleSubmit, formState: { errors }, reset, setValue, watch, } = useForm({ defaultValues: { source: "website", page: "", }, }) const watchedValues = watch() // Update page URL when component mounts useState(() => { if (typeof window !== 'undefined') { setValue("page", window.location.pathname) } }) const onFormSubmit = async (data: ContactFormData) => { setIsSubmitting(true) setSubmitError(null) try { // Add URL and timestamp const formData = { ...data, timestamp: new Date().toISOString(), url: window.location.href, } // Call the webhook client directly const result = await WebhookClient.submitContactForm(formData) if (!result.success) { throw new Error(result.error || "Failed to submit form") } setIsSubmitted(true) reset() // Call custom onSubmit handler if provided if (onSubmit) { onSubmit(formData) } } catch (error) { console.error("Form submission error:", error) setSubmitError("Failed to submit form. Please try again.") } finally { setIsSubmitting(false) } } const onFormReset = () => { setIsSubmitted(false) setSubmitError(null) reset() } if (isSubmitted) { return (

Thank You!

We've received your message and will get back to you within 24 hours.

) } return (
{/* Honeypot field for spam prevention */}
{/* Name Fields */}
{/* Contact Information */}
{ // Remove all non-digit characters const phoneDigits = value.replace(/\D/g, '') return phoneDigits.length >= 10 && phoneDigits.length <= 15 || "Please enter a valid phone number (10-15 digits)" }, })} />
{/* Company (Optional) */} {/* Message */} {/* Submit Error */} {submitError && (
{submitError}
)} {/* Submit Button */} {isSubmitting ? "Sending..." : "Send Message"} ) }