Extract countryStates logic from customer_address modal and re-use new CountryStates service

This commit is contained in:
luisramos0
2018-10-16 22:45:38 +01:00
parent 5c5a2194d6
commit 1804bf5a2b
3 changed files with 10 additions and 14 deletions

View File

@@ -1 +1 @@
angular.module("admin.customers", ['ngResource', 'admin.tagRules', 'admin.indexUtils', 'admin.utils', 'admin.dropdown'])
angular.module("admin.customers", ['ngResource', 'admin.tagRules', 'admin.indexUtils', 'admin.utils', 'admin.dropdown', 'ofn.admin'])

View File

@@ -1,4 +1,4 @@
angular.module("admin.customers").directive 'editAddressDialog', ($compile, $templateCache, $filter, DialogDefaults, Customers, StatusMessage) ->
angular.module("admin.customers").directive 'editAddressDialog', ($compile, $templateCache, DialogDefaults, Customers, StatusMessage, CountryStates) ->
restrict: 'A'
scope: true
link: (scope, element, attr) ->
@@ -6,9 +6,9 @@ angular.module("admin.customers").directive 'editAddressDialog', ($compile, $tem
scope.errors = []
scope.$watch 'address.country_id', (newCountryID) ->
if newCountryID
scope.states = scope.filterStates(newCountryID)
scope.clearState() unless scope.addressStateMatchesCountry()
return unless newCountryID
scope.states = CountryStates.statesFor(scope.availableCountries, newCountryID)
scope.clearState() unless CountryStates.addressStateMatchesCountryStates(scope.states, scope.address.state_id)
scope.updateAddress = ->
scope.edit_address_form.$setPristine()
@@ -27,19 +27,12 @@ angular.module("admin.customers").directive 'editAddressDialog', ($compile, $tem
else
scope.addressType = 'ship_address'
scope.address = scope.customer[scope.addressType]
scope.states = scope.filterStates(scope.address?.country_id)
scope.states = CountryStates.statesFor(scope.availableCountries, scope.address?.country_id)
template = $compile($templateCache.get('admin/edit_address_dialog.html'))(scope)
template.dialog(DialogDefaults)
template.dialog('open')
scope.$apply()
scope.filterStates = (countryID) ->
return [] unless countryID
$filter('filter')(scope.availableCountries, {id: parseInt(countryID)}, true)[0].states
scope.clearState = ->
scope.address.state_id = ""
scope.addressStateMatchesCountry = ->
scope.states.some (state) -> state.id == scope.address.state_id

View File

@@ -3,6 +3,9 @@ angular.module("ofn.admin").factory "CountryStates", ($filter) ->
statesFor: (countries, country_id) ->
return [] unless country_id
country = $filter('filter')(countries, {id: country_id}, true)[0]
country = $filter('filter')(countries, {id: parseInt(country_id)}, true)[0]
return [] unless country
country.states
addressStateMatchesCountryStates: (countryStates, stateId) ->
countryStates.some (state) -> state.id == stateId