Skip to content

Commit

Permalink
Hide remove item button when item not possible to remove
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
andrzejkrzywda committed Dec 6, 2023
1 parent 42b5f29 commit 6cc1461
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 2 deletions.
5 changes: 5 additions & 0 deletions rails_application/app/read_models/orders/add_item_to_order.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 6 additions & 2 deletions rails_application/app/views/orders/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,12 @@
<td class="py-2" id="<%= "orders_order_#{product.id}_quantity" %>"><%= order_line.try(&:quantity) || 0 %></td>
<td class="py-2"><%= number_to_currency(product.price) %></td>
<td class="py-2" id="<%= "orders_order_#{product.id}_value" %>"><%= number_to_currency(order_line.try(&:value)) %></td>
<td class="py-2 text-right"><%= button_to "Add", add_item_order_path(id: @order_id, product_id: product.id), class: "hover:underline text-blue-500" %></td>
<td class="py-2 text-right"><%= button_to "Remove", remove_item_order_path(id: @order_id, product_id: product.id), class: "hover:underline text-blue-500" %></td>
<td class="py-2"><%= button_to "Add", add_item_order_path(id: @order_id, product_id: product.id), class: "hover:underline text-blue-500" %></td>
<% if order_line.nil? %>
<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 %>
</tr>
<% end %>
</tbody>
Expand Down
22 changes: 22 additions & 0 deletions rails_application/test/integration/orders_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.")
Expand Down

0 comments on commit 6cc1461

Please sign in to comment.