Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
Fix Address Editing JS (#10602)
Matthew Kennedy1ca39588a291
diff --git a/frontend/app/assets/javascripts/spree/frontend/checkout/address.js b/frontend/app/assets/javascripts/spree/frontend/checkout/address.jsindex 77d7474af1..05298f108b 100644--- a/frontend/app/assets/javascripts/spree/frontend/checkout/address.js+++ b/frontend/app/assets/javascripts/spree/frontend/checkout/address.js@@ -1,16 +1,24 @@-Spree.ready(function ($) {- Spree.onAddress = function () {+Spree.ready(function($) {+ Spree.onAddress = function() {if ($('#checkout_form_address').length) {- Spree.updateState = function (region) {+ Spree.updateState = function(region) {var countryId = getCountryId(region)if (countryId != null) {if (Spree.Checkout[countryId] == null) {$.ajax({- async: false, method: 'GET', url: Spree.pathFor('/api/v2/storefront/countries/' + countryId + '?include=checkout_zone_applicable_states'), dataType: 'json'- }).done(function (data) {- var json = data.included; var xStates = [];+ async: false,+ method: 'GET',+ url: Spree.pathFor('/api/v2/storefront/countries/' + countryId + '?include=checkout_zone_applicable_states'),+ dataType: 'json'+ }).done(function(data) {+ var json = data.included;+ var xStates = [];for (var i = 0; i < json.length; i++) {- var obj = json[i]; xStates.push({ 'id': obj.id, 'name': obj.attributes.name })+ var obj = json[i];+ xStates.push({+ id: obj.id,+ name: obj.attributes.name+ })}Spree.Checkout[countryId] = {states: xStates,@@ -27,7 +35,7 @@ Spree.ready(function ($) {}}- Spree.toggleZipcode = function (data, region) {+ Spree.toggleZipcode = function(data, region) {var requiredIndicator = $('span#required_marker').first().text()var zipcodeRequired = data.zipcode_requiredvar zipcodePara = $('#' + region + 'zipcode')@@ -49,7 +57,7 @@ Spree.ready(function ($) {}}- Spree.fillStates = function (data, region) {+ Spree.fillStates = function(data, region) {var selectedvar statesRequired = data.states_requiredvar states = data.states@@ -63,64 +71,72 @@ Spree.ready(function ($) {if (states.length > 0) {selected = parseInt(stateSelect.val())stateSelect.html('')- $.each(states, function (idx, state) {+ $.each(states, function(idx, state) {var opt = $(document.createElement('option')).attr('value', state.id).html(state.name)if (selected.toString(10) === state.id.toString(10)) {opt.prop('selected', true)}stateSelect.append(opt)})- stateSelect.prop('required', false)- stateSelect.prop('disabled', false).show()- stateLabel.addClass('state-select-label')- stateInput.hide().prop('disabled', true)- statePara.show()- stateSpanRequired.hide()- stateSelect.removeClass('required')+ // If States are listed for the Country selected kill the input field+ stateInput.hide()+ .prop('disabled', true)+ .prop('required', false)+ .val('')- if (statesRequired) {- stateSelect.addClass('required')- stateSelectImg.show()- stateSpanRequired.show()- stateSelect.prop('required', true)- }- stateSelect.removeClass('hidden')- stateInput.removeClass('required')+ // Activate the State select dropdown.+ statePara.show()+ stateSelect.prop('required', statesRequired)+ .prop('disabled', false)+ .show()+ stateSelectImg.show()+ stateLabel.addClass('state-select-label')+ stateSpanRequired.toggle(statesRequired)} else {- stateSelect.hide().prop('disabled', true)- stateLabel.removeClass('state-select-label')+ // If no States are listed in the database for the country selected+ // and a State is not required => (United Kingdom).+ // Kill the State selector and input field.stateSelectImg.hide()- stateInput.show()+ stateSelect.hide()+ .prop('disabled', true)+ .prop('required', false)+ .find('option').remove()++ stateInput.prop('disabled', true)+ .prop('required', false)+ .hide()++ // Toggle visibility of States parent element based on State required.+ statePara.toggle(statesRequired)+if (statesRequired) {+ // If a State is required, but none are listed in the database+ // for the country selected => (Hong Kong)+ // Enable the State input field, set it to required.+ stateInput.show()+ .prop('disabled', false)+ .prop('required', true)stateSpanRequired.show()- stateLabel.removeClass('state-select-label')- stateInput.addClass('required form-control')- } else {- stateInput.val('')- stateSpanRequired.hide()- stateInput.removeClass('required')+ stateLabel.removeClass('state-select-label') // required for floating label}- statePara.toggle(!!statesRequired)- stateInput.prop('disabled', !statesRequired)- stateInput.removeClass('hidden')- stateSelect.removeClass('required')}}- $('#bcountry select').change(function () {+ $('#bcountry select').change(function() {Spree.updateState('b')})- $('#scountry select').change(function () {+ $('#scountry select').change(function() {Spree.updateState('s')})Spree.updateState('b')var orderUseBilling = $('input#order_use_billing')- orderUseBilling.change(function () {+ orderUseBilling.change(function() {updateShippingFormState(orderUseBilling)})updateShippingFormState(orderUseBilling)}- function updateShippingFormState (orderUseBilling) {++ function updateShippingFormState(orderUseBilling) {if (orderUseBilling.is(':checked')) {$('#shipping .inner').hide()$('#shipping .inner input, #shipping .inner select').prop('disabled', true)@@ -130,7 +146,8 @@ Spree.ready(function ($) {Spree.updateState('s')}}- function getCountryId (region) {++ function getCountryId(region) {return $('#' + region + 'country select').val()}}diff --git a/frontend/app/helpers/spree/addresses_helper.rb b/frontend/app/helpers/spree/addresses_helper.rbindex 59f19b7836..88d51416d7 100644--- a/frontend/app/helpers/spree/addresses_helper.rb+++ b/frontend/app/helpers/spree/addresses_helper.rb@@ -10,7 +10,7 @@ module Spreemethod_name = I18n.t("activerecord.attributes.spree/address.#{method}")required = Spree.t(:required)form.text_field(method,- class: [is_required ? 'required' : nil, 'spree-flat-input'].compact,+ class: ['spree-flat-input'].compact,required: is_required,placeholder: is_required ? "#{method_name} #{required}" : method_name,aria: { label: method_name }) +@@ -27,7 +27,7 @@ module Spreemethod_name = Spree.t(:zipcode)required = Spree.t(:required)form.text_field(:zipcode,- class: [is_required ? 'required' : nil, 'spree-flat-input'].compact,+ class: ['spree-flat-input'].compact,required: is_required,placeholder: is_required ? "#{method_name} #{required}" : method_name,aria: { label: Spree.t(:zipcode) }) +@@ -44,11 +44,11 @@ module Spreeform.collection_select(:state_id, checkout_zone_applicable_states_for(country).sort_by(&:name),:id, :name,{ prompt: Spree.t(:state) },- class: [have_states ? 'required' : 'hidden', 'spree-flat-select'].compact,+ class: ['spree-flat-select'].compact,aria: { label: Spree.t(:state) },disabled: !have_states) +form.text_field(:state_name,- class: [!have_states ? 'required' : 'hidden', 'spree-flat-input'].compact,+ class: ['spree-flat-input'].compact,aria: { label: Spree.t(:state) },disabled: have_states,placeholder: Spree.t(:state) + " #{Spree.t(:required)}") +@@ -60,7 +60,7 @@ module Spreeclass: [!have_states ? 'hidden' : nil, 'position-absolute spree-flat-select-arrow'].compact)].join.tr('"', "'").delete("\n")- content_tag(:noscript, form.text_field(:state_name, class: 'required')) ++ content_tag(:noscript, form.text_field(:state_name)) +javascript_tag("document.write(\"<span class='d-block position-relative'>#{state_elements.html_safe}</span>\");")enddiff --git a/frontend/app/views/spree/addresses/_form.html.erb b/frontend/app/views/spree/addresses/_form.html.erbindex 8ec2a49a09..dce342fde6 100644--- a/frontend/app/views/spree/addresses/_form.html.erb+++ b/frontend/app/views/spree/addresses/_form.html.erb@@ -6,7 +6,7 @@<div id="<%= "#{address_id}country-selection" %>" class="d-block position-relative has-float-label"><%= address_form.collection_select :country_id, available_countries, :id, :name,{ prompt: Spree.t(:country).upcase },- { class: 'required form-control spree-flat-select',+ { class: 'form-control spree-flat-select',aria: { label: Spree.t(:country) } } %><%= image_tag 'arrow.svg', class: 'position-absolute spree-flat-select-arrow' %><%= address_form.label(Spree.t(:country), Spree.t(:country).upcase, class:'text-uppercase') %>diff --git a/frontend/app/views/spree/users/_address.html.erb b/frontend/app/views/spree/users/_address.html.erbindex 6f9501bd00..74a156302d 100644--- a/frontend/app/views/spree/users/_address.html.erb+++ b/frontend/app/views/spree/users/_address.html.erb@@ -9,7 +9,7 @@<%= "#{address.address1} #{address.address2}" %>,</div><div>- <%= "#{address.city}, #{address.state&.abbr} #{address.zipcode}" %>,+ <%= "#{address.city}, #{address.state_text} #{address.zipcode}" %>,</div><div><%= address.country.name %>diff --git a/frontend/spec/features/address_spec.rb b/frontend/spec/features/address_spec.rbindex b6dae2d443..52e18107f6 100644--- a/frontend/spec/features/address_spec.rb+++ b/frontend/spec/features/address_spec.rb@@ -16,7 +16,6 @@ describe 'Address', type: :feature, inaccessible: true do@zipcode_label_css = '#b_zipcode_label'end-context 'store checkout_zone', js: true dolet!(:store) { create(:store, default: true) }let!(:asia_zone) do@@ -76,7 +75,6 @@ describe 'Address', type: :feature, inaccessible: true doendend-context 'when checkout_zone is set by preference' dobefore dostore.update(checkout_zone_id: nil)@@ -123,14 +121,19 @@ describe 'Address', type: :feature, inaccessible: true doit 'shows the state input field' doselect canada.name, from: @country_css- expect(page).to have_css(@state_select_css, visible: :hidden, class: ['!required'])- expect(page).to have_css(@state_name_css, visible: true, class: ['!hidden', 'required'])- expect(page).not_to have_css("input#{@state_name_css}[disabled]")+ expect(page).to have_css(@state_select_css, filter_set: :select, disabled: true, visible: :hidden)+ expect(page).to have_css(@state_name_css, filter_set: :field, disabled: false, visible: :visible)+ expect(page).not_to have_css('label.state-select-label')+ end++ it 'removes state-select-label from label class list' do+ select canada.name, from: @country_css+ expect(page).not_to have_css('label.state-select-label')endit 'shows placeholder and label text indicating a required field' doselect canada.name, from: @country_css- find("input[placeholder='#{Spree.t(:state)} #{ Spree.t(:required)}']").set "Ontario"+ find("input[placeholder='#{Spree.t(:state)} #{Spree.t(:required)}']").set 'Ontario'expect(page).to have_css("label#{@state_label_css}", text: Spree.t(:required))endend@@ -146,8 +149,13 @@ describe 'Address', type: :feature, inaccessible: true doit 'shows the state collection selection' doselect canada.name, from: @country_css- expect(page).to have_css(@state_select_css, visible: true, class: ['!hidden', 'required'])- expect(page).to have_css(@state_name_css, visible: :hidden, class: ['!required'])+ expect(page).to have_css(@state_select_css, visible: :visible)+ expect(page).to have_css(@state_name_css, filter_set: :field, disabled: true, visible: :hidden)+ end++ it 'adds state-select-label to the lable class list' do+ select canada.name, from: @country_css+ expect(page).to have_css('label.state-select-label')endit 'shows the state required indicator in the label' do@@ -169,8 +177,8 @@ describe 'Address', type: :feature, inaccessible: true doselect canada.name, from: @country_csspage.find(@state_name_css).set('Toscana')select france.name, from: @country_css- expect(page).to have_css(@state_name_css, filter_set: :field, disabled: true, with: '', visible: :hidden, class: ['!hidden', '!required'])- expect(page).to have_css(@state_select_css, visible: :hidden, class: ['!required'])+ expect(page).to have_css(@state_name_css, filter_set: :field, disabled: true, visible: :hidden)+ expect(page).to have_css(@state_select_css, visible: :hidden)endendend@@ -191,30 +199,28 @@ describe 'Address', type: :feature, inaccessible: true doendend--context 'country that requires zipcode', js: true dolet!(:canada) { create(:country, name: 'Canada', states_required: true, iso: 'CA', zipcode_required: true) }let!(:ug) { create(:country, name: 'Uganda', states_required: false, iso: 'UG', zipcode_required: false) }- before { Spree::Config[:default_country_id] = canada.id }-before do+ Spree::Config[:default_country_id] = canada.id+add_to_cart(mug) doclick_link 'Checkout'endendit 'loads the page with the zipcode field showing required in the label and placeholder' do- expect(page).to have_css("label#{@zipcode_label_css}", text: Spree.t(:required), visible: false)- find("input[placeholder='#{Spree.t(:zipcode)} #{Spree.t(:required)}']").set "98378"+ expect(page).to have_css("label#{@zipcode_label_css}", text: Spree.t(:required), visible: :hidden)+ find("input[placeholder='#{Spree.t(:zipcode)} #{Spree.t(:required)}']").set '98378'expect(page).to have_css("label#{@zipcode_label_css}", text: Spree.t(:required))endcontext 'When the country is changed to one that does not require a zip code' doit 'the JS removes the required markers in the label and placeholder text' doselect ug.name, from: @country_css- find("input[placeholder='#{Spree.t(:zipcode)}']").set "98378"+ find("input[placeholder='#{Spree.t(:zipcode)}']").set '98378'expect(page).to have_css("label#{@zipcode_label_css}", text: Spree.t(:zipcode).upcase)endend@@ -224,24 +230,24 @@ describe 'Address', type: :feature, inaccessible: true dolet!(:canada) { create(:country, name: 'Canada', states_required: true, iso: 'CA', zipcode_required: true) }let!(:ug) { create(:country, name: 'Uganda', states_required: false, iso: 'UG', zipcode_required: false) }- before { Spree::Config[:default_country_id] = ug.id }-before do+ Spree::Config[:default_country_id] = ug.id+add_to_cart(mug) doclick_link 'Checkout'endendit 'loads the page without the zipcode field showing required in the label and placeholder' do- find("input[placeholder='#{Spree.t(:zipcode)}']").set "98378"+ find("input[placeholder='#{Spree.t(:zipcode)}']").set '98378'expect(page).to have_css("label#{@zipcode_label_css}", text: Spree.t(:zipcode).upcase)endcontext 'When the country is changed to one that does require a zip code' doit 'the JS adds the required markers in the label and placeholder text' doselect canada.name, from: @country_css- expect(page).to have_css("label#{@zipcode_label_css}", text: Spree.t(:required), visible: false)- find("input[placeholder='#{Spree.t(:zipcode)} #{Spree.t(:required)}']").set "98378"+ expect(page).to have_css("label#{@zipcode_label_css}", text: Spree.t(:required), visible: :hidden)+ find("input[placeholder='#{Spree.t(:zipcode)} #{Spree.t(:required)}']").set '98378'expect(page).to have_css("label#{@zipcode_label_css}", text: Spree.t(:required))endend