-
-
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.
Merge pull request #5461 from nebulab/rainerd/admin/order/address-form
[Admin] Order process: Integrate address form component for billing and shipping
- Loading branch information
Showing
20 changed files
with
278 additions
and
52 deletions.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
admin/app/components/solidus_admin/orders/show/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,44 @@ | ||
<div class="<%= stimulus_id %>"> | ||
<%= render component("orders/show").new(order: @order) %> | ||
<%= render component("ui/modal").new(title: t(".title.#{@type}"), close_path: solidus_admin.order_path(@order)) do |modal| %> | ||
<%= 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(".subtitle.#{@type}") %></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_attribute, value: '0', id: false %> | ||
|
||
<%= render component("ui/forms/checkbox").new( | ||
name: "#{form.object_name}[#{use_attribute}]", | ||
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}") %> | ||
</span> | ||
</label> | ||
</div> | ||
<% end %> | ||
|
||
<% modal.with_actions do %> | ||
<%= render component("ui/button").new( | ||
tag: :a, | ||
scheme: :secondary, | ||
text: t(".cancel"), | ||
href: solidus_admin.order_path(@order) | ||
) %> | ||
|
||
<%= render component("ui/button").new( | ||
tag: :button, | ||
text: t(".save"), | ||
form: form_id | ||
) %> | ||
<% end %> | ||
<% end %> | ||
</div> |
29 changes: 29 additions & 0 deletions
29
admin/app/components/solidus_admin/orders/show/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,29 @@ | ||
# frozen_string_literal: true | ||
|
||
class SolidusAdmin::Orders::Show::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 use_attribute | ||
case @type | ||
when 'ship' | ||
'use_shipping' | ||
when 'bill' | ||
'use_billing' | ||
end | ||
end | ||
|
||
def validate_address_type(type) | ||
VALID_TYPES.include?(type) ? type : raise(ArgumentError, "Invalid address type: #{type}") | ||
end | ||
end |
15 changes: 15 additions & 0 deletions
15
admin/app/components/solidus_admin/orders/show/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,15 @@ | ||
# 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: | ||
ship: Edit Shipping Address | ||
bill: Edit Billing Address | ||
subtitle: | ||
ship: Shipping Address | ||
bill: Billing Address | ||
use_this_address: | ||
ship: Use this address also for Billing | ||
bill: Use this address also for Shipping |
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
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
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
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
67 changes: 67 additions & 0 deletions
67
admin/app/controllers/solidus_admin/addresses_controller.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,67 @@ | ||
# frozen_string_literal: true | ||
|
||
module SolidusAdmin | ||
class AddressesController < BaseController | ||
include Spree::Core::ControllerHelpers::StrongParameters | ||
|
||
before_action :load_order | ||
before_action :validate_address_type | ||
|
||
def new | ||
address = @order.send("#{address_type}_address") | ||
@order.send("build_#{address_type}_address", country_id: default_country_id) if address.nil? | ||
address ||= @order.send("#{address_type}_address") | ||
address.country_id ||= default_country_id if address.country.nil? | ||
|
||
respond_to do |format| | ||
format.html { render component('orders/show/address').new(order: @order, type: address_type) } | ||
end | ||
end | ||
|
||
def update | ||
if @order.contents.update_cart(order_params) | ||
redirect_to order_path(@order), status: :see_other, notice: t('.success') | ||
else | ||
flash.now[:error] = @order.errors[:base].join(", ") if @order.errors[:base].any? | ||
|
||
respond_to do |format| | ||
format.html { render component('orders/show/address').new(order: @order, type: address_type), status: :unprocessable_entity } | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
def address_type | ||
params[:type].presence_in(%w[bill ship]) | ||
end | ||
|
||
def validate_address_type | ||
unless address_type | ||
flash[:error] = t('.errors.address_type_invalid') | ||
redirect_to spree.admin_order_url(@order) | ||
end | ||
end | ||
|
||
def default_country_id | ||
@default_country_id ||= begin | ||
country = Spree::Country.default | ||
country.id if Spree::Country.available.exists?(id: country.id) | ||
end | ||
end | ||
|
||
def load_order | ||
@order = Spree::Order.find_by!(number: params[:order_id]) | ||
authorize! action_name, @order | ||
end | ||
|
||
def order_params | ||
params.require(:order).permit( | ||
:use_billing, | ||
:use_shipping, | ||
bill_address_attributes: permitted_address_attributes, | ||
ship_address_attributes: permitted_address_attributes | ||
) | ||
end | ||
end | ||
end |
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
41 changes: 41 additions & 0 deletions
41
admin/spec/components/previews/solidus_admin/orders/show/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,41 @@ | ||
# frozen_string_literal: true | ||
|
||
# @component "orders/show/address" | ||
class SolidusAdmin::Orders::Show::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 current_component.new(order: order, type: type) | ||
end | ||
|
||
private | ||
|
||
def fake_order(type) | ||
order = Spree::Order.new | ||
country = Spree::Country.find_or_initialize_by(iso: Spree::Config.default_country_iso) | ||
|
||
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", { country: country }) | ||
order | ||
end | ||
|
||
def type_options | ||
current_component::VALID_TYPES | ||
end | ||
end |
1 change: 1 addition & 0 deletions
1
...components/previews/solidus_admin/orders/show/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/show/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::Show::Address::Component, type: :component do | ||
it "renders the overview preview" do | ||
render_preview(:overview) | ||
end | ||
end |
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
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
Oops, something went wrong.