From 6cc14614e8627f3bfe291abf34a64d0b64cf7aaf Mon Sep 17 00:00:00 2001 From: Andrzej Krzywda Date: Wed, 6 Dec 2023 18:28:24 +0100 Subject: [PATCH] Hide remove item button when item not possible to remove This change applies to Sales Panel. A similar change is needed in the Client Panel. Tested this via integration test and checking if remove url is visible or not in different states. This doesn't test the Turbo mechanism which is used here too. I don't like this duplication between the Rails view button and the same in Turbo call. I suppose we should be less granular and just rerender whole table here instead of single values. Another ugly place is now this if/else in the view. Hard to avoid it, but maybe once we move this view to Ruby code, it can be better structured (plan for the future). --- .../read_models/orders/add_item_to_order.rb | 5 +++++ .../orders/remove_item_from_order.rb | 1 + .../app/views/orders/edit.html.erb | 8 +++++-- .../test/integration/orders_test.rb | 22 +++++++++++++++++++ 4 files changed, 34 insertions(+), 2 deletions(-) diff --git a/rails_application/app/read_models/orders/add_item_to_order.rb b/rails_application/app/read_models/orders/add_item_to_order.rb index 8a290626c..38cd61860 100644 --- a/rails_application/app/read_models/orders/add_item_to_order.rb +++ b/rails_application/app/read_models/orders/add_item_to_order.rb @@ -1,5 +1,9 @@ module Orders class AddItemToOrder < Infra::EventHandler + include Rails.application.routes.url_helpers + include ActionView::Helpers::UrlHelper + include ActionView::Helpers::FormTagHelper + def call(event) order_id = event.data.fetch(:order_id) ApplicationRecord.with_advisory_lock(order_id) do @@ -13,6 +17,7 @@ def call(event) broadcaster.call(order_id, product_id, "quantity", item.quantity) broadcaster.call(order_id, product_id, "value", ActiveSupport::NumberHelper.number_to_currency(item.value)) + broadcaster.call(order_id, product_id, "remove_item_button", button_to("Remove", remove_item_order_path(id: order_id, product_id: product_id), class: "hover:underline text-blue-500")) end event_store.link_event_to_stream(event, "Orders$all") diff --git a/rails_application/app/read_models/orders/remove_item_from_order.rb b/rails_application/app/read_models/orders/remove_item_from_order.rb index e1f614e78..9a1947b95 100644 --- a/rails_application/app/read_models/orders/remove_item_from_order.rb +++ b/rails_application/app/read_models/orders/remove_item_from_order.rb @@ -9,6 +9,7 @@ def call(event) broadcaster.call(order_id, product_id, "quantity", item.quantity) broadcaster.call(order_id, product_id, "value", ActiveSupport::NumberHelper.number_to_currency(item.value)) + broadcaster.call(order_id, product_id, "remove_item_button", "") if item.quantity.zero? event_store.link_event_to_stream(event, "Orders$all") end diff --git a/rails_application/app/views/orders/edit.html.erb b/rails_application/app/views/orders/edit.html.erb index a71286463..be416188c 100644 --- a/rails_application/app/views/orders/edit.html.erb +++ b/rails_application/app/views/orders/edit.html.erb @@ -44,8 +44,12 @@ "><%= order_line.try(&:quantity) || 0 %> <%= number_to_currency(product.price) %> "><%= number_to_currency(order_line.try(&:value)) %> - <%= button_to "Add", add_item_order_path(id: @order_id, product_id: product.id), class: "hover:underline text-blue-500" %> - <%= button_to "Remove", remove_item_order_path(id: @order_id, product_id: product.id), class: "hover:underline text-blue-500" %> + <%= button_to "Add", add_item_order_path(id: @order_id, product_id: product.id), class: "hover:underline text-blue-500" %> + <% if order_line.nil? %> + "> + <% else %> + "><%= button_to("Remove", remove_item_order_path(id: @order_id, product_id: product.id), class: "hover:underline text-blue-500") %> + <% end %> <% end %> diff --git a/rails_application/test/integration/orders_test.rb b/rails_application/test/integration/orders_test.rb index da559d4e9..aebb9a811 100644 --- a/rails_application/test/integration/orders_test.rb +++ b/rails_application/test/integration/orders_test.rb @@ -47,10 +47,16 @@ def test_happy_path get "/" get "/orders/new" + follow_redirect! + + assert_remove_buttons_not_visible(async_remote_id, fearless_id) + post "/orders/#{order_id}/add_item?product_id=#{async_remote_id}" post "/orders/#{order_id}/add_item?product_id=#{fearless_id}" post "/orders/#{order_id}/add_item?product_id=#{fearless_id}" Sidekiq::Job.drain_all + get "/orders/#{order_id}/edit" + assert_remove_buttons_visible(async_remote_id, fearless_id, order_id) apply_discount_10_percent(order_id) Sidekiq::Job.drain_all @@ -224,6 +230,22 @@ def test_discount_is_applied_for_new_order private + def assert_remove_buttons_visible(async_remote_id, fearless_id, order_id) + assert_match(/#{Regexp.escape(remove_item_order_path(id: order_id, product_id: async_remote_id))}/, response.body) + assert_match(/#{Regexp.escape(remove_item_order_path(id: order_id, product_id: fearless_id))}/, response.body) + end + + def assert_remove_buttons_not_visible(async_remote_id, fearless_id) + url = request.original_url + uri = URI.parse(url) + puts uri.query + path_components = uri.path.split('/') + order_uuid = path_components[-2] + + assert_no_match(/#{Regexp.escape(remove_item_order_path(id: order_uuid, product_id: async_remote_id))}/, response.body) + assert_no_match(/#{Regexp.escape(remove_item_order_path(id: order_uuid, product_id: fearless_id))}/, response.body) + end + def verify_shipping(order_id) get "/orders/#{order_id}" assert_select("dd", "Shipping address is missing.")