From 6abfe77f34c97e513d7ff5b104463adbdc4499e7 Mon Sep 17 00:00:00 2001 From: TeshuKatepalli Date: Fri, 27 Oct 2023 11:31:07 +0530 Subject: [PATCH] Updated the sidebar form code to align with forms code --- blocks/sidebar-form/sidebar-form.js | 47 ++++++++++++----------------- 1 file changed, 19 insertions(+), 28 deletions(-) diff --git a/blocks/sidebar-form/sidebar-form.js b/blocks/sidebar-form/sidebar-form.js index a1a82fc0..1058fd1e 100644 --- a/blocks/sidebar-form/sidebar-form.js +++ b/blocks/sidebar-form/sidebar-form.js @@ -1,33 +1,24 @@ -function embedHubSpotForm(formFields) { - const script = document.createElement('script'); - script.type = 'text/javascript'; - script.src = '/scripts/v2.js'; - script.charset = 'utf-8'; - script.setAttribute('async', ''); - script.onload = () => { - window.hbspt.forms.create(formFields); - }; - document.head.appendChild(script); -} +import { readBlockConfig } from '../../scripts/aem.js'; -export default function decorate(block) { - const formDetails = {}; - const tableRows = block.querySelectorAll('tr'); - tableRows.forEach((row) => { - const cells = row.children; - if (cells.length >= 2) { - const key = cells[0].innerText.trim(); - const value = cells[1].innerText.trim(); - formDetails[key] = key === 'target' ? `#${value}` : value; - } - }); - // Add a delay of 4 seconds (4000 milliseconds) before loading the form - setTimeout(() => { - embedHubSpotForm(formDetails); - }, 3000); +let formConfig = {}; +export default function decorate(block) { + formConfig = readBlockConfig(block); const form = document.createElement('div'); - form.id = formDetails.target.replace('#', ''); + form.id = formConfig.target; block.textContent = ''; - block.appendChild(form); + block.append(form); +} + +export function isForm() { + return !!formConfig.target; +} + +export function buildForm(hbspt) { + hbspt.forms.create({ + region: formConfig.region, + portalId: formConfig.portalid, + formId: formConfig.formid, + target: `#${formConfig.target}`, + }); }