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