From 57ec0b849d305c21f14e234ed58adccbda7ccf8c Mon Sep 17 00:00:00 2001 From: Rainer Dema Date: Tue, 14 Nov 2023 14:26:07 +0100 Subject: [PATCH 1/6] Extend `ui/forms/field` component for string form names Updated `ui/forms/field` to support string form names and objects, in addition to form objects. Added `extract_form_details` method for handling argument extraction and validation. --- .../orders/show/address/component.html.erb | 4 +- .../orders/show/address/component.rb | 3 +- .../ui/forms/address/component.html.erb | 24 +++++----- .../ui/forms/address/component.rb | 9 ++-- .../solidus_admin/ui/forms/field/component.rb | 47 +++++++++++++------ .../solidus_admin/addresses_controller.rb | 11 ++++- .../orders/show/address/component_preview.rb | 3 +- .../component_preview/overview.html.erb | 2 +- .../ui/forms/address/component_preview.rb | 13 +++-- .../component_preview/overview.html.erb | 4 +- 10 files changed, 76 insertions(+), 44 deletions(-) diff --git a/admin/app/components/solidus_admin/orders/show/address/component.html.erb b/admin/app/components/solidus_admin/orders/show/address/component.html.erb index f8a547ac963..1a341f0ca1c 100644 --- a/admin/app/components/solidus_admin/orders/show/address/component.html.erb +++ b/admin/app/components/solidus_admin/orders/show/address/component.html.erb @@ -5,9 +5,7 @@

<%= t(".subtitle.#{@type}") %>

- <%= form.fields_for :"#{@type}_address" do |address_form| %> - <%= render component('ui/forms/address').new(form: address_form, disabled: false) %> - <% end %> + <%= render component('ui/forms/address').new(address: @address, name: "order[#{@type}_address_attributes]") %>
@@ -58,7 +58,7 @@ <%= @order.class.human_attribute_name(:bill_address) %>
<% if @order.bill_address.blank? %> - <%= link_to t(".add_billing"), solidus_admin.new_order_bill_address_path(@order), class: 'body-link' %> + <%= link_to t(".add_billing"), solidus_admin.edit_order_bill_address_path(@order), class: 'body-link' %> <% elsif @order.bill_address == @order.ship_address %> <%= t('.same_as_shipping') %> <% else %> diff --git a/admin/app/controllers/solidus_admin/addresses_controller.rb b/admin/app/controllers/solidus_admin/addresses_controller.rb index 5e86c734d9d..0eef22fad42 100644 --- a/admin/app/controllers/solidus_admin/addresses_controller.rb +++ b/admin/app/controllers/solidus_admin/addresses_controller.rb @@ -7,17 +7,24 @@ class AddressesController < BaseController 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? + def show + address = find_address || build_new_address respond_to do |format| - format.html { render component('orders/show/address').new(order: @order, address: address, type: address_type) } + format.html do + render component('orders/show/address').new( + order: @order, + address: address, + type: address_type, + ) + end end end + def edit + redirect_to action: :show + end + def update if @order.contents.update_cart(order_params) redirect_to order_path(@order), status: :see_other, notice: t('.success') @@ -39,6 +46,20 @@ def update private + def find_address + if params[:address_id].present? && @order.user + @order.user.addresses.find(params[:address_id]) + else + @order.send("#{address_type}_address") + end + end + + def build_new_address + @order.send("build_#{address_type}_address", country_id: default_country_id).tap do |address| + address.country_id ||= default_country_id if address.country.nil? + end + end + def address_type params[:type].presence_in(%w[bill ship]) end diff --git a/admin/config/routes.rb b/admin/config/routes.rb index d0c3a997fa9..6b45f676195 100644 --- a/admin/config/routes.rb +++ b/admin/config/routes.rb @@ -21,8 +21,8 @@ resources :orders, only: [:index, :show, :edit, :update] do resources :line_items, only: [:destroy, :create, :update] resource :customer - resource :ship_address, only: [:new, :update], controller: "addresses", type: "ship" - resource :bill_address, only: [:new, :update], controller: "addresses", type: "bill" + resource :ship_address, only: [:show, :edit, :update], controller: "addresses", type: "ship" + resource :bill_address, only: [:show, :edit, :update], controller: "addresses", type: "bill" member do get :variants_for From d24e4d1627f4ab73a9b91447933e8977d4df8039 Mon Sep 17 00:00:00 2001 From: Rainer Dema Date: Tue, 14 Nov 2023 14:42:48 +0100 Subject: [PATCH 3/6] Add "Select address" feature Implemented the functionality for the 'Select Address' feature. This addition includes dynamic handling of address selection, enabling users to pick from a list of existing associated addresses. --- .../orders/show/address/component.html.erb | 33 +++++++++++++++++-- .../orders/show/address/component.rb | 22 +++++++++++-- .../orders/show/address/component.yml | 1 + .../solidus_admin/addresses_controller.rb | 2 ++ 4 files changed, 54 insertions(+), 4 deletions(-) diff --git a/admin/app/components/solidus_admin/orders/show/address/component.html.erb b/admin/app/components/solidus_admin/orders/show/address/component.html.erb index 1a341f0ca1c..d0499a49586 100644 --- a/admin/app/components/solidus_admin/orders/show/address/component.html.erb +++ b/admin/app/components/solidus_admin/orders/show/address/component.html.erb @@ -1,9 +1,38 @@ -
+
<%= 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| %>
-

<%= t(".subtitle.#{@type}") %>

+
+

+ <%= t(".subtitle.#{@type}") %> +

+ + <% if @user&.addresses&.any? %> +
+ + <%= t(".select_address") %> + <%= render component("ui/icon").new(name: 'arrow-down-s-fill', class: 'w-5 h-5') %> + + +
+ <% @user.addresses.each do |address| %> + <%= tag.a( + href: solidus_admin.send("order_#{@type}_address_path", @order, address_id: address.id), + class: 'block text-black text-sm hover:bg-gray-50 p-2 mx-2 w-auto rounded-lg', + ) do %> + <%= format_address(address) %> + <% end %> + <% end %> +
+
+ <% end %> +
+
<%= render component('ui/forms/address').new(address: @address, name: "order[#{@type}_address_attributes]") %>
diff --git a/admin/app/components/solidus_admin/orders/show/address/component.rb b/admin/app/components/solidus_admin/orders/show/address/component.rb index dd434dc2b64..e0e347337de 100644 --- a/admin/app/components/solidus_admin/orders/show/address/component.rb +++ b/admin/app/components/solidus_admin/orders/show/address/component.rb @@ -5,10 +5,11 @@ class SolidusAdmin::Orders::Show::Address::Component < SolidusAdmin::BaseCompone VALID_TYPES = ['ship', 'bill'].freeze - def initialize(order:, address:, type: 'ship') + def initialize(order:, address:, user: nil, type: 'ship') @order = order - @type = validate_address_type(type) + @user = user @address = address + @type = validate_address_type(type) end def form_id @@ -24,6 +25,23 @@ def use_attribute end end + def format_address(address) + safe_join([ + address.name, + tag.br, + address.address1, + tag.br, + address.address2, + address.city, + address.zipcode, + address.state&.name, + tag.br, + address.country.name, + tag.br, + address.phone, + ], " ") + end + def validate_address_type(type) VALID_TYPES.include?(type) ? type : raise(ArgumentError, "Invalid address type: #{type}") end diff --git a/admin/app/components/solidus_admin/orders/show/address/component.yml b/admin/app/components/solidus_admin/orders/show/address/component.yml index e406088f45f..d7c82068537 100644 --- a/admin/app/components/solidus_admin/orders/show/address/component.yml +++ b/admin/app/components/solidus_admin/orders/show/address/component.yml @@ -4,6 +4,7 @@ en: save: Save cancel: Cancel back: Back + select_address: Select address title: ship: Edit Shipping Address bill: Edit Billing Address diff --git a/admin/app/controllers/solidus_admin/addresses_controller.rb b/admin/app/controllers/solidus_admin/addresses_controller.rb index 0eef22fad42..9a7b8e20c2d 100644 --- a/admin/app/controllers/solidus_admin/addresses_controller.rb +++ b/admin/app/controllers/solidus_admin/addresses_controller.rb @@ -14,6 +14,7 @@ def show format.html do render component('orders/show/address').new( order: @order, + user: @order.user, address: address, type: address_type, ) @@ -35,6 +36,7 @@ def update format.html do render component('orders/show/address').new( order: @order, + user: @order.user, address: @order.send("#{address_type}_address"), type: address_type, status: :unprocessable_entity, From cc878bd695ac1874009917fedc9f982145a31e23 Mon Sep 17 00:00:00 2001 From: Rainer Dema Date: Tue, 14 Nov 2023 14:44:34 +0100 Subject: [PATCH 4/6] Implement close after click and ESC event for address menu --- .../solidus_admin/orders/show/address/component.html.erb | 3 ++- .../solidus_admin/orders/show/address/component.js | 9 +++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) create mode 100644 admin/app/components/solidus_admin/orders/show/address/component.js diff --git a/admin/app/components/solidus_admin/orders/show/address/component.html.erb b/admin/app/components/solidus_admin/orders/show/address/component.html.erb index d0499a49586..c6a1811fbea 100644 --- a/admin/app/components/solidus_admin/orders/show/address/component.html.erb +++ b/admin/app/components/solidus_admin/orders/show/address/component.html.erb @@ -10,7 +10,7 @@ <% if @user&.addresses&.any? %> -
+
-target="addresses"> <%= format_address(address) %> <% end %> diff --git a/admin/app/components/solidus_admin/orders/show/address/component.js b/admin/app/components/solidus_admin/orders/show/address/component.js new file mode 100644 index 00000000000..ebb6df4ab34 --- /dev/null +++ b/admin/app/components/solidus_admin/orders/show/address/component.js @@ -0,0 +1,9 @@ +import { Controller } from '@hotwired/stimulus' + +export default class extends Controller { + static targets = ["addresses"] + + close() { + this.addressesTarget.removeAttribute('open') + } +} From 4e053bf2408b299aee2276aa820dc504716ff46a Mon Sep 17 00:00:00 2001 From: Rainer Dema Date: Tue, 14 Nov 2023 15:09:10 +0100 Subject: [PATCH 5/6] Integrate turbo frame for address form --- .../solidus_admin/orders/show/address/component.html.erb | 5 ++++- .../solidus_admin/orders/show/address/component.rb | 4 ++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/admin/app/components/solidus_admin/orders/show/address/component.html.erb b/admin/app/components/solidus_admin/orders/show/address/component.html.erb index c6a1811fbea..f3e1600f2ab 100644 --- a/admin/app/components/solidus_admin/orders/show/address/component.html.erb +++ b/admin/app/components/solidus_admin/orders/show/address/component.html.erb @@ -25,6 +25,7 @@ href: solidus_admin.send("order_#{@type}_address_path", @order, address_id: address.id), class: 'block text-black text-sm hover:bg-gray-50 p-2 mx-2 w-auto rounded-lg', 'data-action': "#{stimulus_id}#close", + 'data-turbo-frame': address_frame_id ) do %> <%= format_address(address) %> <% end %> @@ -35,7 +36,9 @@
- <%= render component('ui/forms/address').new(address: @address, name: "order[#{@type}_address_attributes]") %> + <%= turbo_frame_tag address_frame_id do %> + <%= render component('ui/forms/address').new(address: @address, name: "order[#{@type}_address_attributes]") %> + <% end %>