@pointamarketing/form-submission
Version:
Form submission utility suite
2 lines (1 loc) • 3.06 kB
JavaScript
async function b(){let o=window;if(!o.recaptchaLoaded)return new Promise(a=>{let n=setInterval(()=>{o.recaptchaLoaded&&(clearInterval(n),a())},100)})}function v(o){return new Promise(a=>{window.grecaptcha.ready(async()=>{try{let n=await window.grecaptcha.execute(o,{action:"submit"});a({token:n,error:null})}catch(n){a({token:null,error:n})}})})}async function R(o,a){let n="https://consumers-gateway-4cygfe52ia-ew.a.run.app:443/pointa.pa.services.public.v1.FormsService/ValidateFormSubmission",s={token:o,recaptchaAction:"submit",recaptchaKey:a};try{let e=await fetch(n,{method:"POST",body:JSON.stringify(s),headers:{"Content-Type":"application/json"}});if(!e.ok)throw new Error(`HTTP error! status: ${e.status}`);return{data:await e.json(),error:null}}catch(e){return{data:null,error:e instanceof Error?e:new Error(String(e))}}}var p=async o=>{await b();let{token:a,error:n}=await v(o);if(n)throw console.error("reCAPTCHA error:",n),new Error("reCAPTCHA execution failed");let{data:s,error:e}=await R(a,o);if(e||s.score<=.8)throw console.error("Validation error:",e),new Error(e?"reCAPTCHA validation failed":"reCAPTCHA score too low")};async function S(o,a){let s=`https://consumers-gateway-4cygfe52ia-ew.a.run.app:443/pointa.pa.services.public.v1.FormsService/${a}`,e=o.reduce((t,{formProperty:r,formValue:i})=>({...t,[r]:i}),{});try{let t=await fetch(s,{method:"POST",body:JSON.stringify(e),headers:{"Content-Type":"application/json"}});if(!t.ok)throw new Error(`Request failed with status: ${t.status}`);return{data:await t.json(),error:null}}catch(t){return{data:null,error:t instanceof Error?t:new Error(String(t))}}}function u(o,a,n){let s,e,t,r,i=()=>{s&&(s.style.display="block"),t&&(t.value="Submit",t.disabled=!1)},w=()=>{r&&(r.style.display="none"),e&&(e.style.display="block");let c=new CustomEvent("formsuccess",{bubbles:!0});r?.dispatchEvent(c)},f=async c=>{if(c.preventDefault(),!(!r||!t)){t.value="Sending...",t.disabled=!0;try{await p(n);let l=new FormData(r),h=Array.from(l.entries()).map(([y,E])=>({formProperty:y,formValue:E})),{error:d}=await S(h,a);if(d)throw console.error("Submit error:",d),new Error("Form submission failed");w()}catch(l){console.error("Form submission error:",l),i()}}},m=()=>{r=document.getElementById(o),r?.parentElement&&(r.parentElement.classList.remove("w-form"),s=r.parentElement.querySelector('[data-name-form="error"]'),e=r.parentElement.querySelector('[data-name-form="success"]'),t=r.querySelector(".form-submit-button"),s&&(s.style.display="none"),e&&(e.style.display="none"),r.addEventListener("submit",f))};document.readyState==="loading"?document.addEventListener("DOMContentLoaded",m):m()}function L(){window.Webflow||(window.Webflow=[]),window.Webflow.push(()=>{u("contact-form","Gams","6LeINhEqAAAAAP6-YnIkE7M5M2vNSwdqlbuZb3QF")})}function C(){window.Webflow||(window.Webflow=[]),window.Webflow.push(()=>{u("newsletter-signup-form","GamsNewsletterSignUp","6LeINhEqAAAAAP6-YnIkE7M5M2vNSwdqlbuZb3QF")})}export{L as initializeGamstandardsContactForm,C as initializeGamstandardsNewsletterSignup};