Skip to content

Commit

Permalink
Hide remove item button in the client panel
Browse files Browse the repository at this point in the history
  • Loading branch information
andrzejkrzywda committed Dec 7, 2023
1 parent 6cc1461 commit 642e218
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}",
Expand Down
10 changes: 7 additions & 3 deletions rails_application/app/views/client/orders/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,13 @@
<td class="py-2 text-right">
<%= button_to "Add", add_item_client_order_path(id: @order_id, product_id: product.uid), class: "hover:underline text-blue-500" %>
</td>
<td class="py-2 text-right">
<%= button_to "Remove", remove_item_client_order_path(id: @order_id, product_id: product.uid), class: "hover:underline text-blue-500" %>
</td>
<% if order_line.nil? %>
<td class="py-2 text-right" id="<%= "client_orders_#{product.uid}_remove_item_button" %>"></td>
<% else %>
<td class="py-2 text-right" id="<%= "client_orders_#{product.uid}_remove_item_button" %>">
<%= button_to("Remove", remove_item_client_order_path(id: @order_id, product_id: product.uid), class: "hover:underline text-blue-500") %>
</td>
<% end %>
</tr>
<% end %>
</tbody>
Expand Down
2 changes: 1 addition & 1 deletion rails_application/app/views/orders/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
<td class="py-2 text-right" id="<%= "orders_order_#{product.id}_remove_item_button" %>"></td>
<% else %>
<td class="py-2 text-right" id="<%= "orders_order_#{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") %></td>
<% end %>
<% end %>
</tr>
<% end %>
</tbody>
Expand Down
5 changes: 5 additions & 0 deletions rails_application/test/integration/client_orders_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down

0 comments on commit 642e218

Please sign in to comment.