import { useState } from 'react'; import { cn } from '@/lib/utils'; interface ContactFormSectionProps { id: string; layout?: string; colorScheme?: string; title?: string; webhook_url?: string; redirect_url?: string; fields?: string[]; elementStyles?: Record; } export function ContactFormSection({ id, layout = 'default', colorScheme = 'default', title, webhook_url, redirect_url, fields = ['name', 'email', 'message'], elementStyles, styles, }: ContactFormSectionProps & { styles?: Record }) { const [formData, setFormData] = useState>({}); // Helper to get text styles (including font family) const getTextStyles = (elementName: string) => { const styles = elementStyles?.[elementName] || {}; return { classNames: cn( styles.fontSize, styles.fontWeight, { 'font-sans': styles.fontFamily === 'secondary', 'font-serif': styles.fontFamily === 'primary', } ), style: { color: styles.color, textAlign: styles.textAlign, backgroundColor: styles.backgroundColor, borderColor: styles.borderColor, borderWidth: styles.borderWidth, borderRadius: styles.borderRadius, } }; }; const titleStyle = getTextStyles('title'); const buttonStyle = getTextStyles('button'); const fieldsStyle = getTextStyles('fields'); const [submitting, setSubmitting] = useState(false); const [error, setError] = useState(null); const handleChange = (e: React.ChangeEvent) => { setFormData({ ...formData, [e.target.name]: e.target.value }); }; const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); setSubmitting(true); setError(null); try { // Submit to webhook if provided if (webhook_url) { await fetch(webhook_url, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(formData), }); } // Redirect after submission if (redirect_url) { // Replace placeholders in redirect URL let finalUrl = redirect_url; Object.entries(formData).forEach(([key, value]) => { finalUrl = finalUrl.replace(`{${key}}`, encodeURIComponent(value)); }); window.location.href = finalUrl; } } catch (err) { setError('Failed to submit form. Please try again.'); } finally { setSubmitting(false); } }; return (
{title && (

{title}

)}
{fields.map((field) => { const fieldLabel = field.charAt(0).toUpperCase() + field.slice(1).replace('_', ' '); const isTextarea = field === 'message' || field === 'content'; return (
{isTextarea ? (