mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-03 22:06:07 +00:00
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:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user