From c73c2dd8a86934fcfd1971a99bde6e8b4a3d4e47 Mon Sep 17 00:00:00 2001 From: Gaetan Craig-Riou Date: Mon, 27 Jan 2025 20:52:41 +1100 Subject: [PATCH] Add basic out of stock modal --- app/controllers/concerns/order_stock_check.rb | 7 +++-- app/views/checkout/edit.html.haml | 5 ++++ spec/support/checkout_helper.rb | 8 ----- spec/system/consumer/checkout/guest_spec.rb | 29 +++++-------------- 4 files changed, 17 insertions(+), 32 deletions(-) diff --git a/app/controllers/concerns/order_stock_check.rb b/app/controllers/concerns/order_stock_check.rb index d0b816ef29..db51cffbc5 100644 --- a/app/controllers/concerns/order_stock_check.rb +++ b/app/controllers/concerns/order_stock_check.rb @@ -11,11 +11,12 @@ module OrderStockCheck end def handle_insufficient_stock - stock_service = Orders::CheckStockService.new(@order) + @any_out_of_stock = false + + stock_service = Orders::CheckStockService.new(order: @order) return if stock_service.sufficient_stock? - flash[:error] = Spree.t(:inventory_error_flash_for_insufficient_quantity) - redirect_to main_app.cart_path + @any_out_of_stock = true end def check_order_cycle_expiry diff --git a/app/views/checkout/edit.html.haml b/app/views/checkout/edit.html.haml index 990c8387b5..f138cc4f5f 100644 --- a/app/views/checkout/edit.html.haml +++ b/app/views/checkout/edit.html.haml @@ -20,6 +20,11 @@ = render partial: "shopping_shared/order_cycles" %div{ "data-controller": "guest-checkout", "data-guest-checkout-distributor-value": @order.distributor.id } + = #TODO create new component ? reuse out_of_stock.html.haml template + - if @any_out_of_stock + = render ModalComponent.new(id: "out-of-stock-items", instant: true) do + %div + Some items are out of stock %div{ style: "display: #{spree_current_user ? 'block' : 'none'}", "data-guest-checkout-target": "checkout" } = render partial: "checkout" diff --git a/spec/support/checkout_helper.rb b/spec/support/checkout_helper.rb index 6fa1bbd6aa..fd85d4a779 100644 --- a/spec/support/checkout_helper.rb +++ b/spec/support/checkout_helper.rb @@ -66,12 +66,4 @@ module CheckoutHelper click_on "Complete order" expect(page).to have_content "Back To Store" end - - def out_of_stock_check(step) - visit checkout_step_path(step) - - expect(page).not_to have_selector 'closing', text: "Checkout now" - expect(page).to have_selector 'closing', text: "Your shopping cart" - expect(page).to have_content "An item in your cart has become unavailable" - end end diff --git a/spec/system/consumer/checkout/guest_spec.rb b/spec/system/consumer/checkout/guest_spec.rb index 0a90caab66..35b8b2fc8e 100644 --- a/spec/system/consumer/checkout/guest_spec.rb +++ b/spec/system/consumer/checkout/guest_spec.rb @@ -88,21 +88,6 @@ RSpec.describe "As a consumer, I want to checkout my order" do end end - shared_examples "when I have an out of stock product in my cart" do - before do - variant.update!(on_demand: false, on_hand: 0) - end - - it "returns me to the cart with an error message" do - visit checkout_path - - expect(page).not_to have_selector 'closing', text: "Checkout now" - expect(page).to have_selector 'closing', text: "Your shopping cart" - expect(page).to have_content "An item in your cart has become unavailable" - expect(page).to have_content "Update" - end - end - context "as a guest user" do before do visit checkout_path @@ -222,8 +207,6 @@ RSpec.describe "As a consumer, I want to checkout my order" do "Local", "Shipping with Fee", "Z Free Shipping without required address" ] end - - it_behaves_like "when I have an out of stock product in my cart" end context "on the 'payment' step" do @@ -235,8 +218,6 @@ RSpec.describe "As a consumer, I want to checkout my order" do it "should allow visit '/checkout/payment'" do expect(page).to have_current_path("/checkout/payment") end - - it_behaves_like "when I have an out of stock product in my cart" end describe "hidding a shipping method" do @@ -305,8 +286,14 @@ RSpec.describe "As a consumer, I want to checkout my order" do login_as(user) end end - it "returns me to the cart with an error message" do - out_of_stock_check(step) + + it "displays a modal warning the user of updated cart" do + visit checkout_step_path(step) + + expect(page).to have_selector 'closing', text: "Checkout now" + within "#out-of-stock-items" do + expect(page).to have_content "Some items are out of stock" + end end end end