Move address and card loading logic down into DetailsController

Also switch to using CreditCardResource to load the card
This commit is contained in:
Rob Harrington
2017-11-10 18:09:24 +11:00
parent a5ae4c801c
commit bdac68900f
5 changed files with 30 additions and 21 deletions

View File

@@ -1,4 +1,4 @@
angular.module("admin.standingOrders").controller "DetailsController", ($scope, StatusMessage) ->
angular.module("admin.standingOrders").controller "DetailsController", ($scope, $http, CreditCardResource, StatusMessage) ->
$scope.cardRequired = false
$scope.registerNextCallback 'details', ->
@@ -10,8 +10,13 @@ angular.module("admin.standingOrders").controller "DetailsController", ($scope,
else
StatusMessage.display 'failure', t('admin.standing_orders.details.invalid_error')
$scope.$watch "standingOrder.customer_id", (newValue, oldValue) ->
return if !newValue?
$scope.loadAddresses(newValue) unless $scope.standingOrder.id?
$scope.loadCreditCards(newValue)
$scope.$watch "standingOrder.payment_method_id", (newValue, oldValue) ->
return if !newValue? || newValue == oldValue
return if !newValue?
paymentMethod = ($scope.paymentMethods.filter (pm) -> pm.id == newValue)[0]
return unless paymentMethod?
if paymentMethod.type == "Spree::Gateway::StripeConnect"
@@ -19,3 +24,15 @@ angular.module("admin.standingOrders").controller "DetailsController", ($scope,
else
$scope.cardRequired = false
$scope.standingOrder.credit_card_id = null
$scope.loadAddresses = (customer_id) ->
$http.get("/admin/customers/#{customer_id}/addresses")
.success (response) =>
delete response.bill_address.id
delete response.ship_address.id
angular.extend($scope.standingOrder.bill_address, response.bill_address)
angular.extend($scope.standingOrder.ship_address, response.ship_address)
$scope.shipAddressFromBilling() unless response.ship_address.address1?
$scope.loadCreditCards = (customer_id) ->
$scope.creditCards = CreditCardResource.index(customer_id: customer_id)

View File

@@ -1,4 +1,4 @@
angular.module("admin.standingOrders").controller "StandingOrderController", ($scope, $http, StandingOrder, StandingOrderForm, customers, schedules, paymentMethods, shippingMethods) ->
angular.module("admin.standingOrders").controller "StandingOrderController", ($scope, StandingOrder, StandingOrderForm, customers, schedules, paymentMethods, shippingMethods) ->
$scope.standingOrder = new StandingOrder()
$scope.errors = null
$scope.save = null
@@ -18,18 +18,6 @@ angular.module("admin.standingOrders").controller "StandingOrderController", ($s
$scope.next = -> $scope.nextCallbacks[$scope.view]()
$scope.back = -> $scope.backCallbacks[$scope.view]()
$scope.$watch "standingOrder.customer_id", (newValue, oldValue) ->
return if !newValue? || newValue == oldValue
$http.get("/admin/customers/#{newValue}/addresses")
.success (response) =>
delete response.bill_address.id
delete response.ship_address.id
angular.extend($scope.standingOrder.bill_address, response.bill_address)
angular.extend($scope.standingOrder.ship_address, response.ship_address)
$scope.shipAddressFromBilling() unless response.ship_address.address1?
$http.get("/admin/customers/#{newValue}/cards")
.success (response) => $scope.creditCards = response.cards
$scope.shipAddressFromBilling = =>
angular.extend($scope.standingOrder.ship_address, $scope.standingOrder.bill_address)

View File

@@ -0,0 +1,5 @@
angular.module("admin.standingOrders").factory 'CreditCardResource', ($resource) ->
resource = $resource '/admin/customers/:customer_id/cards.json', {},
'index':
method: 'GET'
isArray: true