import React, { useEffect, useState } from 'react'; import { Loader2 } from 'lucide-react'; /** * Public embed page (iframe). No sign-in required. * Route: /embed/cta/:publicId */ export default function CtaFormEmbed() { const publicId = window.location.pathname.split('/').filter(Boolean).pop() || ''; const [phase, setPhase] = useState('loading'); const [form, setForm] = useState(null); const [error, setError] = useState(''); const [values, setValues] = useState({}); const [submitting, setSubmitting] = useState(false); const [done, setDone] = useState(false); const [doneMsg, setDoneMsg] = useState(''); useEffect(() => { if (!publicId) { setError('Invalid form link'); setPhase('error'); return; } fetch(`/api/public/cta-forms/${encodeURIComponent(publicId)}`) .then((r) => r.json().then((j) => ({ ok: r.ok, j }))) .then(({ ok, j }) => { if (!ok) throw new Error(j.detail || 'Form not found'); setForm(j); const init = {}; (j.fields || []).forEach((f) => { init[f.key] = ''; }); setValues(init); setPhase('ready'); }) .catch((e) => { setError(e.message || 'Could not load form'); setPhase('error'); }); }, [publicId]); const onChange = (key, val) => { setValues((prev) => ({ ...prev, [key]: val })); }; const onSubmit = async (e) => { e.preventDefault(); setSubmitting(true); setError(''); try { const res = await fetch(`/api/public/cta-forms/${encodeURIComponent(publicId)}/submit`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ fields: values, page_url: window.location.href, }), }); const data = await res.json().catch(() => ({})); if (!res.ok) { throw new Error( typeof data.detail === 'string' ? data.detail : 'Submission failed' ); } setDone(true); setDoneMsg(data.message || 'Thank you — we will be in touch soon.'); } catch (err) { setError(err.message || 'Submission failed'); } finally { setSubmitting(false); } }; if (phase === 'loading') { return (
); } if (phase === 'error') { return (

{error}

); } return (
{done ? (

{doneMsg}

) : (
{(form.fields || []).map((f) => (
{f.type === 'textarea' ? (