Merge pull request #10274 from jibees/8905-convert-popup-to-flash-messages-

On `/cart` page, if consumer adds more quantity than available, use in-app flash message instead of native alert
This commit is contained in:
Rachel Arnould
2023-02-02 15:50:17 +01:00
committed by GitHub
3 changed files with 15 additions and 18 deletions

View File

@@ -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()

View File

@@ -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()

View File

@@ -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