| | |
| | | */ |
| | | 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 |
| | | autoUpload: false, |
| | | showSummary: true, |
| | | }); |
| | | }); |
| | | |
| | | 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)) { |
| | | for (const [key, value] of Object.entries(data)) { |
| | | if (key === '_wpnonce' || key === '_wp_http_referer') continue; |
| | | |
| | | if (Array.isArray(value)) { |
| | |
| | | } |
| | | } |
| | | |
| | | this.controller.showFormStatus(formId, 'uploading'); |
| | | this.controller.showFormStatus(config.id, 'uploading'); |
| | | |
| | | try { |
| | | const response = await fetch(`${jvbSettings.api}forms`, { |
| | |
| | | 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.showSummary(config.id, '.jvb-form-block'); |
| | | |
| | | // Clean up uploaded files |
| | | if (window.jvbUploads) { |
| | |
| | | |
| | | } 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); |
| | | } |
| | | } |
| | | } |