From d9737130529e506a0442892250fdfa4cefac1ff8 Mon Sep 17 00:00:00 2001 From: Rainer Dema Date: Tue, 7 Nov 2023 16:09:43 +0100 Subject: [PATCH] Disable state field for countries without states Modify the address form logic to accommodate countries without associated states. The state select field is now automatically disabled when a country with no states is selected. Additionally, this ensure state select field is disabled for preloaded selected country without states. --- .../solidus_admin/orders/show/component.rb | 2 +- .../ui/forms/address/component.html.erb | 1 + .../solidus_admin/ui/forms/address/component.js | 17 +++++++++++------ 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/admin/app/components/solidus_admin/orders/show/component.rb b/admin/app/components/solidus_admin/orders/show/component.rb index 02718d07761..3300f13c719 100644 --- a/admin/app/components/solidus_admin/orders/show/component.rb +++ b/admin/app/components/solidus_admin/orders/show/component.rb @@ -21,7 +21,7 @@ def format_address(address) address.address2, address.city, address.zipcode, - address.state.name, + address.state&.name, tag.br, address.country.name, tag.br, diff --git a/admin/app/components/solidus_admin/ui/forms/address/component.html.erb b/admin/app/components/solidus_admin/ui/forms/address/component.html.erb index 5a2a18c0691..e4c66ad6057 100644 --- a/admin/app/components/solidus_admin/ui/forms/address/component.html.erb +++ b/admin/app/components/solidus_admin/ui/forms/address/component.html.erb @@ -25,6 +25,7 @@ :state_id, state_options, value: @form.object.try(:state_id), + disabled: @form.object.country&.states&.empty?, "data-#{stimulus_id}-target": "state" ) %> diff --git a/admin/app/components/solidus_admin/ui/forms/address/component.js b/admin/app/components/solidus_admin/ui/forms/address/component.js index acded52e2b9..1b255ee39a4 100644 --- a/admin/app/components/solidus_admin/ui/forms/address/component.js +++ b/admin/app/components/solidus_admin/ui/forms/address/component.js @@ -18,12 +18,17 @@ export default class extends Controller { stateSelect.innerHTML = "" - data.forEach(state => { - const option = document.createElement("option") + if (data.length === 0) { + stateSelect.disabled = true + } else { + stateSelect.disabled = false - option.value = state.id - option.innerText = state.name - stateSelect.appendChild(option) - }) + data.forEach((state) => { + const option = document.createElement("option") + option.value = state.id + option.innerText = state.name + stateSelect.appendChild(option) + }) + } } }