From 6030d7e05b529ea8353669de10cd9810bf58c77a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Turbelin?= Date: Mon, 25 Nov 2024 23:10:50 +0100 Subject: [PATCH] Handle the async action for entreprises sells field changes --- app/views/admin/enterprises/_form.html.haml | 7 ++++- .../enterprises/form/_admin_only.html.haml | 11 ++++--- .../form/_primary_details.html.haml | 2 -- .../controllers/admin_only_controller.js | 30 +++++++++++++++++++ .../controllers/primary_details_controller.js | 12 ++------ 5 files changed, 45 insertions(+), 17 deletions(-) create mode 100644 app/webpacker/controllers/admin_only_controller.js diff --git a/app/views/admin/enterprises/_form.html.haml b/app/views/admin/enterprises/_form.html.haml index 71a9b2378b..0284abb32b 100644 --- a/app/views/admin/enterprises/_form.html.haml +++ b/app/views/admin/enterprises/_form.html.haml @@ -1,7 +1,12 @@ - enterprise_side_menu_items(@enterprise).each do |item| - case item[:name] - when 'primary_details' - %fieldset.alpha.no-border-bottom{ id: "#{item[:name]}_panel", data: { controller: "primary-details", "primary-details-primary-producer-value": @enterprise.is_primary_producer.to_s, "primary-details-enterprise-sells-value": @enterprise.sells, "tabs-and-panels-target": "panel default" }} + %fieldset.alpha.no-border-bottom{ id: "#{item[:name]}_panel", data: { controller: "primary-details", "primary-details-primary-producer-value": @enterprise.is_primary_producer.to_s, "tabs-and-panels-target": "panel default" }} + %legend= t(".#{ item[:name] }.legend") + = render "admin/enterprises/form/#{ item[:form_name] || item[:name] }", f: f + + - when 'admin_only' + %fieldset.alpha.no-border-bottom{ id: "#{item[:name]}_panel", data: { controller: "admin-only", "admin-only-enterprise-sells-value": @enterprise.sells, "tabs-and-panels-target": "panel admin-only" }} %legend= t(".#{ item[:name] }.legend") = render "admin/enterprises/form/#{ item[:form_name] || item[:name] }", f: f diff --git a/app/views/admin/enterprises/form/_admin_only.html.haml b/app/views/admin/enterprises/form/_admin_only.html.haml index 0d8033bcf1..646aac8951 100644 --- a/app/views/admin/enterprises/form/_admin_only.html.haml +++ b/app/views/admin/enterprises/form/_admin_only.html.haml @@ -3,16 +3,19 @@ = f.label :sells, t('.sells') = render partial: 'admin/shared/whats_this_tooltip', locals: {tooltip_text: t('.sells_tip')} .two.columns - = f.radio_button :sells, "none", 'ng-model' => 'Enterprise.sells', data: {action: "change->primary-details#enterpriseSellsChanged"} + = f.radio_button :sells, "none", 'ng-model' => 'Enterprise.sells', data: { action: "change->admin-only#enterpriseSellsChanged"} = f.label :sells, t('.none'), value: "none" .two.columns - = f.radio_button :sells, "own", 'ng-model' => 'Enterprise.sells', data: {action: "change->primary-details#enterpriseSellsChanged"} + = f.radio_button :sells, "own", 'ng-model' => 'Enterprise.sells', data: { action: "change->admin-only#enterpriseSellsChanged" } = f.label :sells, t('.own'), value: "own" .four.columns.omega - = f.radio_button :sells, "any", 'ng-model' => 'Enterprise.sells', data: {action: "change->primary-details#enterpriseSellsChanged"} + = f.radio_button :sells, "any", 'ng-model' => 'Enterprise.sells', data: { action: "change->admin-only#enterpriseSellsChanged" } = f.label :sells, t('.any'), value: "any" - %span{ style: "width: 30px; height: 30px;", class: "hidden", data: { "primary-details-target": "spinner" } } + %span{ style: "width: 30px; height: 30px;", class: "hidden", data: { "admin-only-target": "spinner" } } = render partial: "components/admin_spinner" + += render partial: 'admin/enterprises/form/permalink' + .row .three.columns.alpha = f.label :external_billing_id, t('.external_billing_id') diff --git a/app/views/admin/enterprises/form/_primary_details.html.haml b/app/views/admin/enterprises/form/_primary_details.html.haml index 27965fb421..297263f263 100644 --- a/app/views/admin/enterprises/form/_primary_details.html.haml +++ b/app/views/admin/enterprises/form/_primary_details.html.haml @@ -31,5 +31,3 @@ .four.columns.omega = f.radio_button :visible, "hidden", 'ng-model' => 'Enterprise.visible' = f.label :visible, t('.hidden'), value: 'hidden' - -= render partial: 'admin/enterprises/form/permalink' diff --git a/app/webpacker/controllers/admin_only_controller.js b/app/webpacker/controllers/admin_only_controller.js new file mode 100644 index 0000000000..beb5eae2e4 --- /dev/null +++ b/app/webpacker/controllers/admin_only_controller.js @@ -0,0 +1,30 @@ +import { Controller } from "stimulus"; + +export default class extends Controller { + static values = { enterpriseSells: String }; + static targets = ["spinner"]; + + enterpriseSellsChanged(event) { + console.log("enterpriseSellsChanged"); + if (event.currentTarget.checked) { + this.enterpriseSellsValue = event.currentTarget.value; + this.spinnerTarget.classList.remove("hidden"); + this.makeRequest(); + } + } + + makeRequest() { + fetch( + `?stimulus=true&enterprise_sells=${this.enterpriseSellsValue}`, + { + method: "GET", + headers: { "Content-type": "application/json; charset=UTF-8" }, + } + ) + .then((data) => data.json()) + .then((operation) => { + CableReady.perform(operation); + this.spinnerTarget.classList.add("hidden"); + }); + } +} diff --git a/app/webpacker/controllers/primary_details_controller.js b/app/webpacker/controllers/primary_details_controller.js index b4e72be4cf..59443d2669 100644 --- a/app/webpacker/controllers/primary_details_controller.js +++ b/app/webpacker/controllers/primary_details_controller.js @@ -1,7 +1,7 @@ import { Controller } from "stimulus"; export default class extends Controller { - static values = { primaryProducer: String, enterpriseSells: String }; + static values = { primaryProducer: String }; static targets = ["spinner"]; primaryProducerChanged(event) { @@ -9,17 +9,9 @@ export default class extends Controller { this.makeRequest(); } - enterpriseSellsChanged(event) { - if (event.currentTarget.checked) { - this.enterpriseSellsValue = event.currentTarget.value; - this.spinnerTarget.classList.remove("hidden"); - this.makeRequest(); - } - } - makeRequest() { fetch( - `?stimulus=true&enterprise_sells=${this.enterpriseSellsValue}&is_primary_producer=${this.primaryProducerValue}`, + `?stimulus=true&is_primary_producer=${this.primaryProducerValue}`, { method: "GET", headers: { "Content-type": "application/json; charset=UTF-8" },