Jake Vanderwerf
4 days ago 0afb2c0046b55c123eafb4ab9ee77efa68d12463
src/forms/view.js
@@ -9,32 +9,31 @@
 */
class FormBlock {
   constructor() {
      this.controller = new window.jvbForm();
      this.controller = window.jvbForm;
      document.querySelectorAll('.jvb-form-block form').forEach(form => {
         this.controller.registerForm(form, {
            autosave: true,
            autoUpload: false
            cache: true,
            autoUpload: false,
            imageMeta: false,
         });
      });
      this.controller.subscribe((event, data) => {
         if (event === 'form-submit') {
            this.handleFormSubmission(data);
            this.handleFormSubmission(data).then(()=>{});
         }
      });
   }
   async handleFormSubmission(data) {
      const { formId, config: formConfig, fullData: formData } = data;
      const form = formConfig.element;
   async handleFormSubmission(eventData) {
      const { config, data } = eventData;
      const form = config.element;
      const submitData = new FormData();
      // Add regular form fields
      for (const [key, value] of Object.entries(formData)) {
         if (key === '_wpnonce' || key === '_wp_http_referer') continue;
      for (const [key, value] of Object.entries(data)) {
         if (Array.isArray(value)) {
            value.forEach(v => submitData.append(`${key}[]`, v));
         } else if (typeof value === 'object' && value !== null) {
@@ -43,11 +42,15 @@
            submitData.append(key, value);
         }
      }
      config.element.querySelectorAll('[name="form_id"],[name="form_type"],[name="timestamp"],[name="cf-turnstile-response"]').forEach(input => {
         submitData.append(input.name, input.value);
      });
      // Add uploaded files
      if (window.jvbUploads) {
         try {
            const files = await window.jvbUploads.getFilesForForm(form);
            files.forEach(({ file, fieldName }) => {
               submitData.append(`${fieldName}[]`, file);
            });
@@ -56,7 +59,7 @@
         }
      }
      this.controller.showFormStatus(formId, 'uploading');
      this.controller.showFormStatus(config.id, 'uploading');
      try {
         const response = await fetch(`${jvbSettings.api}forms`, {
@@ -68,13 +71,15 @@
         const result = await response.json();
         if (!response.ok) {
            this.controller.showFormStatus(formId, 'error');
            this.controller.showFormStatus(config.id, 'error');
            this.controller.handleFormError(form, result);
            return;
         }
         this.controller.showFormStatus(formId, 'submitted');
         this.controller.showSummary(formId, '.jvb-form-block');
         this.controller.showFormStatus(config.id, 'submitted');
         // this.controller.handleFormSuccess(form, result);
         this.controller.showSummary({ changes: data, config: config });
         window.jvbA11y.announce('Form successfully submitted!');
         // Clean up uploaded files
         if (window.jvbUploads) {
@@ -87,13 +92,13 @@
      } catch (error) {
         console.error('Form submission error:', error);
         this.controller.showFormStatus(formId, 'error');
         this.controller.showFormStatus(config.id, 'error');
         this.controller.handleFormError(form, {
            message: 'Network error. Please check your connection and try again.',
            code: 'network_error'
         });
      } finally {
         await this.controller.store.delete(formId);
         await this.controller.store.delete(config.id);
      }
   }
}