Extract country states logic out of subscriptions address controller into new service CountryStates

This commit is contained in:
luisramos0
2018-10-16 21:58:27 +01:00
parent feaf16d878
commit 5c5a2194d6
4 changed files with 25 additions and 32 deletions

View File

@@ -1,20 +1,14 @@
angular.module("admin.subscriptions").controller "AddressController", ($scope, $filter, StatusMessage, availableCountries) ->
angular.module("admin.subscriptions").controller "AddressController", ($scope, StatusMessage, availableCountries, CountryStates) ->
$scope.countries = availableCountries
$scope.statesFor = (country_id) ->
return [] unless country_id
country = $filter('filter')(availableCountries, {id: country_id}, true)[0]
return [] unless country
country.states
$scope.billStates = $scope.statesFor($scope.subscription.bill_address.country_id)
$scope.shipStates = $scope.statesFor($scope.subscription.ship_address.country_id)
$scope.billStates = CountryStates.statesFor(availableCountries, $scope.subscription.bill_address.country_id)
$scope.shipStates = CountryStates.statesFor(availableCountries, $scope.subscription.ship_address.country_id)
$scope.$watch 'subscription.bill_address.country_id', (newValue, oldValue) ->
$scope.billStates = $scope.statesFor(newValue) if newValue?
$scope.billStates = CountryStates.statesFor(availableCountries, newValue) if newValue?
$scope.$watch 'subscription.ship_address.country_id', (newValue, oldValue) ->
$scope.shipStates = $scope.statesFor(newValue) if newValue?
$scope.shipStates = CountryStates.statesFor(availableCountries, newValue) if newValue?
$scope.registerNextCallback 'address', ->
$scope.subscription_form.$submitted = true