diff --git a/admin/app/components/solidus_admin/orders/show/component.html.erb b/admin/app/components/solidus_admin/orders/show/component.html.erb index 4527f809f58..a92fa6ea60c 100644 --- a/admin/app/components/solidus_admin/orders/show/component.html.erb +++ b/admin/app/components/solidus_admin/orders/show/component.html.erb @@ -27,6 +27,8 @@
<%= customer_name(@order.user) || tag.span(t('.no_name'), class: "text-gray-500") %>
<%= t(".orders_count", count: @order.user.orders.complete.count) %>
+ <% else %> + <%= render component('orders/show/customer_search').new(order: @order) %> <% end %> diff --git a/admin/app/components/solidus_admin/orders/show/customer_search/component.html.erb b/admin/app/components/solidus_admin/orders/show/customer_search/component.html.erb new file mode 100644 index 00000000000..e867f7dd7d2 --- /dev/null +++ b/admin/app/components/solidus_admin/orders/show/customer_search/component.html.erb @@ -0,0 +1,14 @@ +
-customers-url-value="<%= solidus_admin.customers_for_order_path(@order) %>" + data-action=" + <%= component('ui/forms/search').stimulus_id %>:search-><%= stimulus_id %>#search + <%= component('ui/forms/search').stimulus_id %>:submit-><%= stimulus_id %>#submit + " +> + <%= render component("ui/forms/search").new( + placeholder: t(".placeholder"), + id: :order_customer + ) %> +
diff --git a/admin/app/components/solidus_admin/orders/show/customer_search/component.js b/admin/app/components/solidus_admin/orders/show/customer_search/component.js new file mode 100644 index 00000000000..68e6b65b05b --- /dev/null +++ b/admin/app/components/solidus_admin/orders/show/customer_search/component.js @@ -0,0 +1,14 @@ +import { Controller } from '@hotwired/stimulus' + +export default class extends Controller { + static values = { customersUrl: String } + + async search({ detail: { query, controller } }) { + controller.resultsValue = + (await (await fetch(`${this.customersUrlValue}?q[name_or_variants_including_master_sku_cont]=${query}`)).text()) + } + + submit(event) { + event.detail.resultTarget.querySelector('form').submit() + } +} diff --git a/admin/app/components/solidus_admin/orders/show/customer_search/component.rb b/admin/app/components/solidus_admin/orders/show/customer_search/component.rb new file mode 100644 index 00000000000..1e3b8c2e385 --- /dev/null +++ b/admin/app/components/solidus_admin/orders/show/customer_search/component.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +class SolidusAdmin::Orders::Show::CustomerSearch::Component < SolidusAdmin::BaseComponent + def initialize(order:) + @order = order + end +end diff --git a/admin/app/components/solidus_admin/orders/show/customer_search/component.yml b/admin/app/components/solidus_admin/orders/show/customer_search/component.yml new file mode 100644 index 00000000000..7d25505767a --- /dev/null +++ b/admin/app/components/solidus_admin/orders/show/customer_search/component.yml @@ -0,0 +1,2 @@ +en: + placeholder: "Search customer" diff --git a/admin/app/components/solidus_admin/orders/show/customer_search/result/component.html.erb b/admin/app/components/solidus_admin/orders/show/customer_search/result/component.html.erb new file mode 100644 index 00000000000..ebe5c4ef2dd --- /dev/null +++ b/admin/app/components/solidus_admin/orders/show/customer_search/result/component.html.erb @@ -0,0 +1,17 @@ +<%= render component('ui/forms/search/result').new do %> + <% if @customer %> + <%= form_for(@order, url: solidus_admin.order_path(@order), html: { + "data-controller": "readonly-when-submitting", + class: "flex items-center", + }) do |f| %> + <%= hidden_field_tag("#{f.object_name}[user_id]", @customer.id) %> + + <% end %> + <% end %> +<% end %> diff --git a/admin/app/components/solidus_admin/orders/show/customer_search/result/component.rb b/admin/app/components/solidus_admin/orders/show/customer_search/result/component.rb new file mode 100644 index 00000000000..695255a3ff4 --- /dev/null +++ b/admin/app/components/solidus_admin/orders/show/customer_search/result/component.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +class SolidusAdmin::Orders::Show::CustomerSearch::Result::Component < SolidusAdmin::BaseComponent + with_collection_parameter :customer + + def initialize(order:, customer:) + @order = order + @customer = customer + @name = (customer.default_user_bill_address || customer.default_user_ship_address)&.address&.name if customer + end +end diff --git a/admin/app/controllers/solidus_admin/orders_controller.rb b/admin/app/controllers/solidus_admin/orders_controller.rb index 71044becabc..8570029ffd8 100644 --- a/admin/app/controllers/solidus_admin/orders_controller.rb +++ b/admin/app/controllers/solidus_admin/orders_controller.rb @@ -74,6 +74,22 @@ def variants_for end end + def customers_for + load_order + + @users = Spree.user_class + .where.not(id: @order.user_id) + .order(created_at: :desc, id: :desc) + .ransack(params[:q]) + .result(distinct: true) + .includes(:default_user_bill_address, :default_user_ship_address) + .limit(10) + + respond_to do |format| + format.html { render component('orders/show/customer_search/result').with_collection(@users, order: @order), layout: false } + end + end + private def load_order diff --git a/admin/config/routes.rb b/admin/config/routes.rb index 9322532bd18..d0c3a997fa9 100644 --- a/admin/config/routes.rb +++ b/admin/config/routes.rb @@ -26,6 +26,7 @@ member do get :variants_for + get :customers_for end end end