From c224df9b6acfb2e95cb265fac65e72aafcefcdd1 Mon Sep 17 00:00:00 2001 From: Pau Perez Date: Tue, 7 Apr 2020 09:18:21 +0200 Subject: [PATCH] Do not trigger an orderChanged with null quantity When loading the page $watchGroup calls the listener function for every listed line item but with a set variant and null quantity and max_quantity. There's no point on computing an order change when there was none. This saves an empty request on the second most used endpoint of the app, specially busy when users are placing orders. --- .../darkswarm/directives/shop_variant.js.coffee | 6 +++++- .../unit/darkswarm/services/cart_spec.js.coffee | 7 +++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/app/assets/javascripts/darkswarm/directives/shop_variant.js.coffee b/app/assets/javascripts/darkswarm/directives/shop_variant.js.coffee index d679e6c3f0..84615e3125 100644 --- a/app/assets/javascripts/darkswarm/directives/shop_variant.js.coffee +++ b/app/assets/javascripts/darkswarm/directives/shop_variant.js.coffee @@ -5,5 +5,9 @@ Darkswarm.directive "shopVariant", -> scope: variant: '=' controller: ($scope, Cart) -> - $scope.$watchGroup ['variant.line_item.quantity', 'variant.line_item.max_quantity'], -> + $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 Cart.adjust($scope.variant.line_item) diff --git a/spec/javascripts/unit/darkswarm/services/cart_spec.js.coffee b/spec/javascripts/unit/darkswarm/services/cart_spec.js.coffee index f8d124c110..0b9c398a85 100644 --- a/spec/javascripts/unit/darkswarm/services/cart_spec.js.coffee +++ b/spec/javascripts/unit/darkswarm/services/cart_spec.js.coffee @@ -45,6 +45,13 @@ describe 'Cart service', -> Cart.adjust(order.line_items[0]) expect(Cart.line_items.length).toEqual 0 + it "does not add an item in the cart without quantity", -> + Cart.line_items = [] + + spyOn(Cart, 'orderChanged') + order.line_items[0].max_quantity = 0 + expect(Cart.orderChanged).not.toHaveBeenCalled() + it "sums the quantity of each line item for cart total", -> order.line_items[0].quantity = 2 expect(Cart.total_item_count()).toEqual 2