Skip to content

A platform for community discussion. Free, open, simple.

FIX: Preserve report sizes when applying Manage Reports (#44395)

Alan Guo Xiang Tande68c98af58f
diff --git a/app/controllers/admin/dashboard_controller.rb b/app/controllers/admin/dashboard_controller.rb
index 0cff87eabed..22323bd9a6a 100644
--- a/app/controllers/admin/dashboard_controller.rb
+++ b/app/controllers/admin/dashboard_controller.rb
@@ -249,6 +249,9 @@ class Admin::DashboardController < Admin::StaffController
raise Discourse::InvalidParameters.new(:items)
end
+ existing_reports =
+ AdminDashboardReport.all.index_by { |report| [report.source, report.identifier] }
+
params
.permit(items: %i[source identifier rows cols])
.fetch(:items, [])
@@ -257,12 +260,13 @@ class Admin::DashboardController < Admin::StaffController
identifier = entry[:identifier]
raise Discourse::InvalidParameters.new(:items) if source.blank? || identifier.blank?
- rows = Integer(entry[:rows].presence || 1, exception: false)
+ existing_report = existing_reports[[source.to_s, identifier.to_s]]
+ rows = Integer(entry[:rows].presence || existing_report&.rows || 1, exception: false)
if rows.nil? || rows < 1 || rows > AdminDashboardReport::MAX_ROWS
raise Discourse::InvalidParameters.new(:items)
end
- cols = Integer(entry[:cols].presence || 1, exception: false)
+ cols = Integer(entry[:cols].presence || existing_report&.cols || 1, exception: false)
if cols.nil? || cols < 1 || cols > AdminDashboardReport::MAX_COLS
raise Discourse::InvalidParameters.new(:items)
end
diff --git a/spec/fabricators/admin_dashboard_report_fabricator.rb b/spec/fabricators/admin_dashboard_report_fabricator.rb
new file mode 100644
index 00000000000..27b691d79d2
--- /dev/null
+++ b/spec/fabricators/admin_dashboard_report_fabricator.rb
@@ -0,0 +1,11 @@
+# frozen_string_literal: true
+
+Fabricator(:admin_dashboard_report) do
+ source "core_report"
+ identifier "signups"
+end
+
+Fabricator(:tall_admin_dashboard_report, from: :admin_dashboard_report) do
+ rows 3
+ cols 2
+end
diff --git a/spec/requests/admin/dashboard_controller_spec.rb b/spec/requests/admin/dashboard_controller_spec.rb
index f1db1f46ef0..563e8079d7d 100644
--- a/spec/requests/admin/dashboard_controller_spec.rb
+++ b/spec/requests/admin/dashboard_controller_spec.rb
@@ -2404,6 +2404,63 @@ RSpec.describe Admin::DashboardController do
expect(rows).to eq([["a", 3, 2], ["b", 1, 1]])
end
+ it "uses explicit dimensions to reset an existing report to its default size" do
+ DiscoursePluginRegistry.register_admin_dashboard_report_source(fake_provider, plugin)
+ Fabricate(:tall_admin_dashboard_report, source: "fake_source", identifier: "a")
+
+ put "/admin/dashboard/reports/layout.json",
+ params: {
+ items: [{ source: "fake_source", identifier: "a", rows: 1, cols: 1 }],
+ }
+
+ expect(response.status).to eq(204)
+ expect(AdminDashboardReport.pluck(:rows, :cols)).to eq([[1, 1]])
+ end
+
+ it "keeps the omitted dimension when resizing an existing report" do
+ DiscoursePluginRegistry.register_admin_dashboard_report_source(fake_provider, plugin)
+ Fabricate(:tall_admin_dashboard_report, source: "fake_source", identifier: "a")
+
+ put "/admin/dashboard/reports/layout.json",
+ params: {
+ items: [{ source: "fake_source", identifier: "a", rows: 2 }],
+ }
+
+ expect(response.status).to eq(204)
+ expect(AdminDashboardReport.pluck(:rows, :cols)).to eq([[2, 2]])
+ end
+
+ it "rejects an invalid size formed by an explicit column span and retained rows" do
+ DiscoursePluginRegistry.register_admin_dashboard_report_source(fake_provider, plugin)
+ Fabricate(:tall_admin_dashboard_report, source: "fake_source", identifier: "a")
+
+ put "/admin/dashboard/reports/layout.json",
+ params: {
+ items: [{ source: "fake_source", identifier: "a", cols: 1 }],
+ }
+
+ expect(response.status).to eq(400)
+ expect(AdminDashboardReport.pluck(:rows, :cols)).to eq([[3, 2]])
+ end
+
+ it "defaults a new report's size when another source has the same identifier" do
+ DiscoursePluginRegistry.register_admin_dashboard_report_source(fake_provider, plugin)
+ Fabricate(:tall_admin_dashboard_report)
+
+ put "/admin/dashboard/reports/layout.json",
+ params: {
+ items: [
+ { source: "core_report", identifier: "signups" },
+ { source: "fake_source", identifier: "signups" },
+ ],
+ }
+
+ expect(response.status).to eq(204)
+ expect(AdminDashboardReport.order(:position).pluck(:source, :rows, :cols)).to eq(
+ [["core_report", 3, 2], ["fake_source", 1, 1]],
+ )
+ end
+
it "persists a single-row card that spans the full width" do
DiscoursePluginRegistry.register_admin_dashboard_report_source(fake_provider, plugin)
diff --git a/spec/system/admin_dashboard_redesign/reports_section_spec.rb b/spec/system/admin_dashboard_redesign/reports_section_spec.rb
index d714bc8535c..1387cc9c647 100644
--- a/spec/system/admin_dashboard_redesign/reports_section_spec.rb
+++ b/spec/system/admin_dashboard_redesign/reports_section_spec.rb
@@ -100,6 +100,41 @@ describe "Admin Dashboard Redesign | Reports section" do
)
end
+ it "keeps existing report sizes when admins add another report" do
+ Fabricate(:tall_admin_dashboard_report, position: 0)
+ Fabricate(:admin_dashboard_report, identifier: "topics", position: 1, cols: 2)
+
+ page.visit("/admin")
+ dashboard.open_manage_reports_via_cog
+ expect(modal).to have_open
+ modal.toggle("core_report:admin_logins")
+ modal.apply
+ expect(modal).to have_closed
+
+ page.refresh
+ expect(dashboard).to have_card_size("core_report:signups", rows: 3, cols: 2)
+ expect(dashboard).to have_card_size("core_report:topics", rows: 1, cols: 2)
+ expect(dashboard).to have_card_size("core_report:admin_logins", rows: 1, cols: 1)
+ end
+
+ it "keeps report sizes when admins reorder reports on mobile", mobile: true do
+ Fabricate(:tall_admin_dashboard_report, position: 0)
+ Fabricate(:admin_dashboard_report, identifier: "topics", position: 1)
+
+ page.visit("/admin")
+ dashboard.open_manage_reports_via_cog
+ expect(modal).to have_open
+ modal.move_up("core_report:topics")
+ expect(modal).to have_enabled_rows_in_order(%w[core_report:topics core_report:signups])
+ modal.apply
+ expect(modal).to have_closed
+
+ page.refresh
+ expect(dashboard).to have_cards_in_order(%w[core_report:topics core_report:signups])
+ expect(dashboard).to have_card_size("core_report:signups", rows: 3, cols: 2)
+ expect(dashboard).to have_card_size("core_report:topics", rows: 1, cols: 1)
+ end
+
it "hides the Add Report tile when the cap is reached" do
identifiers =
Reports::ListQuery
diff --git a/spec/system/page_objects/components/manageable_row_list_modal.rb b/spec/system/page_objects/components/manageable_row_list_modal.rb
index c21b062cdb8..4cbb5ff1948 100644
--- a/spec/system/page_objects/components/manageable_row_list_modal.rb
+++ b/spec/system/page_objects/components/manageable_row_list_modal.rb
@@ -29,6 +29,15 @@ module PageObjects
all("#{@modal} #{ENABLED_ROW}").map { |el| el["data-identifier"] }
end
+ def has_enabled_rows_in_order?(identifiers)
+ has_css?("#{@modal} #{ENABLED_ROW}", count: identifiers.size) &&
+ identifiers.each_with_index.all? do |identifier, index|
+ has_css?(
+ "#{@modal} #{ENABLED_ROW}:nth-child(#{index + 1})[data-identifier='#{identifier}']",
+ )
+ end
+ end
+
def has_all_row?(identifier)
has_css?("#{@modal} #{ROW}[data-identifier='#{identifier}']")
end
@@ -67,6 +76,11 @@ module PageObjects
self
end
+ def move_up(identifier)
+ find("#{@modal} #{ROW}[data-identifier='#{identifier}'] .d-icon-arrow-up").click
+ self
+ end
+
def has_disabled_move_up?(identifier)
has_css?(
"#{@modal} #{ROW}[data-identifier='#{identifier}'] button.manageable-row-list__arrow[disabled] .d-icon-arrow-up",
diff --git a/spec/system/page_objects/pages/admin_dashboard_reports.rb b/spec/system/page_objects/pages/admin_dashboard_reports.rb
index 16091126cdf..e016dc80c22 100644
--- a/spec/system/page_objects/pages/admin_dashboard_reports.rb
+++ b/spec/system/page_objects/pages/admin_dashboard_reports.rb
@@ -13,10 +13,26 @@ module PageObjects
within(SECTION_SELECTOR) { all(".db-report__card").map { |el| el["data-identifier"] } }
end
+ def has_cards_in_order?(identifiers)
+ has_css?("#{SECTION_SELECTOR} .db-report__card", count: identifiers.size) &&
+ identifiers.each_with_index.all? do |identifier, index|
+ has_css?(
+ "#{SECTION_SELECTOR} .db-report__card:nth-child(#{index + 1})[data-identifier='#{identifier}']",
+ )
+ end
+ end
+
def has_card?(identifier)
has_css?("#{SECTION_SELECTOR} .db-report__card[data-identifier='#{identifier}']")
end
+ def has_card_size?(identifier, rows:, cols:)
+ selector = "#{SECTION_SELECTOR} .db-report__card[data-identifier='#{identifier}']"
+ selector += cols > 1 ? ".--wide" : ":not(.--wide)"
+ selector += rows > 1 ? ".--rows-#{rows}" : ":not([class*='--rows-'])"
+ has_css?(selector)
+ end
+
def has_default_report?
has_card?(default_report_identifier)
end