Jake Vanderwerf
2 days ago 235ce5716edc2f7cbe80fdccf26eac7269587839
build/forms/view.js
@@ -1 +1 @@
(()=>{class e{constructor(){this.controller=new window.jvbForm,document.querySelectorAll(".jvb-form-block form").forEach((e=>{this.controller.registerForm(e)})),this.controller.subscribe(((e,o)=>{"form-submit"===e&&this.handleFormSubmission(o)}))}async handleFormSubmission(e){let[o,t,r]=[e.formId,e.config,e.data],s=t.element,n={"X-WP-Nonce":jvbSettings.nonce,"Content-Type":"application/json"};e.form_type=o,e.form_id=s.id,s.closest(".jvb-form-block"),this.controller.showFormStatus(o,"uploading");try{const e=await fetch(`${jvbSettings.api}forms`,{method:"POST",headers:n,body:JSON.stringify(r)});if(!e.ok){this.controller.showFormStatus(o,"error");const t=await e.json().catch((()=>({})));throw new Error(t.message||`Request failed with status ${e.status}`)}this.controller.showFormStatus(o,"submitted"),this.controller.showSummary(o,".jvb-form-block"),this.controller.store.delete(o)}catch(e){throw e}}updateUI(e,o){let t=window.getTemplate("formSummary");t.querySelector("h2").textContent="Success!",console.log("Form Response: ",e),console.log(t);for(let[o,r]of Object.entries(e)){let e=t.querySelector(`#${o}`);if(e){let o=e.querySelector("h4");o.innerText.includes("%s")?o.innerHTML=o.replace("%s","<b>"+r+"</b>"):e.querySelector("div").innerHTML=r}}o.append(t)}}document.addEventListener("DOMContentLoaded",(function(){new e}))})();
(()=>{class o{constructor(){this.controller=window.jvbForm,document.querySelectorAll(".jvb-form-block form").forEach(o=>{this.controller.registerForm(o,{cache:!0,autoUpload:!1,imageMeta:!1})}),this.controller.subscribe((o,e)=>{"form-submit"===o&&this.handleFormSubmission(e).then(()=>{})})}async handleFormSubmission(o){const{config:e,data:r}=o,t=e.element,n=new FormData;for(const[o,e]of Object.entries(r))Array.isArray(e)?e.forEach(e=>n.append(`${o}[]`,e)):"object"==typeof e&&null!==e?n.append(o,JSON.stringify(e)):n.append(o,e);if(e.element.querySelectorAll('[name="form_id"],[name="form_type"],[name="timestamp"],[name="cf-turnstile-response"]').forEach(o=>{n.append(o.name,o.value)}),window.jvbUploads)try{(await window.jvbUploads.getFilesForForm(t)).forEach(({file:o,fieldName:e})=>{n.append(`${e}[]`,o)})}catch(o){console.error("Error getting files:",o)}this.controller.showFormStatus(e.id,"uploading");try{const o=await fetch(`${jvbSettings.api}forms`,{method:"POST",credentials:"same-origin",body:n}),a=await o.json();if(!o.ok)return this.controller.showFormStatus(e.id,"error"),void this.controller.handleFormError(t,a);if(this.controller.showFormStatus(e.id,"submitted"),this.controller.showSummary({changes:r,config:e}),window.jvbA11y.announce("Form successfully submitted!"),window.jvbUploads){const o=t.querySelectorAll("[data-upload-field]");for(const e of o){const o=window.jvbUploads.determineFieldId(e);await window.jvbUploads.clearFieldFromStores(o)}}}catch(o){console.error("Form submission error:",o),this.controller.showFormStatus(e.id,"error"),this.controller.handleFormError(t,{message:"Network error. Please check your connection and try again.",code:"network_error"})}finally{await this.controller.store.delete(e.id)}}}document.addEventListener("DOMContentLoaded",async function(){window.auth.subscribe(e=>{"auth-loaded"===e&&new o})})})();