From dc6be6f06bfafd4e35d4645a4a797e9319187278 Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Wed, 12 May 2021 15:41:32 +0100 Subject: [PATCH] Don't resubmit the whole cart contents for no reason. There's a couple of places where this was causing a cart update submission where it wasn't needed, eg the items had not actually changed. The conditional here was designed to stop that from happening, but it was actually passing every time (the conditional logic was not actually catching the case it was supposed to). This is really expensive! --- .../controllers/shop_variant_controller.js.coffee | 8 +++----- .../checkout/shop_variant_controller_spec.js.coffee | 6 +++++- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/app/assets/javascripts/darkswarm/controllers/shop_variant_controller.js.coffee b/app/assets/javascripts/darkswarm/controllers/shop_variant_controller.js.coffee index 825bc9f39f..e71a7a0a29 100644 --- a/app/assets/javascripts/darkswarm/controllers/shop_variant_controller.js.coffee +++ b/app/assets/javascripts/darkswarm/controllers/shop_variant_controller.js.coffee @@ -1,9 +1,5 @@ Darkswarm.controller "ShopVariantCtrl", ($scope, $modal, Cart) -> - $scope.$watchGroup [ - 'variant.line_item.quantity', - 'variant.line_item.max_quantity' - ], (new_value, old_value) -> - return if old_value[0] == null && new_value[0] == null + $scope.updateCart = (line_item) -> Cart.adjust($scope.variant.line_item) $scope.variant.line_item.quantity ||= 0 @@ -44,10 +40,12 @@ Darkswarm.controller "ShopVariantCtrl", ($scope, $modal, Cart) -> $scope.add = (quantity) -> item = $scope.variant.line_item item.quantity = $scope.sanitizedQuantity() + quantity + $scope.updateCart(item) $scope.addMax = (quantity) -> item = $scope.variant.line_item item.max_quantity = $scope.sanitizedMaxQuantity() + quantity + $scope.updateCart(item) $scope.canAdd = (quantity) -> wantedQuantity = $scope.sanitizedQuantity() + quantity diff --git a/spec/javascripts/unit/darkswarm/controllers/checkout/shop_variant_controller_spec.js.coffee b/spec/javascripts/unit/darkswarm/controllers/checkout/shop_variant_controller_spec.js.coffee index 4576e376c7..c560a287c4 100644 --- a/spec/javascripts/unit/darkswarm/controllers/checkout/shop_variant_controller_spec.js.coffee +++ b/spec/javascripts/unit/darkswarm/controllers/checkout/shop_variant_controller_spec.js.coffee @@ -1,6 +1,7 @@ describe "ShopVariantCtrl", -> ctrl = null scope = null + CartMock = null beforeEach -> module 'Darkswarm' @@ -16,7 +17,10 @@ describe "ShopVariantCtrl", -> max_quantity: undefined } } - ctrl = $controller 'ShopVariantCtrl', {$scope: scope, $modal: $modal, Cart: null} + CartMock = + adjust: -> + true + ctrl = $controller 'ShopVariantCtrl', {$scope: scope, $modal: $modal, Cart: CartMock} it "initializes the quantity for shop display", -> expect(scope.variant.line_item.quantity).toEqual 0