'use client' import React, { useState } from 'react' import type { FDContactFormBlock as FDContactFormBlockProps } from '@/payload-types' import type { Media } from '@/payload-types' import { FDImage } from '@/components/FDImage' const bgMap: Record = { white: 'bg-white', gray: 'bg-fd-gray-light', } export const FDContactFormBlockComponent: React.FC = ({ heading, description, form: formRelation, submitText = 'Skicka förfrågan', privacyText, privacyLinkText, privacyLinkUrl, sideImage, background = 'white', }) => { const [formData, setFormData] = useState>({}) const [status, setStatus] = useState<'idle' | 'sending' | 'sent' | 'error'>('idle') const media = sideImage as Media | undefined const hasSideImage = Boolean(media?.url) const sectionBg = bgMap[background || 'white'] // Extract form object — could be a populated object or just an ID const form = formRelation && typeof formRelation === 'object' ? formRelation : null const formId = form ? form.id : (formRelation ?? null) const handleChange = (name: string, value: string) => { setFormData((prev) => ({ ...prev, [name]: value })) } const handleSubmit = async (e: React.FormEvent) => { e.preventDefault() if (!formId) { console.warn('No form ID configured') return } setStatus('sending') try { const res = await fetch('/api/form-submissions', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ form: formId, submissionData: Object.entries(formData).map(([field, value]) => ({ field, value, })), }), }) setStatus(res.ok ? 'sent' : 'error') } catch { setStatus('error') } } const baseInputClass = 'w-full bg-gray-100 rounded-lg px-4 py-3 font-joey text-fd-navy text-base outline-none focus:ring-2 focus:ring-fd-navy/20 transition-shadow' const renderFormField = (field: any) => { const blockType = field.blockType const name = field.name || field.label?.toLowerCase().replace(/\s+/g, '-') || '' const label = field.label || '' const required = field.required || false const width = field.width ? Number(field.width) : 100 const input = (() => { switch (blockType) { case 'textarea': return (