From 09e2c6949363959fb5161e4b5c2280bb2aa5ac8b Mon Sep 17 00:00:00 2001 From: pacnpal <183241239+pacnpal@users.noreply.github.com> Date: Fri, 26 Sep 2025 14:34:59 -0400 Subject: [PATCH] Refactor designer, manufacturer, and ride model forms to utilize Alpine.js for state management. Improved form submission handling, HTMX event integration, and enhanced user experience through better event dispatching and modal management. --- templates/rides/partials/designer_form.html | 81 ++++++---- .../rides/partials/manufacturer_form.html | 81 ++++++---- templates/rides/partials/ride_model_form.html | 138 ++++++++++++------ 3 files changed, 206 insertions(+), 94 deletions(-) diff --git a/templates/rides/partials/designer_form.html b/templates/rides/partials/designer_form.html index 38a1faaa..1fc04183 100644 --- a/templates/rides/partials/designer_form.html +++ b/templates/rides/partials/designer_form.html @@ -1,35 +1,66 @@ {% load static %} -
+ if (event.detail.xhr.status >= 200 && event.detail.xhr.status < 300) { + const data = JSON.parse(event.detail.xhr.response); + + // Dispatch event with designer data for parent components + this.$dispatch('designer-created', { + id: data.id, + name: data.name + }); + + // Close modal if in modal context + this.$dispatch('close-designer-modal'); + } else { + // Handle error case + this.$dispatch('designer-creation-error', { + error: event.detail.xhr.responseText + }); + } + } + })); +}); + + + {% csrf_token %}
diff --git a/templates/rides/partials/manufacturer_form.html b/templates/rides/partials/manufacturer_form.html index f665a759..1a809ba4 100644 --- a/templates/rides/partials/manufacturer_form.html +++ b/templates/rides/partials/manufacturer_form.html @@ -1,35 +1,66 @@ {% load static %} - + if (event.detail.xhr.status >= 200 && event.detail.xhr.status < 300) { + const data = JSON.parse(event.detail.xhr.response); + + // Dispatch event with manufacturer data for parent components + this.$dispatch('manufacturer-created', { + id: data.id, + name: data.name + }); + + // Close modal if in modal context + this.$dispatch('close-manufacturer-modal'); + } else { + // Handle error case + this.$dispatch('manufacturer-creation-error', { + error: event.detail.xhr.responseText + }); + } + } + })); +}); + + + {% csrf_token %}
diff --git a/templates/rides/partials/ride_model_form.html b/templates/rides/partials/ride_model_form.html index 9b1049d7..d8a6073c 100644 --- a/templates/rides/partials/ride_model_form.html +++ b/templates/rides/partials/ride_model_form.html @@ -1,53 +1,103 @@ {% load static %} - + }, + + async submitForm(event) { + if (this.submitting) return; + + this.submitting = true; + const formData = new FormData(event.target); + const csrfToken = this.$el.querySelector('[name=csrfmiddlewaretoken]').value; + + // Use HTMX for form submission + htmx.ajax('POST', '/rides/models/create/', { + values: Object.fromEntries(formData), + headers: { + 'X-CSRFToken': csrfToken + }, + target: this.$el, + swap: 'none' + }); + }, + + handleResponse(event) { + this.submitting = false; + + if (event.detail.xhr.status >= 200 && event.detail.xhr.status < 300) { + const data = JSON.parse(event.detail.xhr.response); + + // Dispatch event with ride model data for parent components + this.$dispatch('ride-model-created', { + id: data.id, + name: data.name + }); + + // Close modal if in modal context + this.$dispatch('close-ride-model-modal'); + } else { + // Handle error case + this.$dispatch('ride-model-creation-error', { + error: event.detail.xhr.responseText + }); + } + }, + + selectManufacturer(manufacturerId, manufacturerName) { + // Update manufacturer fields using AlpineJS reactive approach + const manufacturerInput = this.$el.querySelector('#id_manufacturer'); + const searchInput = this.$el.querySelector('#id_manufacturer_search'); + const resultsDiv = this.$el.querySelector('#manufacturer-search-results'); + + if (manufacturerInput) manufacturerInput.value = manufacturerId; + if (searchInput) searchInput.value = manufacturerName; + if (resultsDiv) resultsDiv.innerHTML = ''; + }, + + clearManufacturerResults() { + const resultsDiv = this.$el.querySelector('#manufacturer-search-results'); + if (resultsDiv) resultsDiv.innerHTML = ''; + } + })); +}); + + + {% csrf_token %}