Skip to content

Commit

Permalink
Add a customer picker for the order page
Browse files Browse the repository at this point in the history
  • Loading branch information
elia committed Nov 10, 2023
1 parent 1fb1f98 commit de59f4a
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
<div class="body-small"><%= customer_name(@order.user) || tag.span(t('.no_name'), class: "text-gray-500") %></div>
<div class="body-small body-link"><%= link_to @order.user.email, spree.admin_user_path(@order.user) %></div>
<div class="body-small text-gray-500"><%= t(".orders_count", count: @order.user.orders.complete.count) %></div>
<% else %>
<%= render component('orders/show/customer_search').new(order: @order) %>
<% end %>
</div>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<div
class="w-full relative overflow-visible"
data-controller="<%= stimulus_id %>"
data-<%= stimulus_id %>-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
) %>
</div>
Original file line number Diff line number Diff line change
@@ -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()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

class SolidusAdmin::Orders::Show::CustomerSearch::Component < SolidusAdmin::BaseComponent
def initialize(order:)
@order = order
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
en:
placeholder: "Search customer"
Original file line number Diff line number Diff line change
@@ -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) %>
<button type="submit" class="flex gap-2 grow items-center">
<%= render component("ui/icon").new(name: "user-line", class: 'w-5 h-5 m-2') %>
<div class="flex-col text-left">
<div class="leading-5 text-black body-small-bold"><%= @name %></div>
<div class="leading-5 text-gray-500 body-small"><%= @customer.email %></div>
</div>
</button>
<% end %>
<% end %>
<% end %>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

class SolidusAdmin::Orders::Show::CustomerSearch::Result::Component < SolidusAdmin::BaseComponent
with_collection_parameter :customer

Check warning on line 4 in admin/app/components/solidus_admin/orders/show/customer_search/result/component.rb

View check run for this annotation

Codecov / codecov/patch

admin/app/components/solidus_admin/orders/show/customer_search/result/component.rb#L3-L4

Added lines #L3 - L4 were not covered by tests

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

Check warning on line 11 in admin/app/components/solidus_admin/orders/show/customer_search/result/component.rb

View check run for this annotation

Codecov / codecov/patch

admin/app/components/solidus_admin/orders/show/customer_search/result/component.rb#L6-L11

Added lines #L6 - L11 were not covered by tests
16 changes: 16 additions & 0 deletions admin/app/controllers/solidus_admin/orders_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,22 @@ def variants_for
end
end

def customers_for
load_order

Check warning on line 78 in admin/app/controllers/solidus_admin/orders_controller.rb

View check run for this annotation

Codecov / codecov/patch

admin/app/controllers/solidus_admin/orders_controller.rb#L78

Added line #L78 was not covered by tests

@users = Spree.user_class

Check warning on line 80 in admin/app/controllers/solidus_admin/orders_controller.rb

View check run for this annotation

Codecov / codecov/patch

admin/app/controllers/solidus_admin/orders_controller.rb#L80

Added line #L80 was not covered by tests
.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 }

Check warning on line 89 in admin/app/controllers/solidus_admin/orders_controller.rb

View check run for this annotation

Codecov / codecov/patch

admin/app/controllers/solidus_admin/orders_controller.rb#L88-L89

Added lines #L88 - L89 were not covered by tests
end
end

private

def load_order
Expand Down
1 change: 1 addition & 0 deletions admin/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

member do
get :variants_for
get :customers_for
end
end
end

0 comments on commit de59f4a

Please sign in to comment.