Handle the async action for entreprises sells field changes

This commit is contained in:
François Turbelin
2024-11-25 23:10:50 +01:00
committed by Rachel Arnould
parent 30dfae7e18
commit 6030d7e05b
5 changed files with 45 additions and 17 deletions

View File

@@ -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

View File

@@ -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')

View File

@@ -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'

View File

@@ -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");
});
}
}

View File

@@ -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" },