Skip to content

Commit

Permalink
Implement order/show/summary component using ui/details_list
Browse files Browse the repository at this point in the history
In this commit, we've added the `order/show/summary` component, 
leveraging
the recently introduced `ui/details_list`.
The component is specifically tailored to display order-related 
information
such as subtotal, taxes, shipping costs, promo code deductions,
adjustments, and the total amount.
  • Loading branch information
rainerdema committed Nov 17, 2023
1 parent e8cd2d4 commit 7a32cb6
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<%= page_with_sidebar do %>
<%= page_with_sidebar_main do %>
<%= render component("orders/cart").new(order: @order) %>
<%= render component("orders/show/summary").new(order: @order) %>
<% end %>

<%= page_with_sidebar_aside do %>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<div class="<%= stimulus_id %> w-full">
<%= render component('ui/panel').new(title: t('.summary')) do %>
<%= render component('ui/details_list').new(
items: [
{ label: t('.subtotal'), value: number_to_currency(@order.item_total), class: 'font-semibold' },
{ label: t('.taxes'), value: number_to_currency(@order.additional_tax_total) },
{ label: t('.shipping'), value: number_to_currency(@order.shipment_total) },
{ label: link_to(t('.add_promo_code'), '#', class: "body-link"), value: number_to_currency(@order.promo_total) },
{ label: link_to(t('.adjustments'), '#', class: "body-link"), value: number_to_currency(@order.adjustment_total) },
{ label: t('.total'), value: number_to_currency(@order.total), class: 'font-semibold' }
]
) %>
<% end %>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

class SolidusAdmin::Orders::Show::Summary::Component < SolidusAdmin::BaseComponent
def initialize(order:)
@order = order
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
en:
summary: Summary
subtotal: Subtotal
taxes: Taxes
shipping: Shipping
add_promo_code: Add Promo Code
adjustments: Adjustments
total: Total
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# frozen_string_literal: true

# @component "orders/show/summary"
class SolidusAdmin::Orders::Show::Summary::ComponentPreview < ViewComponent::Preview
include SolidusAdmin::Preview

def overview
order = fake_order(item_total: 340, additional_tax_total: 10, shipment_total: 20, promo_total: 10, adjustment_total: 20)
render_with_template(locals: { order: order })
end

# @param item_total [Float]
# @param additional_tax_total [Float]
# @param shipment_total [Float]
# @param promo_total [Float]
# @param adjustment_total [Float]
def playground(item_total: 100, additional_tax_total: 10, shipment_total: 5, promo_total: 0, adjustment_total: 0)
fake_order = fake_order(
item_total: item_total,
additional_tax_total: additional_tax_total,
shipment_total: shipment_total,
promo_total: promo_total,
adjustment_total: adjustment_total
)

render current_component.new(order: fake_order)
end

private

def fake_order(item_total:, additional_tax_total:, shipment_total:, promo_total:, adjustment_total:)
order = Spree::Order.new

order.define_singleton_method(:item_total) { item_total }
order.define_singleton_method(:additional_tax_total) { additional_tax_total }
order.define_singleton_method(:shipment_total) { shipment_total }
order.define_singleton_method(:promo_total) { promo_total }
order.define_singleton_method(:adjustment_total) { adjustment_total }
order.define_singleton_method(:total) {
item_total.to_f + additional_tax_total.to_f + shipment_total.to_f - promo_total.to_f - adjustment_total.to_f
}

order
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= render current_component.new(order: order) %>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

require "spec_helper"

RSpec.describe SolidusAdmin::Orders::Show::Summary::Component, type: :component do
it "renders the overview preview" do
render_preview(:overview)
end
end

0 comments on commit 7a32cb6

Please sign in to comment.