Merge pull request #2357 from luisramos0/enterprises_country_state

changed the country and state selectors to angular on enterprises admin
This commit is contained in:
Pau Pérez Fabregat
2018-06-19 09:30:24 +02:00
committed by GitHub
8 changed files with 50 additions and 15 deletions

View File

@@ -0,0 +1,18 @@
# Used in enterprise new and edit forms to reset the state when the country is changed
angular.module("admin.enterprises").controller 'countryCtrl', ($scope, availableCountries) ->
$scope.countries = availableCountries
$scope.countriesById = $scope.countries.reduce (obj, country) ->
obj[country.id] = country
obj
, {}
$scope.$watch 'Enterprise.address.country_id', (newID, oldID) ->
$scope.clearState() unless $scope.addressStateMatchesCountry()
$scope.clearState = ->
$scope.Enterprise.address.state_id = null
$scope.addressStateMatchesCountry = ->
$scope.countriesById[$scope.Enterprise.address.country_id].states.some (state) ->
state.id == $scope.Enterprise.address.state_id

View File

@@ -0,0 +1,5 @@
angular.module("admin.enterprises").controller 'NewEnterpriseController', ($scope, defaultCountryID) ->
$scope.Enterprise =
address:
country_id: defaultCountryID
state_id: null

View File

@@ -8,6 +8,7 @@ class Api::Admin::EnterpriseSerializer < ActiveModel::Serializer
has_one :owner, serializer: Api::Admin::UserSerializer
has_many :users, serializer: Api::Admin::UserSerializer
has_one :address, serializer: Api::AddressSerializer
def tag_groups
object.tag_rules.prioritised.reject(&:is_default).each_with_object([]) do |tag_rule, tag_groups|

View File

@@ -3,3 +3,4 @@
= admin_inject_payment_methods
= admin_inject_shipping_methods
= admin_inject_enterprise_permissions
= admin_inject_available_countries(module: 'admin.enterprises')

View File

@@ -87,15 +87,17 @@
= af.text_field :city, { placeholder: t(:city_placeholder)}
.five.columns.omega
= af.text_field :zipcode, { placeholder: t(:postcode_placeholder)}
.row
.three.columns.alpha
= af.label :state_id, t(:state)
\/
= af.label :country_id, t(:country)
.four.columns
= af.collection_select :state_id, af.object.country.states, :id, :name, {}, :class => "select2 fullwidth"
.five.columns.omega
= af.collection_select :country_id, available_countries, :id, :name, {}, :class => "select2 fullwidth"
%div{ "ng-controller" => "countryCtrl" }
.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' } }
.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' } }
.row
.twelve.columns.alpha
&nbsp;

View File

@@ -31,7 +31,8 @@
\/
= af.label :country_id, t(:country)
%span.required *
.four.columns
= af.collection_select :state_id, af.object.country.states, :id, :name, {}, :class => "select2 fullwidth"
.four.columns.omega
= af.collection_select :country_id, available_countries, :id, :name, {}, :class => "select2 fullwidth"
%div{ "ng-controller" => "countryCtrl" }
.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' } }
.four.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' } }

View File

@@ -6,10 +6,15 @@
- content_for :page_actions do
%li= button_link_to t('.back_link'), main_app.admin_enterprises_path, icon: 'icon-arrow-left'
- content_for :app_wrapper_attrs do
= "ng-app='admin.enterprises'"
= admin_inject_available_countries(module: 'admin.enterprises')
= admin_inject_json "admin.enterprises", "defaultCountryID", Spree::Config[:default_country_id]
-# Form
= form_for [main_app, :admin, @enterprise], html: { "nav-check" => '', "nav-callback" => '' } do |f|
.row
.twelve.columns.fullwidth_inputs{ ng: { app: "admin.users" } }
.twelve.columns.fullwidth_inputs{ ng: { controller: "NewEnterpriseController" } }
= render 'new_form', f: f

View File

@@ -53,7 +53,7 @@ feature %q{
fill_in 'enterprise_address_attributes_address1', :with => '35 Ballantyne St'
fill_in 'enterprise_address_attributes_city', :with => 'Thornbury'
fill_in 'enterprise_address_attributes_zipcode', :with => '3072'
select2_search 'Australia', :from => 'Country'
# default country (Australia in this test) should be selected by default
select2_search 'Victoria', :from => 'State'
click_button 'Create'
@@ -342,6 +342,8 @@ feature %q{
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'
end
scenario "without violating rules" do