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!
This commit is contained in:
Matt-Yorkley
2021-05-12 15:41:32 +01:00
parent 47e2976fd1
commit dc6be6f06b
2 changed files with 8 additions and 6 deletions

View File

@@ -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

View File

@@ -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