From af918e63ee3f9e88e1ee746ccdcd121be2847bb3 Mon Sep 17 00:00:00 2001 From: Maikel Linke Date: Thu, 10 Dec 2020 16:37:14 +1100 Subject: [PATCH] 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. --- .../shop_variant_controller.js.coffee | 16 +++++++---- .../shop_variant_controller_spec.js.coffee | 28 ++++++++++--------- 2 files changed, 26 insertions(+), 18 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 adc25f4ad7..cf21446a8d 100644 --- a/app/assets/javascripts/darkswarm/controllers/shop_variant_controller.js.coffee +++ b/app/assets/javascripts/darkswarm/controllers/shop_variant_controller.js.coffee @@ -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 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 8a2be1ee42..de350faeb2 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 @@ -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