Files
validation-lab-site/src/components/LeadForm.astro
T

177 lines
7.7 KiB
Plaintext

---
// The lead form used by both audience pages. Labels come from the shared
// per-locale strings; on submit a small fetch POSTs the payload to the
// page's Zoho Flow webhook (token-in-URL authentication, no server code).
// Deliberately minimal — the CRM only needs a name and an email; country
// is deduced downstream from the submitted locale or the IP address.
// The hubs variant carries a hidden "capabilities" field (set by the
// capability checklist) and the founders variant a hidden "price_range"
// field (set by the pricing checklist) — leads arrive in the CRM already
// tagged with what interested them.
const { type: pageId, locale, strings, site } = Astro.props;
// pageId arrives as the route id ("for-founders"/"for-support-organisations")
// — normalise to the form kind:
const type = pageId === "for-founders" ? "founders" : "hubs";
const fields =
type === "founders"
? [
{ name: "name", label: strings.field_name, input: "text", required: true },
{ name: "email", label: strings.field_email, input: "email", required: true },
]
: [
{ name: "organisation", label: strings.field_organisation, input: "text", required: true },
{ name: "name", label: strings.field_name, input: "text", required: true },
{ name: "email", label: strings.field_email, input: "email", required: true },
{ name: "message", label: strings.field_message, input: "textarea" },
];
const webhook =
type === "founders" ? site.zoho_webhook_founders : site.zoho_webhook_hubs;
const title = type === "founders" ? strings.form_founders_title : strings.form_hubs_title;
const note = type === "founders" ? strings.form_founders_note : strings.form_hubs_note;
---
<section class="lead-wrap">
<form
id="lead-form"
class="lead"
data-webhook={webhook}
data-sending-label={strings.form_sending}
data-locale={locale}
data-type={type}
data-i18n-success={strings.form_success}
data-i18n-error={strings.form_error}
data-contact-email={site.contact_email}
>
<h2 id="lead-form">{title}</h2>
<p class="lead-note">{note}</p>
<p class="cap-selection" id="cap-selection" hidden></p>
<p class="cap-selection" id="price-selection" hidden></p>
{fields.map((f) => (
<label>
{f.label}{f.required ? " *" : ""}
{f.input === "select" ? (
<select name={f.name} required={f.required ?? false}>
{f.options.map(([value, label]) => (
<option value={value}>{label}</option>
))}
</select>
) : f.input === "textarea" ? (
<textarea name={f.name} required={f.required ?? false}></textarea>
) : (
<input type={f.input} name={f.name} required={f.required ?? false} />
)}
</label>
))}
<input
class="hp"
type="text"
name="website"
tabindex="-1"
autocomplete="off"
aria-hidden="true"
/>
<input type="hidden" name="capabilities" value="" />
<input type="hidden" name="price_range" value="" />
<input type="hidden" name="price_model" value="" />
<input type="hidden" name="locale" value={locale} />
<input type="hidden" name="source" value={type} />
<label class="consent">
<input type="checkbox" name="consent" required />
<span>{strings.form_consent}</span>
</label>
<button class="button" type="submit">{type === "hubs" ? strings.form_hubs_send : strings.form_founders_send ?? strings.form_send}</button>
</form>
<script is:inline>
(() => {
const form = document.getElementById("lead-form");
if (!form || form.dataset.bound) return;
form.dataset.bound = "1";
const STRINGS = {
sending: form.dataset.sendingLabel,
};
// CHECKLISTS above the form (capabilities on the hubs page, the
// four price questions on the founders page): collect the checked
// boxes at submit time so each selection rides the payload — machine
// keys joined as group=option pairs for prices, and a human-readable
// echo per checklist mirrors the live selection back to the visitor.
const collect = (attrName, containerId) => {
const field = form.querySelector(`input[name="${attrName === "data-cap" ? "capabilities" : "price_range"}"]`);
const echo = document.getElementById(containerId);
const key = (c) =>
attrName === "data-cap"
? c.dataset.cap
: `${c.dataset.priceGroup}=${c.dataset.price}`;
const refresh = () => {
const checked = [...document.querySelectorAll(`[${attrName}]:checked`)];
if (echo) {
echo.textContent = checked
.map((c) => c.dataset.capLabel || c.dataset.priceLabel || key(c))
.join(", ");
echo.hidden = !checked.length;
}
if (field) field.value = checked.map(key).join(",");
};
document.querySelectorAll(`[${attrName}]`).forEach((el) => {
el.addEventListener("change", refresh);
});
refresh();
};
collect("data-cap", "cap-selection");
collect("data-price", "price-selection");
form.addEventListener("submit", async (event) => {
event.preventDefault();
const button = form.querySelector('button[type="submit"]');
const data = new FormData(form);
const payload = {};
data.forEach((v, k) => { payload[k] = v; });
// consent as a JSON boolean (the Flow/CRM mapping expects true/false,
// not the checkbox's literal "on"; an unchecked box never arrives)
payload.consent = data.get("consent") === "on";
if (payload.website) return; // honeypot: pretend success, send nothing
// The pricing-model picker sits outside the <form>; it is mirrored
// into the hidden price_model input on change, so FormData carries it.
// On top of the machine fields, concat every tracked selection into
// the message: Zoho Flow reliably maps the standard fields only, and
// this way nothing is lost in field mapping.
const lines = [];
const capLabels = [...document.querySelectorAll("[data-cap]:checked")]
.map((c) => c.dataset.capLabel || c.dataset.cap);
if (capLabels.length) lines.push("Selected features: " + capLabels.join(", "));
if (payload.price_model) lines.push("Pricing model: " + payload.price_model);
const pricePairs = [...document.querySelectorAll("[data-price]:checked")]
.map((c) => c.dataset.priceGroup + " = " + c.dataset.priceLabel);
if (pricePairs.length) lines.push("Price answers: " + pricePairs.join("; "));
if (lines.length) payload.message = (payload.message ? payload.message + "\n\n" : "") + lines.join("\n");
button.disabled = true;
button.textContent = STRINGS.sending || "Sending…";
try {
const res = await fetch(form.dataset.webhook, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(payload),
});
if (!res.ok) throw new Error("HTTP " + res.status);
form.innerHTML =
'<p class="lead-ok"></p>';
form.querySelector(".lead-ok").textContent = form.dataset.i18nSuccess;
} catch {
const p = document.createElement("p");
p.className = "lead-err";
p.textContent = form.dataset.i18nError + " ";
const a = document.createElement("a");
a.href = `mailto:${form.dataset.contactEmail}?subject=Validation%20Lab`;
a.textContent = form.dataset.contactEmail;
p.appendChild(a);
form.innerHTML = "";
form.appendChild(p);
button.disabled = false;
}
});
})();
</script>
</section>