From f5aae5f8b930ca62943b91fad98c8f78ec11aa08 Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Bellet Date: Mon, 16 Jan 2023 11:32:01 +0100 Subject: [PATCH 1/2] Remplace ouf of stock `alert` by Flash message --- .../darkswarm/directives/on_hand.js.coffee | 4 ++-- spec/system/consumer/shopping/cart_spec.rb | 20 +++++++------------ 2 files changed, 9 insertions(+), 15 deletions(-) diff --git a/app/assets/javascripts/darkswarm/directives/on_hand.js.coffee b/app/assets/javascripts/darkswarm/directives/on_hand.js.coffee index 0edae73c22..ad9bd9e9c8 100644 --- a/app/assets/javascripts/darkswarm/directives/on_hand.js.coffee +++ b/app/assets/javascripts/darkswarm/directives/on_hand.js.coffee @@ -1,4 +1,4 @@ -angular.module('Darkswarm').directive "ofnOnHand", (StockQuantity) -> +angular.module('Darkswarm').directive "ofnOnHand", (StockQuantity, Messages) -> restrict: 'A' require: "ngModel" scope: true @@ -16,7 +16,7 @@ angular.module('Darkswarm').directive "ofnOnHand", (StockQuantity) -> ngModel.$parsers.push (viewValue) -> available_quantity = scope.available_quantity() if parseInt(viewValue) > available_quantity - alert t("js.insufficient_stock", {on_hand: available_quantity}) + Messages.flash({error: t("js.insufficient_stock", {on_hand: available_quantity})}) viewValue = available_quantity ngModel.$setViewValue viewValue ngModel.$render() diff --git a/spec/system/consumer/shopping/cart_spec.rb b/spec/system/consumer/shopping/cart_spec.rb index 068a31019e..6e371dd953 100644 --- a/spec/system/consumer/shopping/cart_spec.rb +++ b/spec/system/consumer/shopping/cart_spec.rb @@ -203,18 +203,16 @@ describe "full-page cart", js: true do variant2.update!(on_hand: 3, on_demand: false) visit main_app.cart_path - accept_alert 'Insufficient stock available, only 2 remaining' do - within "tr.variant-#{variant.id}" do - fill_in "order_line_items_attributes_0_quantity", with: '4' - end + within "tr.variant-#{variant.id}" do + fill_in "order_line_items_attributes_0_quantity", with: '4' end + expect(page).to have_content "Insufficient stock available, only 2 remaining" expect(page).to have_field "order_line_items_attributes_0_quantity", with: '2' - accept_alert 'Insufficient stock available, only 3 remaining' do - within "tr.variant-#{variant2.id}" do - fill_in "order_line_items_attributes_1_quantity", with: '4' - end + within "tr.variant-#{variant2.id}" do + fill_in "order_line_items_attributes_1_quantity", with: '4' end + expect(page).to have_content "Insufficient stock available, only 3 remaining" expect(page).to have_field "order_line_items_attributes_1_quantity", with: '3' end @@ -225,12 +223,8 @@ describe "full-page cart", js: true do visit main_app.cart_path variant.update! on_hand: 2 - accept_alert do - fill_in "order_line_items_attributes_0_quantity", with: '4' - end + fill_in "order_line_items_attributes_0_quantity", with: '4' click_button 'Update' - - expect(page).to have_content "Insufficient stock available, only 2 remaining" expect(page).to have_field "order_line_items_attributes_0_quantity", with: '1' end From 915174b513b8b7a49d5e1a1c2df0c149d3fefe70 Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Bellet Date: Mon, 16 Jan 2023 11:47:05 +0100 Subject: [PATCH 2/2] Do not stack same flash messages Very ugly in UI. --- .../javascripts/darkswarm/directives/flash.js.coffee | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/app/assets/javascripts/darkswarm/directives/flash.js.coffee b/app/assets/javascripts/darkswarm/directives/flash.js.coffee index 40337ed5f2..b5a5e7ef19 100644 --- a/app/assets/javascripts/darkswarm/directives/flash.js.coffee +++ b/app/assets/javascripts/darkswarm/directives/flash.js.coffee @@ -17,9 +17,12 @@ angular.module('Darkswarm').directive "ofnFlash", (flash, $timeout, RailsFlashLo # Callback when a new flash message is pushed to flash service show = (message, type)=> - if message - $scope.flashes.push({message: message, type: typePairings[type]}) - $timeout($scope.delete, 10000) + return unless message + # if same message already exists, don't add it again + return if $scope.flashes.some((flash) -> flash.message == message) + + $scope.flashes.push({message: message, type: typePairings[type]}) + $timeout($scope.delete, 10000) $scope.delete = -> $scope.flashes.shift()