From 1220ff8a068ff21fce960f06bf04d755c3fd0986 Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Wed, 27 Apr 2016 15:05:44 +1000 Subject: [PATCH] Notify when stock limit reached on shopfront rather than silently capping --- .../darkswarm/directives/on_hand.js.coffee | 9 +++++++++ .../partials/shop_variant_no_group_buy.html.haml | 2 +- .../partials/shop_variant_with_group_buy.html.haml | 2 +- spec/features/consumer/shopping/shopping_spec.rb | 11 +++++++++++ 4 files changed, 22 insertions(+), 2 deletions(-) create mode 100644 app/assets/javascripts/darkswarm/directives/on_hand.js.coffee diff --git a/app/assets/javascripts/darkswarm/directives/on_hand.js.coffee b/app/assets/javascripts/darkswarm/directives/on_hand.js.coffee new file mode 100644 index 0000000000..0e3a96c608 --- /dev/null +++ b/app/assets/javascripts/darkswarm/directives/on_hand.js.coffee @@ -0,0 +1,9 @@ +Darkswarm.directive "ofnOnHand", -> + restrict: 'A' + link: (scope, elem, attr) -> + on_hand = parseInt(attr.ofnOnHand) + elem.bind 'change', (e) -> + if parseInt(elem.val()) > on_hand + scope.$apply -> + alert t('insufficient_stock', {on_hand: on_hand}) + elem.val(on_hand) diff --git a/app/assets/javascripts/templates/partials/shop_variant_no_group_buy.html.haml b/app/assets/javascripts/templates/partials/shop_variant_no_group_buy.html.haml index 36a4e44ade..1153d93b0f 100644 --- a/app/assets/javascripts/templates/partials/shop_variant_no_group_buy.html.haml +++ b/app/assets/javascripts/templates/partials/shop_variant_no_group_buy.html.haml @@ -7,6 +7,6 @@ placeholder: "0", "ofn-disable-scroll" => true, "ng-model" => "variant.line_item.quantity", - max: "{{variant.on_demand && 9999 || variant.count_on_hand }}", + "ofn-on-hand" => "{{variant.on_demand && 9999 || variant.count_on_hand }}", "ng-disabled" => "!variant.on_demand && variant.count_on_hand == 0", name: "variants[{{variant.id}}]", id: "variants_{{variant.id}}"} diff --git a/app/assets/javascripts/templates/partials/shop_variant_with_group_buy.html.haml b/app/assets/javascripts/templates/partials/shop_variant_with_group_buy.html.haml index f2b0b95837..6601f0c019 100644 --- a/app/assets/javascripts/templates/partials/shop_variant_with_group_buy.html.haml +++ b/app/assets/javascripts/templates/partials/shop_variant_with_group_buy.html.haml @@ -8,7 +8,7 @@ "ng-model" => "variant.line_item.quantity", placeholder: "{{'shop_variant_quantity_min' | t}}", "ofn-disable-scroll" => true, - max: "{{variant.on_demand && 9999 || variant.count_on_hand }}", + "ofn-on-hand" => "{{variant.on_demand && 9999 || variant.count_on_hand }}", name: "variants[{{variant.id}}]", id: "variants_{{variant.id}}"} %span.bulk-input %input.bulk.second{type: :number, diff --git a/spec/features/consumer/shopping/shopping_spec.rb b/spec/features/consumer/shopping/shopping_spec.rb index 5b617e7db6..2ca2d63341 100644 --- a/spec/features/consumer/shopping/shopping_spec.rb +++ b/spec/features/consumer/shopping/shopping_spec.rb @@ -238,6 +238,17 @@ feature "As a consumer I want to shop with a distributor", js: true do Spree::LineItem.where(id: li).should be_empty end + it "alerts us when we enter a quantity greater than the stock available" do + variant.update_attributes on_hand: 5 + visit shop_path + + accept_alert 'Insufficient stock available, only 5 remaining' do + fill_in "variants[#{variant.id}]", with: '10' + end + + page.should have_field "variants[#{variant.id}]", with: '5' + end + describe "when a product goes out of stock just before it's added to the cart" do it "stops the attempt, shows an error message and refreshes the products asynchronously" do variant.update_attributes! on_hand: 0