mirror of
https://github.com/Sea-Haven-Industries/seahaven-site.git
synced 2026-10-04 18:21:58 +00:00
58 lines
2 KiB
JavaScript
58 lines
2 KiB
JavaScript
|
|
// Basin AJAX submission with reCAPTCHA v3 for the contact form and job apply forms.
|
||
|
|
(function () {
|
||
|
|
"use strict";
|
||
|
|
|
||
|
|
const SITE_KEY = "6Les66kUAAAAANyLrgkl7iuN4JUpNlB5upaMovI4";
|
||
|
|
// Contact page wraps the form in a .contact-form div; services puts the class
|
||
|
|
// on the <form> itself; job pages use .apply-form. Match all three.
|
||
|
|
const forms = document.querySelectorAll(".contact-form form, form.contact-form, form.apply-form");
|
||
|
|
if (!forms.length) return;
|
||
|
|
|
||
|
|
forms.forEach((form) => {
|
||
|
|
const success = document.getElementById("form-success");
|
||
|
|
const statusEl = document.getElementById("form-status"); // optional role="status" region
|
||
|
|
const tokenField = form.querySelector("#g-recaptcha-response");
|
||
|
|
const btn = form.querySelector('button[type="submit"]');
|
||
|
|
const originalLabel = btn ? btn.textContent : "";
|
||
|
|
|
||
|
|
const announce = (msg) => {
|
||
|
|
if (statusEl) statusEl.textContent = msg;
|
||
|
|
else if (msg) alert(msg);
|
||
|
|
};
|
||
|
|
|
||
|
|
form.addEventListener("submit", async (e) => {
|
||
|
|
e.preventDefault();
|
||
|
|
if (btn) {
|
||
|
|
btn.textContent = btn.dataset.sending || "Sending…";
|
||
|
|
btn.disabled = true;
|
||
|
|
}
|
||
|
|
announce("");
|
||
|
|
|
||
|
|
try {
|
||
|
|
const token = await grecaptcha.execute(SITE_KEY, { action: "submit" });
|
||
|
|
if (tokenField) tokenField.value = token;
|
||
|
|
const res = await fetch(form.action, {
|
||
|
|
method: "POST",
|
||
|
|
body: new FormData(form),
|
||
|
|
headers: { Accept: "application/json" },
|
||
|
|
});
|
||
|
|
if (!res.ok) throw new Error("Submission failed");
|
||
|
|
form.reset();
|
||
|
|
form.style.display = "none";
|
||
|
|
if (success) {
|
||
|
|
success.style.display = "block";
|
||
|
|
success.scrollIntoView({ behavior: "smooth", block: "center" });
|
||
|
|
}
|
||
|
|
} catch (err) {
|
||
|
|
if (btn) {
|
||
|
|
btn.textContent = originalLabel;
|
||
|
|
btn.disabled = false;
|
||
|
|
}
|
||
|
|
announce(
|
||
|
|
"Something went wrong. Please try again or email us directly at work-orders@seahaven.com"
|
||
|
|
);
|
||
|
|
}
|
||
|
|
});
|
||
|
|
});
|
||
|
|
})();
|