mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-23 01:03:21 +00:00
Merge pull request #2876 from luisramos0/subs-states-fix
Fix bug in subscriptions address controller where country states were not correctly returned
This commit is contained in:
@@ -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,10 @@ 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)
|
||||
unless CountryStates.addressStateMatchesCountryStates(scope.states, scope.address.state_id)
|
||||
scope.address.state_id = ""
|
||||
|
||||
scope.updateAddress = ->
|
||||
scope.edit_address_form.$setPristine()
|
||||
@@ -27,19 +28,9 @@ 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
|
||||
@@ -1,10 +1,20 @@
|
||||
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
|
||||
$filter('filter')(availableCountries, {id: country_id})[0].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', (newCountryID) ->
|
||||
return unless newCountryID
|
||||
$scope.billStates = CountryStates.statesFor(availableCountries, newCountryID)
|
||||
unless CountryStates.addressStateMatchesCountryStates($scope.billStates, $scope.subscription.bill_address.state_id)
|
||||
$scope.subscription.bill_address.state_id = ""
|
||||
|
||||
$scope.$watch 'subscription.ship_address.country_id', (newCountryID) ->
|
||||
return unless newCountryID
|
||||
$scope.shipStates = CountryStates.statesFor(availableCountries, newCountryID)
|
||||
unless CountryStates.addressStateMatchesCountryStates($scope.shipStates, $scope.subscription.ship_address.state_id)
|
||||
$scope.subscription.ship_address.state_id = ""
|
||||
|
||||
$scope.registerNextCallback 'address', ->
|
||||
$scope.subscription_form.$submitted = true
|
||||
@@ -18,9 +28,3 @@ angular.module("admin.subscriptions").controller "AddressController", ($scope, $
|
||||
$scope.registerBackCallback 'address', ->
|
||||
StatusMessage.clear()
|
||||
$scope.setView('details')
|
||||
|
||||
$scope.$watch 'subscription.bill_address.country_id', (newValue, oldValue) ->
|
||||
$scope.billStates = $scope.statesFor(newValue) if newValue?
|
||||
|
||||
$scope.$watch 'subscription.ship_address.country_id', (newValue, oldValue) ->
|
||||
$scope.shipStates = $scope.statesFor(newValue) if newValue?
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
angular.module("admin.utils").factory "CountryStates", ($filter) ->
|
||||
new class CountryStates
|
||||
|
||||
statesFor: (countries, country_id) ->
|
||||
return [] unless country_id
|
||||
country = $filter('filter')(countries, {id: parseInt(country_id)}, true)[0]
|
||||
return [] unless country
|
||||
country.states
|
||||
|
||||
addressStateMatchesCountryStates: (countryStates, stateId) ->
|
||||
countryStates.some (state) -> state.id == stateId
|
||||
Reference in New Issue
Block a user