Skip to content

Commit

Permalink
Incorporate ui/forms/address component in new orders/address comp…
Browse files Browse the repository at this point in the history
…onent

Leverage the existing `ui/forms/address` component to render the address forms
within the admin order process.
  • Loading branch information
rainerdema committed Nov 3, 2023
1 parent c3858ba commit 4e7004e
Show file tree
Hide file tree
Showing 8 changed files with 158 additions and 0 deletions.
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 admin/app/components/solidus_admin/orders/address/component.rb
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 admin/app/components/solidus_admin/orders/address/component.yml
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}"
2 changes: 2 additions & 0 deletions admin/app/controllers/solidus_admin/addresses_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ def new
address.country_id ||= default_country_id if address.country.nil?

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

Expand All @@ -25,6 +26,7 @@ def update
flash.now[:error] = @order.errors.full_messages.join(", ")

respond_to do |format|
format.html { render component('orders/address').new(order: @order, type: address_type), status: :unprocessable_entity }
end
end
end
Expand Down
1 change: 1 addition & 0 deletions admin/config/locales/orders.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ en:
orders:
title: "Orders"
addresses:
title: "Addresses"
address_type_invalid: "Invalid address type. Please select either billing or shipping address."
update:
success: "The address has been successfully updated."
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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= render current_component.new(order: order, type: type) %>
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

0 comments on commit 4e7004e

Please sign in to comment.