Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
Fix handling errors when updating/destroying line items from cart (#12820)
Łukasz Adamczyk2010ed772062
diff --git a/storefront/app/controllers/spree/line_items_controller.rb b/storefront/app/controllers/spree/line_items_controller.rbindex e62791e187..de234d69db 100644--- a/storefront/app/controllers/spree/line_items_controller.rb+++ b/storefront/app/controllers/spree/line_items_controller.rb@@ -22,8 +22,6 @@ module Spreequantity: @quantity,options: options)- flash.now[:error] = result.value.errors.full_messages.to_sentence if result.failure?-@line_item = result.valueif result.success?@@ -45,9 +43,12 @@ module Spreequantity = line_item_params[:quantity]&.to_i || 1result = cart_set_item_quantity_service.call(order: @order, line_item: @line_item, quantity: quantity)- @error = result.value.errors.full_messages.to_sentence if result.failure?-- load_line_items if result.success?+ if result.success?+ load_line_items+ else+ @error = result.value.errors.full_messages.to_sentence+ flash.now[:error] = @error+ endrespond_to do |format|format.turbo_stream@@ -57,9 +58,15 @@ module Spreedef destroyresult = cart_remove_line_item_service.call(order: @order, line_item: @line_item)- load_line_items if result.success?- track_event('product_removed', { line_item: @line_item })+ if result.success?+ load_line_items++ track_event('product_removed', { line_item: @line_item })+ else+ @error = result.value.errors.full_messages.to_sentence+ flash.now[:error] = @error+ endrespond_to do |format|format.turbo_streamdiff --git a/storefront/app/helpers/spree/cart_helper.rb b/storefront/app/helpers/spree/cart_helper.rbindex 4ee1b73655..656fe2546d 100644--- a/storefront/app/helpers/spree/cart_helper.rb+++ b/storefront/app/helpers/spree/cart_helper.rb@@ -22,7 +22,7 @@ module Spreeclass: opts[:class],data: {'quantity-picker-target': opts[:action],- action: "click->quantity-picker##{opts[:action]} click->cart#disableCart"+ action: "click->quantity-picker##{opts[:action]} click->cart#disableCart turbo-stream-form:submit-end->cart#enableCart"})enddiff --git a/storefront/app/javascript/spree/storefront/controllers/cart_controller.js b/storefront/app/javascript/spree/storefront/controllers/cart_controller.jsindex 8766543693..1f1dee6303 100644--- a/storefront/app/javascript/spree/storefront/controllers/cart_controller.js+++ b/storefront/app/javascript/spree/storefront/controllers/cart_controller.js@@ -7,4 +7,9 @@ export default class extends Controller {this.containerTarget.classList.add('pointer-events-none', 'opacity-50')this.spinnerTarget.classList.remove('hidden')}++ enableCart() {+ this.containerTarget.classList.remove('pointer-events-none', 'opacity-50')+ this.spinnerTarget.classList.add('hidden')+ }}diff --git a/storefront/app/views/spree/line_items/destroy.turbo_stream.erb b/storefront/app/views/spree/line_items/destroy.turbo_stream.erbindex 7eb5a28fc3..b0281ff19a 100644--- a/storefront/app/views/spree/line_items/destroy.turbo_stream.erb+++ b/storefront/app/views/spree/line_items/destroy.turbo_stream.erb@@ -1,13 +1,17 @@-<%= turbo_stream.replace_all '.cart-contents' do %>- <%= turbo_frame_tag cart_id(@order), class: 'cart-contents' do %>- <% if @order.line_items.empty? %>- <%= render 'spree/orders/empty' %>- <% else %>- <%= render 'spree/orders/cart' %>- <% end %>+<% if flash[:error] %>+ <%= spree_turbo_update_flashes %>+<% else %>+ <%= turbo_stream.replace_all '.cart-contents' do %>+ <%= turbo_frame_tag cart_id(@order), class: 'cart-contents' do %>+ <% if @order.line_items.empty? %>+ <%= render 'spree/orders/empty' %>+ <% else %>+ <%= render 'spree/orders/cart' %>+ <% end %>- <%= render_storefront_partials(:remove_from_cart_partials) %>+ <%= render_storefront_partials(:remove_from_cart_partials) %>+ <% end %><% end %>-<% end %>-<%= spree_turbo_update_cart(@order) %>+ <%= spree_turbo_update_cart(@order) %>+<% end %>diff --git a/storefront/app/views/themes/default/spree/orders/_line_item.html.erb b/storefront/app/views/themes/default/spree/orders/_line_item.html.erbindex 6348fdc26d..9df785b0f3 100644--- a/storefront/app/views/themes/default/spree/orders/_line_item.html.erb+++ b/storefront/app/views/themes/default/spree/orders/_line_item.html.erb@@ -1,5 +1,5 @@-<%= turbo_frame_tag dom_id(line_item), data: { controller: 'reveal' } do %>- <li class="cart-line-item flex items-top py-6 px-4 w-full" data-reveal-target="item">+<%= turbo_frame_tag dom_id(line_item) do %>+ <li class="cart-line-item flex items-top py-6 px-4 w-full"><div class="line-item-overlay"></div><div class="cart-product-image flex-shrink-0" id="<%= dom_id(line_item) %>-image" data-turbo-permanent><% image = line_item.variant.default_image %>@@ -14,7 +14,7 @@<%= link_to line_item.name, spree_storefront_resource_url(line_item.product), class: 'font-semibold text-text', data: { 'turbo-frame': '_top' } %><%= form_for line_item, url: spree.line_item_url(line_item, order_token: line_item.order.token), method: :delete, data: { controller: 'turbo-stream-form' } do |item_form| %>- <%= button_tag type: :submit, class: 'remove-line-item-button h-100', data: { action: "click->reveal#hide click->cart#disableCart" } do %>+ <%= button_tag type: :submit, class: 'remove-line-item-button h-100', data: { action: "click->cart#disableCart turbo-stream-form:submit-end->cart#enableCart" } do %><%= render 'spree/shared/icons/cross', size: 16 %><% end %><% end %>diff --git a/storefront/spec/controllers/spree/line_items_controller_spec.rb b/storefront/spec/controllers/spree/line_items_controller_spec.rbindex 30925ad3c6..00ce92d234 100644--- a/storefront/spec/controllers/spree/line_items_controller_spec.rb+++ b/storefront/spec/controllers/spree/line_items_controller_spec.rb@@ -26,6 +26,24 @@ describe Spree::LineItemsController, type: :controller doit 'handles invalid variant id' doexpect { post :create, params: { variant_id: 9999, quantity: 1 }, format: :turbo_stream }.to raise_error(ActiveRecord::RecordNotFound)end++ context 'when adding item fails' do+ let(:add_item_service) { instance_double(Spree::Cart::AddItem, call: result) }+ let(:result) { double('result', failure?: true, success?: false, value: double('error_object', errors: double('errors', full_messages: ['Item could not be added to cart']))) }++ before do+ allow(Spree::Cart::AddItem).to receive(:new).and_return(add_item_service)+ end++ it 'responds with an error' do+ post :create, params: { variant_id: variant.id, quantity: 1 }, format: :turbo_stream++ expect(response).to have_http_status(:ok)++ expect(assigns(:error)).to eq('Item could not be added to cart')+ expect(flash.now[:error]).to eq('Item could not be added to cart')+ end+ endendcontext '#update' do@@ -38,6 +56,24 @@ describe Spree::LineItemsController, type: :controller doput :update, params: { id: line_item.id, line_item: { quantity: 3 } }, format: :turbo_streamexpect(line_item.reload.quantity).to eq(3)end++ context 'when setting quantity fails' do+ let(:set_quantity_service) { instance_double(Spree::Cart::SetQuantity, call: result) }+ let(:result) { double('result', failure?: true, success?: false, value: double('error_object', errors: double('errors', full_messages: ['Quantity must be greater than 0']))) }++ before do+ allow(Spree::Cart::SetQuantity).to receive(:new).and_return(set_quantity_service)+ end++ it 'responds with an error' do+ put :update, params: { id: line_item.id, line_item: { quantity: 0 } }, format: :turbo_stream++ expect(response).to have_http_status(:ok)++ expect(assigns(:error)).to eq('Quantity must be greater than 0')+ expect(flash.now[:error]).to eq('Quantity must be greater than 0')+ end+ endendcontext '#destroy' do@@ -50,5 +86,23 @@ describe Spree::LineItemsController, type: :controller dodelete :destroy, params: { id: line_item.id }, format: :turbo_streamexpect(order.line_items).not_to include(line_item)end++ context 'when removing item fails' do+ let(:remove_item_service) { instance_double(Spree::Cart::RemoveLineItem, call: result) }+ let(:result) { double('result', failure?: true, success?: false, value: double('error_object', errors: double('errors', full_messages: ['Item could not be removed from cart']))) }++ before do+ allow(Spree::Cart::RemoveLineItem).to receive(:new).and_return(remove_item_service)+ end++ it 'responds with an error' do+ delete :destroy, params: { id: line_item.id }, format: :turbo_stream++ expect(response).to have_http_status(:ok)+ expect(assigns(:error)).to eq('Item could not be removed from cart')+ expect(flash.now[:error]).to eq('Item could not be removed from cart')+ expect(remove_item_service).to have_received(:call).with(order: order, line_item: line_item)+ end+ endendend