-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Incorporate
ui/forms/address
component in new orders/address
comp…
…onent Leverage the existing `ui/forms/address` component to render the address forms within the admin order process.
- Loading branch information
1 parent
c3858ba
commit 4e7004e
Showing
8 changed files
with
158 additions
and
0 deletions.
There are no files selected for viewing
70 changes: 70 additions & 0 deletions
70
admin/app/components/solidus_admin/orders/address/component.html.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<div class="<%= stimulus_id %>"> | ||
<%= page do %> | ||
<%= page_header do %> | ||
<%= page_header_back(solidus_admin.edit_order_path(@order)) %> | ||
<%= page_header_title(t(".title", address: t(".#{address_type_action}_address"))) %> | ||
<%= page_header_actions do %> | ||
<%= render component("ui/button").new( | ||
tag: :a, | ||
scheme: :secondary, | ||
text: t(".cancel"), | ||
href: solidus_admin.edit_order_path(@order) | ||
) %> | ||
|
||
<%= render component("ui/button").new( | ||
tag: :button, | ||
text: t(".save"), | ||
form: form_id | ||
) %> | ||
<% end %> | ||
<% end %> | ||
|
||
<%= page_with_sidebar do %> | ||
<%= page_with_sidebar_main do %> | ||
<%= render component('ui/panel').new do %> | ||
<%= form_for @order, url: solidus_admin.send("order_#{@type}_address_path", @order), html: { id: form_id } do |form| %> | ||
<div class="w-full flex flex-col mb-4"> | ||
<h2 class="text-sm mb-4 font-semibold"><%= t(".#{address_type_action}_address") %></h2> | ||
<div class="w-full flex gap-4"> | ||
<%= form.fields_for :"#{@type}_address" do |address_form| %> | ||
<%= render component('ui/forms/address').new(form: address_form, disabled: false) %> | ||
<% end %> | ||
</div> | ||
|
||
<label class="flex gap-2 items-center"> | ||
<%= form.hidden_field "use_#{address_type_action}", value: '0', id: false %> | ||
|
||
<%= render component("ui/forms/checkbox").new( | ||
name: "#{form.object_name}[use_#{address_type_action}]", | ||
checked: form.object.send("#{@type}_address").new_record? && form.object.bill_address == form.object.ship_address, | ||
value: '1' | ||
) %> | ||
|
||
<span class="body-text-sm"> | ||
<%= t(".use_this_address", type: "#{@type == 'ship' ? 'billing' : 'shipping'}") %> | ||
</span> | ||
</label> | ||
</div> | ||
<% end %> | ||
<% end %> | ||
<% end %> | ||
<% end %> | ||
|
||
<%= page_footer do %> | ||
<%= page_footer_actions do %> | ||
<%= render component("ui/button").new( | ||
tag: :a, | ||
scheme: :secondary, | ||
text: t(".cancel"), | ||
href: solidus_admin.edit_order_path(@order) | ||
) %> | ||
|
||
<%= render component("ui/button").new( | ||
tag: :button, | ||
text: t(".save"), | ||
form: form_id | ||
) %> | ||
<% end %> | ||
<% end %> | ||
<% end %> | ||
</div> |
26 changes: 26 additions & 0 deletions
26
admin/app/components/solidus_admin/orders/address/component.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# frozen_string_literal: true | ||
|
||
class SolidusAdmin::Orders::Address::Component < SolidusAdmin::BaseComponent | ||
include SolidusAdmin::Layout::PageHelpers | ||
|
||
VALID_TYPES = ['ship', 'bill'].freeze | ||
|
||
def initialize(order:, type: 'ship') | ||
@order = order | ||
@type = validate_address_type(type) | ||
end | ||
|
||
def form_id | ||
@form_id ||= "#{stimulus_id}--form-#{@type}-#{@order.id}" | ||
end | ||
|
||
def address_type_action | ||
(@type.end_with?('l') ? "#{@type}ing" : "#{@type}ping").to_s | ||
end | ||
|
||
private | ||
|
||
def validate_address_type(type) | ||
VALID_TYPES.include?(type) ? type : raise(ArgumentError, "Invalid address type: #{type}") | ||
end | ||
end |
10 changes: 10 additions & 0 deletions
10
admin/app/components/solidus_admin/orders/address/component.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# Add your component translations here. | ||
# Use the translation in the example in your template with `t(".hello")`. | ||
en: | ||
save: Save | ||
cancel: Cancel | ||
back: Back | ||
title: "Edit %{address}" | ||
shipping_address: Shipping Address | ||
billing_address: Billing Address | ||
use_this_address: "Use this address also for %{type}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
admin/spec/components/previews/solidus_admin/orders/address/component_preview.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# frozen_string_literal: true | ||
|
||
# @component "orders/address" | ||
class SolidusAdmin::Orders::Address::ComponentPreview < ViewComponent::Preview | ||
include SolidusAdmin::Preview | ||
|
||
def overview | ||
type = "ship" | ||
order = fake_order(type) | ||
|
||
render_with_template( | ||
locals: { | ||
order: order, | ||
type: type | ||
} | ||
) | ||
end | ||
|
||
# @param type select :type_options | ||
def playground(type: "ship") | ||
order = fake_order(type) | ||
render component("orders/address").new(order: order, type: type) | ||
end | ||
|
||
private | ||
|
||
def fake_order(type) | ||
order = Spree::Order.new | ||
order.define_singleton_method(:id) { 1 } | ||
order.define_singleton_method(:persisted?) { true } | ||
order.define_singleton_method(:to_param) { id.to_s } | ||
order.send("build_#{type}_address") | ||
order | ||
end | ||
|
||
def type_options | ||
current_component::VALID_TYPES | ||
end | ||
end |
1 change: 1 addition & 0 deletions
1
...spec/components/previews/solidus_admin/orders/address/component_preview/overview.html.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<%= render current_component.new(order: order, type: type) %> |
9 changes: 9 additions & 0 deletions
9
admin/spec/components/solidus_admin/orders/address/component_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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::Address::Component, type: :component do | ||
it "renders the overview preview" do | ||
render_preview(:overview) | ||
end | ||
end |