-
-
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 #5869 from MadelineCollier/admin-user-admin-order-…
…history [Admin] Add new users admin order history page
- Loading branch information
Showing
9 changed files
with
287 additions
and
5 deletions.
There are no files selected for viewing
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
52 changes: 52 additions & 0 deletions
52
admin/app/components/solidus_admin/users/orders/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,52 @@ | ||
<%= page do %> | ||
<%= page_header do %> | ||
<%= page_header_back(solidus_admin.users_path) %> | ||
<%= page_header_title(t(".title", email: @user.email)) %> | ||
|
||
<%= page_header_actions do %> | ||
<%= render component("ui/button").new(tag: :a, text: t(".create_order_for_user"), href: spree.new_admin_order_path(user_id: @user.id)) %> | ||
<% end %> | ||
<% end %> | ||
|
||
<%= page_header do %> | ||
<% tabs.each do |tab| %> | ||
<%= render(component("ui/button").new(tag: :a, scheme: :ghost, text: tab[:text], 'aria-current': tab[:current], href: tab[:href])) %> | ||
<% end %> | ||
<% end %> | ||
|
||
<%= page_with_sidebar do %> | ||
<%= page_with_sidebar_main do %> | ||
<%= render component('ui/panel').new(title: t(".order_history")) do %> | ||
<% if @orders.present? %> | ||
<%= render component('ui/table').new( | ||
id: stimulus_id, | ||
data: { | ||
class: model_class, | ||
rows: rows, | ||
fade: -> { row_fade(_1) }, | ||
columns: columns, | ||
url: -> { row_url(_1) }, | ||
}, | ||
)%> | ||
<% else %> | ||
<%= t(".no_orders_found") %> | ||
<%= render component("ui/button").new(tag: :a, text: t(".create_one"), href: spree.new_admin_order_path(user_id: @user.id)) %> | ||
<% end %> | ||
<% end %> | ||
<% end %> | ||
|
||
<%= page_with_sidebar_aside do %> | ||
<%= render component("ui/panel").new(title: t("spree.lifetime_stats")) do %> | ||
<%= render component("ui/details_list").new( | ||
items: [ | ||
{ label: t("spree.total_sales"), value: @user.display_lifetime_value.to_html }, | ||
{ label: t("spree.order_count"), value: @user.order_count.to_i }, | ||
{ label: t("spree.average_order_value"), value: @user.display_average_order_value.to_html }, | ||
{ label: t("spree.member_since"), value: @user.created_at.to_date }, | ||
{ label: t(".last_active"), value: last_login(@user) }, | ||
] | ||
) %> | ||
<% end %> | ||
<% end %> | ||
<% end %> | ||
<% end %> |
145 changes: 145 additions & 0 deletions
145
admin/app/components/solidus_admin/users/orders/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,145 @@ | ||
# frozen_string_literal: true | ||
|
||
class SolidusAdmin::Users::Orders::Component < SolidusAdmin::BaseComponent | ||
include SolidusAdmin::Layout::PageHelpers | ||
|
||
def initialize(user:, orders:) | ||
@user = user | ||
@orders = orders | ||
end | ||
|
||
def form_id | ||
@form_id ||= "#{stimulus_id}--form-#{@user.id}" | ||
end | ||
|
||
def tabs | ||
[ | ||
{ | ||
text: t('.account'), | ||
href: solidus_admin.user_path(@user), | ||
current: false, | ||
}, | ||
{ | ||
text: t('.addresses'), | ||
href: solidus_admin.addresses_user_path(@user), | ||
current: false, | ||
}, | ||
{ | ||
text: t('.order_history'), | ||
href: solidus_admin.orders_user_path(@user), | ||
current: true, | ||
}, | ||
{ | ||
text: t('.items'), | ||
href: spree.items_admin_user_path(@user), | ||
current: false, | ||
}, | ||
{ | ||
text: t('.store_credit'), | ||
href: spree.admin_user_store_credits_path(@user), | ||
current: false, | ||
}, | ||
] | ||
end | ||
|
||
def last_login(user) | ||
return t('.last_login.never') if user.try(:last_sign_in_at).blank? | ||
|
||
t( | ||
'.last_login.login_time_ago', | ||
# @note The second `.try` is only here for the specs to work. | ||
last_login_time: time_ago_in_words(user.try(:last_sign_in_at)) | ||
).capitalize | ||
end | ||
|
||
def model_class | ||
Spree::Order | ||
end | ||
|
||
def row_url(order) | ||
spree.edit_admin_order_path(order) | ||
end | ||
|
||
def rows | ||
@orders | ||
end | ||
|
||
def row_fade(_order) | ||
false | ||
end | ||
|
||
def columns | ||
[ | ||
number_column, | ||
state_column, | ||
date_column, | ||
payment_column, | ||
shipment_column, | ||
total_column, | ||
] | ||
end | ||
|
||
def number_column | ||
{ | ||
header: :order, | ||
data: ->(order) do | ||
if !row_fade(order) | ||
content_tag :div, order.number, class: 'font-semibold' | ||
else | ||
content_tag :div, order.number | ||
end | ||
end | ||
} | ||
end | ||
|
||
def state_column | ||
{ | ||
header: :state, | ||
data: ->(order) do | ||
color = { | ||
'complete' => :green, | ||
'returned' => :red, | ||
'canceled' => :blue, | ||
'cart' => :graphite_light, | ||
}[order.state] || :yellow | ||
component('ui/badge').new(name: order.state.humanize, color: color) | ||
end | ||
} | ||
end | ||
|
||
def date_column | ||
{ | ||
header: :date, | ||
data: ->(order) do | ||
content_tag :div, l(order.created_at, format: :short) | ||
end | ||
} | ||
end | ||
|
||
def total_column | ||
{ | ||
header: :total, | ||
data: ->(order) do | ||
content_tag :div, number_to_currency(order.total) | ||
end | ||
} | ||
end | ||
|
||
def payment_column | ||
{ | ||
header: :payment, | ||
data: ->(order) do | ||
component('ui/badge').new(name: order.payment_state.humanize, color: order.paid? ? :green : :yellow) if order.payment_state? | ||
end | ||
} | ||
end | ||
|
||
def shipment_column | ||
{ | ||
header: :shipment, | ||
data: ->(order) do | ||
component('ui/badge').new(name: order.shipment_state.humanize, color: order.shipped? ? :green : :yellow) if order.shipment_state? | ||
end | ||
} | ||
end | ||
end |
16 changes: 16 additions & 0 deletions
16
admin/app/components/solidus_admin/users/orders/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,16 @@ | ||
en: | ||
title: "Users / %{email} / Order History" | ||
account: Account | ||
addresses: Addresses | ||
order_history: Order History | ||
items: Items | ||
store_credit: Store Credit | ||
last_active: Last Active | ||
last_login: | ||
login_time_ago: "%{last_login_time} ago" | ||
never: Never | ||
invitation_sent: Invitation sent | ||
create_order_for_user: Create order for this user | ||
no_orders_found: No Orders found. | ||
create_one: Create One | ||
back: Back |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,6 +49,7 @@ | |
member do | ||
get :addresses | ||
put :update_addresses | ||
get :orders | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -161,4 +161,49 @@ | |
expect(page).to have_field("user[ship_address_attributes][name]", with: "Elrond") | ||
end | ||
end | ||
|
||
context "when viewing a user's order history" do | ||
context "when a user has no orders" do | ||
before do | ||
create(:user, email: "[email protected]") | ||
visit "/admin/users" | ||
find_row("[email protected]").click | ||
click_on "Order History" | ||
end | ||
|
||
it "shows the order history page" do | ||
expect(page).to have_content("Users / [email protected] / Order History") | ||
expect(page).to have_content("Lifetime Stats") | ||
expect(page).to have_content("Order History") | ||
expect(page).to be_axe_clean | ||
end | ||
|
||
it "shows the appropriate content" do | ||
expect(page).to have_content("No Orders found.") | ||
end | ||
end | ||
|
||
context "when a user has ordered before" do | ||
before do | ||
create(:user, :with_orders, email: "[email protected]") | ||
visit "/admin/users" | ||
find_row("[email protected]").click | ||
click_on "Order History" | ||
end | ||
|
||
it "shows the order history page" do | ||
expect(page).to have_content("Users / [email protected] / Order History") | ||
expect(page).to have_content("Lifetime Stats") | ||
expect(page).to have_content("Order History") | ||
expect(page).to be_axe_clean | ||
end | ||
|
||
it "shows the order history" do | ||
expect(page).to have_content(/R\d+/) # Matches on any order number. | ||
expect(page).to have_content("Shipment") | ||
expect(page).to have_content("Payment") | ||
expect(page).not_to have_content("No Orders found.") | ||
end | ||
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