From 0cfe842bd6f664489074db9975db63db9cf38d45 Mon Sep 17 00:00:00 2001 From: binarygit Date: Fri, 2 Sep 2022 14:39:49 +0545 Subject: [PATCH 1/3] Fix typo: dependant to dependent in dependent-select controller --- .../admin/orders/customer_details/_address_form.html.haml | 6 +++--- ..._select_controller.js => dependent_select_controller.js} | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) rename app/webpacker/controllers/{dependant_select_controller.js => dependent_select_controller.js} (91%) diff --git a/app/views/spree/admin/orders/customer_details/_address_form.html.haml b/app/views/spree/admin/orders/customer_details/_address_form.html.haml index c64a70aac7..1e3e2f9865 100644 --- a/app/views/spree/admin/orders/customer_details/_address_form.html.haml +++ b/app/views/spree/admin/orders/customer_details/_address_form.html.haml @@ -29,17 +29,17 @@ %div{class: "field"} = f.label :zipcode, Spree.t(:zip) + ':' = f.text_field :zipcode, class: 'fullwidth' - %div{"data-controller": "dependant-select", "data-dependant-select-options-value": countries_with_states } + %div{"data-controller": "dependent-select", "data-dependent-select-options-value": countries_with_states } %div{class: "field"} = f.label :country_id, Spree.t(:country) + ':' %span{id: "#{s_or_b}country"} = f.select :country_id, countries, { selected: @order.send("#{address_type}_address").country_id }, - { "data-controller": "tom-select", "data-dependant-select-target": "source", "data-action": "dependant-select#handleSelectChange", class: "primary" } + { "data-controller": "tom-select", "data-dependent-select-target": "source", "data-action": "dependent-select#handleSelectChange", class: "primary" } %div{class: "field"} = f.label :state_id, Spree.t(:state) + ':' %span{id: "#{s_or_b}state"} = f.select :state_id, states_for_country(@order.send("#{address_type}_address").country), { selected: @order.send("#{address_type}_address").state_id }, - { "data-controller": "tom-select", "data-dependant-select-target": "select", class: "primary" } + { "data-controller": "tom-select", "data-dependent-select-target": "select", class: "primary" } %div{class: "field"} = f.label :phone, Spree.t(:phone) + ':' diff --git a/app/webpacker/controllers/dependant_select_controller.js b/app/webpacker/controllers/dependent_select_controller.js similarity index 91% rename from app/webpacker/controllers/dependant_select_controller.js rename to app/webpacker/controllers/dependent_select_controller.js index 577f11c951..bf99f2c6b4 100644 --- a/app/webpacker/controllers/dependant_select_controller.js +++ b/app/webpacker/controllers/dependent_select_controller.js @@ -23,7 +23,7 @@ export default class extends Controller { } populateNewOptions(sourceId) { - const options = this.dependantOptionsFor(sourceId); + const options = this.dependentOptionsFor(sourceId); options.forEach((item) => { this.addOption(item[0], item[1]); @@ -40,7 +40,7 @@ export default class extends Controller { this.selectTarget.appendChild(newOption); } - dependantOptionsFor(sourceId) { + dependentOptionsFor(sourceId) { return this.optionsValue.find((option) => option[0] === sourceId)[1]; } } From 36b5cb3b49ec038297a393809e7759fe7049ae30 Mon Sep 17 00:00:00 2001 From: binarygit Date: Fri, 2 Sep 2022 14:41:35 +0545 Subject: [PATCH 2/3] Replace country controller with dependent select in address tab --- app/views/admin/enterprises/form/_address.html.haml | 10 +++++----- spec/system/admin/enterprises_spec.rb | 3 ++- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/app/views/admin/enterprises/form/_address.html.haml b/app/views/admin/enterprises/form/_address.html.haml index 810953d4ea..388310523c 100644 --- a/app/views/admin/enterprises/form/_address.html.haml +++ b/app/views/admin/enterprises/form/_address.html.haml @@ -25,16 +25,16 @@ = af.text_field :city, { placeholder: t(:city_placeholder) } .four.columns.omega = af.text_field :zipcode, { placeholder: t(:postcode_placeholder) } -.row +.row{"data-controller": "dependent-select", "data-dependent-select-options-value": countries_with_states } .three.columns.alpha = af.label :state_id, t(:state) \/ = af.label :country_id, t(:country) %span.required * - .four.columns{ "ng-controller" => "countryCtrl" } - %input.ofn-select2.fullwidth#enterprise_address_attributes_state_id{ name: 'enterprise[address_attributes][state_id]', type: 'number', data: 'countriesById[Enterprise.address.country_id].states', placeholder: t('admin.choose'), ng: { model: 'Enterprise.address.state_id' } } - .four.columns.omega{ "ng-controller" => "countryCtrl" } - %input.ofn-select2.fullwidth#enterprise_address_attributes_country_id{ name: 'enterprise[address_attributes][country_id]', type: 'number', data: 'countries', placeholder: t('admin.choose'), ng: { model: 'Enterprise.address.country_id' } } + .four.columns + = af.select :state_id, @enterprise.address.country.states.map { |s| [s.name, s.id] }, {}, { "data-controller": "tom-select", "data-dependent-select-target": "select", class: "primary" } + .four.columns.omega{ data: { controller: "primary-details" }} + = af.select :country_id, available_countries.map { |c| [c.name, c.id] }, {}, { "data-controller": "tom-select", "data-dependent-select-target": "source", "data-action": "dependent-select#handleSelectChange", class: "primary" } .row .three.columns.alpha = af.label :latitude, t(:latitude) diff --git a/spec/system/admin/enterprises_spec.rb b/spec/system/admin/enterprises_spec.rb index cad13d04fb..a709fa387c 100644 --- a/spec/system/admin/enterprises_spec.rb +++ b/spec/system/admin/enterprises_spec.rb @@ -185,7 +185,8 @@ describe ' fill_in 'enterprise_address_attributes_latitude', with: '-37.4713077' fill_in 'enterprise_address_attributes_longitude', with: '144.7851531' # default country (Australia in this test) should be selected by default - select2_select 'Victoria', from: 'enterprise_address_attributes_state_id' + page.find("#enterprise_address_attributes_state_id-ts-control").click + page.find(".option", text: "Victoria").click accept_alert do click_link "Shop Preferences" From abbb61b943abb0c22012bdcb147f898d1b0b5bf2 Mon Sep 17 00:00:00 2001 From: binarygit Date: Fri, 2 Sep 2022 14:46:30 +0545 Subject: [PATCH 3/3] Remove country controller from enterprises/new and Business Details tab --- .../controllers/country_controller.js.coffee | 20 ------------------- .../admin/enterprises/_new_form.html.haml | 6 +++--- .../admin/enterprises/form/_address.html.haml | 3 +-- .../form/_business_address.html.haml | 7 +++---- spec/system/admin/enterprises_spec.rb | 11 +++++++--- 5 files changed, 15 insertions(+), 32 deletions(-) delete mode 100644 app/assets/javascripts/admin/enterprises/controllers/country_controller.js.coffee diff --git a/app/assets/javascripts/admin/enterprises/controllers/country_controller.js.coffee b/app/assets/javascripts/admin/enterprises/controllers/country_controller.js.coffee deleted file mode 100644 index a9aa6148bf..0000000000 --- a/app/assets/javascripts/admin/enterprises/controllers/country_controller.js.coffee +++ /dev/null @@ -1,20 +0,0 @@ -# Used in enterprise new and edit forms to reset the state when the country is changed -angular.module("admin.enterprises").controller 'countryCtrl', ($scope, $timeout, availableCountries) -> - $scope.address_type = "address" - $scope.countries = availableCountries - - $scope.countriesById = $scope.countries.reduce (obj, country) -> - obj[country.id] = country - obj - , {} - - $timeout -> - $scope.$watch 'Enterprise.' + $scope.address_type + '.country_id', (newID, oldID) -> - $scope.clearState() unless $scope.addressStateMatchesCountry() - - $scope.clearState = -> - $scope.Enterprise[$scope.address_type].state_id = null - - $scope.addressStateMatchesCountry = -> - $scope.countriesById[$scope.Enterprise[$scope.address_type].country_id].states.some (state) -> - state.id == $scope.Enterprise[$scope.address_type].state_id diff --git a/app/views/admin/enterprises/_new_form.html.haml b/app/views/admin/enterprises/_new_form.html.haml index a0b99d6508..f3526fd1f7 100644 --- a/app/views/admin/enterprises/_new_form.html.haml +++ b/app/views/admin/enterprises/_new_form.html.haml @@ -87,16 +87,16 @@ = af.text_field :city, { placeholder: t(:city_placeholder)} .five.columns.omega = af.text_field :zipcode, { placeholder: t(:postcode_placeholder)} - %div{ "ng-controller" => "countryCtrl" } + %div{"data-controller": "dependant-select", "data-dependant-select-options-value": countries_with_states } .row .three.columns.alpha = af.label :state_id, t(:state) \/ = af.label :country_id, t(:country) .four.columns - %input.ofn-select2.fullwidth#enterprise_address_attributes_state_id{ name: 'enterprise[address_attributes][state_id]', type: 'number', data: 'countriesById[Enterprise.address.country_id].states', placeholder: t('admin.choose'), ng: { model: 'Enterprise.address.state_id' } } + = af.select :country_id, available_countries.map { |c| [c.name, c.id] }, {}, { "data-controller": "tom-select", "data-dependant-select-target": "source", "data-action": "dependant-select#handleSelectChange", class: "primary" } .five.columns.omega - %input.ofn-select2.fullwidth#enterprise_address_attributes_country_id{ name: 'enterprise[address_attributes][country_id]', type: 'number', data: 'countries', placeholder: t('admin.choose'), ng: { model: 'Enterprise.address.country_id' } } + = af.select :state_id, @enterprise.address.country.states.map { |s| [s.name, s.id] }, {}, { "data-controller": "tom-select", "data-dependant-select-target": "select", class: "primary" } .row .three.columns.alpha = af.label :latitude, t(:latitude) diff --git a/app/views/admin/enterprises/form/_address.html.haml b/app/views/admin/enterprises/form/_address.html.haml index 388310523c..319d6097f1 100644 --- a/app/views/admin/enterprises/form/_address.html.haml +++ b/app/views/admin/enterprises/form/_address.html.haml @@ -41,8 +41,7 @@ \/ = af.label :longitude, t(:longitude) %span.required * - %div{'ofn-with-tip' => t('latitude_longitude_tip')} - %a= t('admin.whats_this') + = render partial: 'admin/shared/tooltip', locals: {tooltip_text: t('latitude_longitude_tip')} .four.columns = af.text_field :latitude, { placeholder: t(:latitude_placeholder) } .four.columns.omega diff --git a/app/views/admin/enterprises/form/_business_address.html.haml b/app/views/admin/enterprises/form/_business_address.html.haml index 069c838baa..44e1b93353 100644 --- a/app/views/admin/enterprises/form/_business_address.html.haml +++ b/app/views/admin/enterprises/form/_business_address.html.haml @@ -29,12 +29,11 @@ = bf.label :country_id, t(:country) \/ = bf.label :state_id, t(:state) - %div{ "ng-controller": "countryCtrl", "ng-init": "address_type='business_address'" } + %div{"data-controller": "dependant-select", "data-dependant-select-options-value": countries_with_states } .four.columns - %input.ofn-select2.fullwidth#enterprise_business_address_attributes_country_id{ name: 'enterprise[business_address_attributes][country_id]', type: 'number', data: 'countries', placeholder: t('admin.choose'), ng: { model: 'Enterprise.business_address.country_id' } } + = bf.select :country_id, available_countries.map { |c| [c.name, c.id] }, {}, { "data-controller": "tom-select", "data-dependant-select-target": "source", "data-action": "dependant-select#handleSelectChange", class: "primary" } .four.columns.omega - %input.ofn-select2.fullwidth#enterprise_business_address_attributes_state_id{ name: 'enterprise[business_address_attributes][state_id]', type: 'number', data: 'countriesById[Enterprise.address.country_id].states', placeholder: t('admin.choose'), ng: { model: 'Enterprise.business_address.state_id' } } - + = bf.select :state_id, @enterprise.address.country.states.map { |s| [s.name, s.id] }, {}, { "data-controller": "tom-select", "data-dependant-select-target": "select", class: "primary" } .row .three.columns.alpha diff --git a/spec/system/admin/enterprises_spec.rb b/spec/system/admin/enterprises_spec.rb index a709fa387c..88b8a277bd 100644 --- a/spec/system/admin/enterprises_spec.rb +++ b/spec/system/admin/enterprises_spec.rb @@ -56,7 +56,8 @@ describe ' fill_in 'enterprise_address_attributes_latitude', with: '-37.4713077' fill_in 'enterprise_address_attributes_longitude', with: '144.7851531' # default country (Australia in this test) should be selected by default - select2_select 'Victoria', from: 'enterprise_address_attributes_state_id' + page.find("#enterprise_address_attributes_country_id-ts-control").click + page.find(".option", text: "Australia").click click_button 'Create' expect(flash_message).to eq('Enterprise "Eaterprises" has been successfully created!') @@ -355,8 +356,12 @@ describe ' fill_in 'enterprise_address_attributes_address1', with: 'z' fill_in 'enterprise_address_attributes_city', with: 'z' fill_in 'enterprise_address_attributes_zipcode', with: 'z' - select2_select 'Australia', from: 'enterprise_address_attributes_country_id' - select2_select 'Victoria', from: 'enterprise_address_attributes_state_id' + + page.find("#enterprise_address_attributes_country_id-ts-control").click + page.find(".option", text: "Australia").click + + page.find("#enterprise_address_attributes_state_id-ts-control").click + page.find(".option", text: "Victoria").click end it "without violating rules" do