Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Admin: improve admin dashboard page (#12502)

Damian Legawiecc7651cc26b5c
diff --git a/admin/app/controllers/spree/admin/dashboard_controller.rb b/admin/app/controllers/spree/admin/dashboard_controller.rb
index 7d16ca5cea..3c847a2279 100644
--- a/admin/app/controllers/spree/admin/dashboard_controller.rb
+++ b/admin/app/controllers/spree/admin/dashboard_controller.rb
@@ -7,6 +7,8 @@ module Spree
before_action :load_vendor
before_action :clear_return_to, only: %i[show]
+ layout :choose_layout
+
def show; end
def getting_started; end
@@ -79,7 +81,7 @@ module Spree
return if product_ids.empty?
- products = current_store.products.with_deleted.where(id: product_ids)
+ products = current_store.products.with_deleted.includes(:master, :variants).where(id: product_ids)
@top_products = line_items_grouped.map do |li|
product = products.find { |p| p.id == li.variant_product_id }
@@ -134,6 +136,14 @@ module Spree
device.nil? ? 'N/A' : device
end.map { |series| [series.first, (series.second.to_f / @visits_scope.count) * 100] }
end
+
+ def choose_layout
+ if action_name == 'analytics'
+ 'turbo_rails/frame'
+ else
+ 'spree/admin'
+ end
+ end
end
end
end
diff --git a/admin/app/views/spree/admin/dashboard/_top_products.html.erb b/admin/app/views/spree/admin/dashboard/_top_products.html.erb
index fd32aee6b7..f466217eba 100644
--- a/admin/app/views/spree/admin/dashboard/_top_products.html.erb
+++ b/admin/app/views/spree/admin/dashboard/_top_products.html.erb
@@ -1,8 +1,11 @@
+<% report_link = spree.new_admin_report_path(type: 'products_performance', date_from: params[:date_from], date_to: params[:date_to], vendor_id: @vendor.try(:id)) %>
+
<div class="card mt-3 mb-4">
- <div class="card-header pb-3">
+ <div class="card-header d-flex justify-content-between align-items-center">
<h5 class="card-title">
- Top products
+ <%= Spree.t('admin.dashboard.top_products') %>
</h5>
+ <%= link_to Spree.t('admin.dashboard.view_report'), report_link, class: 'btn btn-light btn-sm', data: { turbo_frame: '_top' } %>
</div>
<div class="card-body m-0 p-0">
<div class="table-responsive">
@@ -62,7 +65,7 @@
</table>
</div>
<p class="text-center mt-0 border-top py-2 mb-0">
- <%= link_to 'Show full report', spree.new_admin_report_path(type: 'products_performance', date_from: params[:date_from], date_to: params[:date_to], vendor_id: @vendor.try(:id)), class: 'm-2 w-50 btn btn-light mt-2', data: { turbo_frame: '_top' } %>
+ <%= link_to Spree.t('admin.dashboard.view_report'), report_link, class: 'm-2 w-50 btn btn-light mt-2', data: { turbo_frame: '_top' } %>
</p>
</div>
</div>
diff --git a/admin/config/locales/en.yml b/admin/config/locales/en.yml
index 2077ef7fb7..a04dce2272 100644
--- a/admin/config/locales/en.yml
+++ b/admin/config/locales/en.yml
@@ -47,6 +47,8 @@ en:
description: Everything you need to set up to start selling.
hi: Hi
whats_happening_on: Here's what's happening on <strong>%{store_name}</strong> today.
+ top_products: Top products
+ view_report: View report
display_on_options:
back_end: Only on admin panel
both: On both admin panel & storefront
diff --git a/admin/spec/controllers/spree/admin/dashboard_controller_spec.rb b/admin/spec/controllers/spree/admin/dashboard_controller_spec.rb
index fa13d5df3b..107e128d81 100644
--- a/admin/spec/controllers/spree/admin/dashboard_controller_spec.rb
+++ b/admin/spec/controllers/spree/admin/dashboard_controller_spec.rb
@@ -19,6 +19,24 @@ describe Spree::Admin::DashboardController, type: :controller do
get :analytics
expect(response).to render_template(:analytics)
end
+
+ context 'with data' do
+ before do
+ create_list(:completed_order_with_totals, 3, store: store)
+ end
+
+ it 'renders analytics' do
+ get :analytics
+ expect(response).to render_template(:analytics)
+ end
+
+ it 'renders top products' do
+ get :analytics
+ expect(assigns(:top_products)).to be_present
+ expect(response.body).to include('Top products')
+ expect(response.body).to include('View report')
+ end
+ end
end
end