diff --git a/rails_application/app/read_models/client_orders/add_item_to_order.rb b/rails_application/app/read_models/client_orders/add_item_to_order.rb index f403c9009..5a9e38ccb 100644 --- a/rails_application/app/read_models/client_orders/add_item_to_order.rb +++ b/rails_application/app/read_models/client_orders/add_item_to_order.rb @@ -1,5 +1,9 @@ module ClientOrders 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) product_id = event.data.fetch(:product_id) @@ -13,11 +17,20 @@ def call(event) broadcast_update(order_id, product_id, "product_quantity", item.product_quantity) broadcast_update(order_id, product_id, "value", ActiveSupport::NumberHelper.number_to_currency(item.value)) + show_remove_item_button(order_id, product_id) end end private + def show_remove_item_button(order_id, product_id) + broadcast_update(order_id, product_id, "remove_item_button", remove_button_html(order_id, product_id)) + end + + def remove_button_html(order_id, product_id) + button_to("Remove", remove_item_client_order_path(id: order_id, product_id: product_id), class: "hover:underline text-blue-500") + end + def broadcast_update(order_id, product_id, target, content) Turbo::StreamsChannel.broadcast_update_to( "client_orders_#{order_id}", diff --git a/rails_application/app/read_models/client_orders/remove_item_from_order.rb b/rails_application/app/read_models/client_orders/remove_item_from_order.rb index babaca024..4a6e1760a 100644 --- a/rails_application/app/read_models/client_orders/remove_item_from_order.rb +++ b/rails_application/app/read_models/client_orders/remove_item_from_order.rb @@ -9,10 +9,17 @@ def call(event) broadcast_update(order_id, product_id, "product_quantity", item.product_quantity) broadcast_update(order_id, product_id, "value", ActiveSupport::NumberHelper.number_to_currency(item.value)) + broadcast_update(order_id, product_id, "remove_item_button", "") if zero_quantity?(item) + + event_store.link_event_to_stream(event, "ClientOrders$all") end private + def zero_quantity?(item) + item.nil? || item.product_quantity.zero? + end + def broadcast_update(order_id, product_id, target, content) Turbo::StreamsChannel.broadcast_update_to( "client_orders_#{order_id}", diff --git a/rails_application/app/views/client/orders/edit.html.erb b/rails_application/app/views/client/orders/edit.html.erb index 0e550a39a..6c781b642 100644 --- a/rails_application/app/views/client/orders/edit.html.erb +++ b/rails_application/app/views/client/orders/edit.html.erb @@ -20,9 +20,13 @@ <%= button_to "Add", add_item_client_order_path(id: @order_id, product_id: product.uid), class: "hover:underline text-blue-500" %> - - <%= button_to "Remove", remove_item_client_order_path(id: @order_id, product_id: product.uid), class: "hover:underline text-blue-500" %> - + <% if order_line.nil? %> + "> + <% else %> + "> + <%= button_to("Remove", remove_item_client_order_path(id: @order_id, product_id: product.uid), class: "hover:underline text-blue-500") %> + + <% end %> <% end %> diff --git a/rails_application/app/views/orders/edit.html.erb b/rails_application/app/views/orders/edit.html.erb index be416188c..70a419346 100644 --- a/rails_application/app/views/orders/edit.html.erb +++ b/rails_application/app/views/orders/edit.html.erb @@ -49,7 +49,7 @@ "> <% else %> "><%= button_to("Remove", remove_item_order_path(id: @order_id, product_id: product.id), class: "hover:underline text-blue-500") %> - <% end %> + <% end %> <% end %> diff --git a/rails_application/test/integration/client_orders_test.rb b/rails_application/test/integration/client_orders_test.rb index 88a807ad7..c0b92c276 100644 --- a/rails_application/test/integration/client_orders_test.rb +++ b/rails_application/test/integration/client_orders_test.rb @@ -15,6 +15,7 @@ def setup def test_happy_path arkency_id = register_customer('Arkency') async_remote_id = register_product("Async Remote", 39, 10) + fearless_id = register_product("Fearless Refactoring", 49, 10) Sidekiq::Job.drain_all get "/clients" @@ -29,6 +30,10 @@ def test_happy_path order_id = SecureRandom.uuid add_item_to_basket_for_order(async_remote_id, order_id) Sidekiq::Job.drain_all + get "/client_orders/#{order_id}/edit" + assert_match(/#{Regexp.escape(remove_item_client_order_path(id: order_id, product_id: async_remote_id))}/, response.body) + assert_no_match(/#{Regexp.escape(remove_item_client_order_path(id: order_id, product_id: fearless_id))}/, response.body) + submit_order_for_customer(arkency_id, order_id) Sidekiq::Job.drain_all get "/client_orders"