| 1 |
import React, { useEffect, useRef } from 'react'; |
| 2 |
import UIOverlay from '../UIComponents/UIOverlay'; |
| 3 |
import { |
| 4 |
formsScriptPayload, |
| 5 |
hublet as region, |
| 6 |
} from '../../constants/leadinConfig'; |
| 7 |
import PreviewDisabled from '../Common/PreviewDisabled'; |
| 8 |
|
| 9 |
export default function PreviewForm({ |
| 10 |
portalId, |
| 11 |
formId, |
| 12 |
fullSiteEditor, |
| 13 |
embedVersion, |
| 14 |
}: { |
| 15 |
portalId: number; |
| 16 |
formId: string; |
| 17 |
fullSiteEditor?: boolean; |
| 18 |
embedVersion?: string; |
| 19 |
}) { |
| 20 |
const isFormV4 = embedVersion === 'v4'; |
| 21 |
|
| 22 |
const inputEl = useRef<HTMLDivElement>(null); |
| 23 |
|
| 24 |
useEffect(() => { |
| 25 |
if (inputEl.current) { |
| 26 |
//@ts-expect-error Hubspot global |
| 27 |
const hbspt = window.parent.hbspt || window.hbspt; |
| 28 |
inputEl.current.innerHTML = ''; |
| 29 |
const isQa = formsScriptPayload.includes('qa'); |
| 30 |
if (isFormV4) { |
| 31 |
const container = document.createElement('div'); |
| 32 |
container.classList.add('hs-form-frame'); |
| 33 |
container.dataset.region = region; |
| 34 |
container.dataset.formId = formId; |
| 35 |
container.dataset.portalId = portalId.toString(); |
| 36 |
container.dataset.env = isQa ? 'qa' : ''; |
| 37 |
inputEl.current.appendChild(container); |
| 38 |
} else { |
| 39 |
const additionalParams = isQa ? { env: 'qa' } : {}; |
| 40 |
|
| 41 |
hbspt.forms.create({ |
| 42 |
portalId, |
| 43 |
formId, |
| 44 |
region, |
| 45 |
target: `#${inputEl.current.id}`, |
| 46 |
...additionalParams, |
| 47 |
}); |
| 48 |
} |
| 49 |
} |
| 50 |
}, [formId, portalId, inputEl, isFormV4]); |
| 51 |
|
| 52 |
if (fullSiteEditor) { |
| 53 |
return <PreviewDisabled />; |
| 54 |
} |
| 55 |
|
| 56 |
return <UIOverlay ref={inputEl} id={`hbspt-previewform-${formId}`} />; |
| 57 |
} |
| 58 |
|