Change bulk quantities on any quantity change

This prepares for changing the quantity with an input field. It also
applies if the quantity is changed after an ajax request.
This commit is contained in:
Maikel Linke
2020-12-10 16:37:14 +11:00
parent 7add9247d5
commit af918e63ee
2 changed files with 26 additions and 18 deletions

View File

@@ -6,18 +6,24 @@ Darkswarm.controller "ShopVariantCtrl", ($scope, $modal, Cart) ->
return if old_value[0] == null && new_value[0] == null
Cart.adjust($scope.variant.line_item)
if $scope.variant.product.group_buy
$scope.$watch "variant.line_item.quantity", ->
item = $scope.variant.line_item
if item.quantity < 1 || item.max_quantity < item.quantity
item.max_quantity = item.quantity
$scope.$watch "variant.line_item.max_quantity", ->
item = $scope.variant.line_item
if item.max_quantity < item.quantity
item.quantity = item.max_quantity
$scope.add = (quantity) ->
item = $scope.variant.line_item
item.quantity += quantity
if $scope.variant.product.group_buy
if item.quantity < 1 || item.max_quantity < item.quantity
item.max_quantity = item.quantity
$scope.addMax = (quantity) ->
item = $scope.variant.line_item
item.max_quantity += quantity
if item.max_quantity < item.quantity
item.quantity = item.max_quantity
$scope.canAdd = (quantity) ->
wantedQuantity = $scope.variant.line_item.quantity + quantity

View File

@@ -4,18 +4,18 @@ describe "ShopVariantCtrl", ->
beforeEach ->
module 'Darkswarm'
scope =
$watchGroup: ->
variant: {
inject ($rootScope, $controller, $modal)->
scope = $rootScope.$new()
scope.$watchGroup = ->
scope.variant = {
on_demand: true
product: {group_buy: false}
product: {group_buy: true}
line_item: {
quantity: 0
max_quantity: 0
}
}
inject ($controller, $modal)->
ctrl = $controller 'ShopVariantCtrl', {$scope: scope, $modal: $modal, Cart: null}
it "adds an item to the cart", ->
@@ -33,20 +33,22 @@ describe "ShopVariantCtrl", ->
expect(scope.variant.line_item.max_quantity).toEqual 5
it "adds to the max quantity to be at least min quantity", ->
scope.variant.product.group_buy = true
scope.variant.line_item.max_quantity = 2
scope.$apply ->
scope.variant.line_item.max_quantity = 2
scope.add 3
scope.$apply ->
scope.add 3
expect(scope.variant.line_item.quantity).toEqual 3
expect(scope.variant.line_item.max_quantity).toEqual 3
it "decreases the min quantity to not exceed max quantity", ->
scope.variant.product.group_buy = true
scope.variant.line_item.quantity = 3
scope.variant.line_item.max_quantity = 5
scope.$apply ->
scope.variant.line_item.quantity = 3
scope.variant.line_item.max_quantity = 5
scope.addMax -3
scope.$apply ->
scope.addMax -3
expect(scope.variant.line_item.quantity).toEqual 2
expect(scope.variant.line_item.max_quantity).toEqual 2