Skip to content

Commit 18604f4

Browse files
committed
Incorporate ui/forms/address component in new orders/address component
Leverage the existing `ui/forms/address` component to render the address forms within the admin order process.
1 parent a3facaa commit 18604f4

File tree

7 files changed

+157
-0
lines changed

7 files changed

+157
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<div class="<%= stimulus_id %>">
2+
<%= page do %>
3+
<%= page_header do %>
4+
<%= page_header_back(solidus_admin.edit_order_path(@order)) %>
5+
<%= page_header_title(t(".title", address: t(".#{address_type_action}_address"))) %>
6+
<%= page_header_actions do %>
7+
<%= render component("ui/button").new(
8+
tag: :a,
9+
scheme: :secondary,
10+
text: t(".cancel"),
11+
href: solidus_admin.edit_order_path(@order)
12+
) %>
13+
14+
<%= render component("ui/button").new(
15+
tag: :button,
16+
text: t(".save"),
17+
form: form_id
18+
) %>
19+
<% end %>
20+
<% end %>
21+
22+
<%= page_with_sidebar do %>
23+
<%= page_with_sidebar_main do %>
24+
<%= render component('ui/panel').new do %>
25+
<%= form_for @order, url: solidus_admin.send("order_#{@type}_address_path", @order), html: { id: form_id } do |form| %>
26+
<div class="w-full flex flex-col mb-4">
27+
<h2 class="text-sm mb-4 font-semibold"><%= t(".#{address_type_action}_address") %></h2>
28+
<div class="w-full flex gap-4">
29+
<%= form.fields_for :"#{@type}_address" do |address_form| %>
30+
<%= render component('ui/forms/address').new(form: address_form, disabled: false) %>
31+
<% end %>
32+
</div>
33+
34+
<label class="flex gap-2 items-center">
35+
<%= form.hidden_field "use_#{address_type_action}", value: '0', id: false %>
36+
37+
<%= render component("ui/forms/checkbox").new(
38+
name: "#{form.object_name}[use_#{address_type_action}]",
39+
checked: form.object.send("#{@type}_address").new_record? && form.object.bill_address == form.object.ship_address,
40+
value: '1'
41+
) %>
42+
43+
<span class="body-text-sm">
44+
<%= t(".use_this_address", type: "#{@type == 'ship' ? 'billing' : 'shipping'}") %>
45+
</span>
46+
</label>
47+
</div>
48+
<% end %>
49+
<% end %>
50+
<% end %>
51+
<% end %>
52+
53+
<%= page_footer do %>
54+
<%= page_footer_actions do %>
55+
<%= render component("ui/button").new(
56+
tag: :a,
57+
scheme: :secondary,
58+
text: t(".cancel"),
59+
href: solidus_admin.edit_order_path(@order)
60+
) %>
61+
62+
<%= render component("ui/button").new(
63+
tag: :button,
64+
text: t(".save"),
65+
form: form_id
66+
) %>
67+
<% end %>
68+
<% end %>
69+
<% end %>
70+
</div>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# frozen_string_literal: true
2+
3+
class SolidusAdmin::Orders::Address::Component < SolidusAdmin::BaseComponent
4+
include SolidusAdmin::Layout::PageHelpers
5+
6+
VALID_TYPES = ['ship', 'bill'].freeze
7+
8+
def initialize(order:, type: 'ship')
9+
@order = order
10+
@type = validate_address_type(type)
11+
end
12+
13+
def form_id
14+
@form_id ||= "#{stimulus_id}--form-#{@type}-#{@order.id}"
15+
end
16+
17+
def address_type_action
18+
(@type.end_with?('l') ? "#{@type}ing" : "#{@type}ping").to_s
19+
end
20+
21+
private
22+
23+
def validate_address_type(type)
24+
VALID_TYPES.include?(type) ? type : raise(ArgumentError, "Invalid address type: #{type}")
25+
end
26+
end
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Add your component translations here.
2+
# Use the translation in the example in your template with `t(".hello")`.
3+
en:
4+
save: Save
5+
cancel: Cancel
6+
back: Back
7+
title: "Edit %{address}"
8+
shipping_address: Shipping Address
9+
billing_address: Billing Address
10+
use_this_address: "Use this address also for %{type}"

admin/app/controllers/solidus_admin/addresses_controller.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ def new
1414
address.country_id ||= default_country_id if address.country.nil?
1515

1616
respond_to do |format|
17+
format.html { render component('orders/address').new(order: @order, type: address_type) }
1718
end
1819
end
1920

@@ -23,6 +24,7 @@ def update
2324
redirect_to edit_order_url(@order)
2425
else
2526
respond_to do |format|
27+
format.html { render component('orders/address').new(order: @order, type: address_type) }
2628
end
2729
end
2830
end
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# frozen_string_literal: true
2+
3+
# @component "orders/address"
4+
class SolidusAdmin::Orders::Address::ComponentPreview < ViewComponent::Preview
5+
include SolidusAdmin::Preview
6+
7+
def overview
8+
type = "ship"
9+
order = fake_order(type)
10+
11+
render_with_template(
12+
locals: {
13+
order: order,
14+
type: type
15+
}
16+
)
17+
end
18+
19+
# @param type select :type_options
20+
def playground(type: "ship")
21+
order = fake_order(type)
22+
render component("orders/address").new(order: order, type: type)
23+
end
24+
25+
private
26+
27+
def fake_order(type)
28+
order = Spree::Order.new
29+
order.define_singleton_method(:id) { 1 }
30+
order.define_singleton_method(:persisted?) { true }
31+
order.define_singleton_method(:to_param) { id.to_s }
32+
order.send("build_#{type}_address")
33+
order
34+
end
35+
36+
def type_options
37+
current_component::VALID_TYPES
38+
end
39+
end
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<%= render current_component.new(order: order, type: type) %>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# frozen_string_literal: true
2+
3+
require "spec_helper"
4+
5+
RSpec.describe SolidusAdmin::Orders::Address::Component, type: :component do
6+
it "renders the overview preview" do
7+
render_preview(:overview)
8+
end
9+
end

0 commit comments

Comments
 (0)