A platform for community discussion. Free, open, simple.
UX: Update period display when custom date range is selected in dashboard (#36858)
Régis Hanold8b4efdfbc40
diff --git a/config/locales/client.en.yml b/config/locales/client.en.ymlindex 4b748e060bb..74363a681b0 100644--- a/config/locales/client.en.yml+++ b/config/locales/client.en.yml@@ -4945,6 +4945,7 @@ en:this_month: "Month"this_week: "Week"today: "Today"+ custom: "Custom"custom_homepage:admin_message: 'One of your themes has enabled the "custom_homepage" modifier but it does not output anything in the [custom-homepage] connector. (This message is only shown to site administrators.)'diff --git a/frontend/discourse/admin/components/dashboard-period-selector.gjs b/frontend/discourse/admin/components/dashboard-period-selector.gjsindex 3312649e200..22df84bc3e2 100644--- a/frontend/discourse/admin/components/dashboard-period-selector.gjs+++ b/frontend/discourse/admin/components/dashboard-period-selector.gjs@@ -28,6 +28,8 @@ export default class DashboardPeriodSelector extends Component {@action={{@setPeriod}}@content={{this.availablePeriods}}@fullDay={{false}}+ @startDate={{@startDate}}+ @endDate={{@endDate}}/><DButton@icon="gear"diff --git a/frontend/discourse/admin/controllers/admin-dashboard-tab.js b/frontend/discourse/admin/controllers/admin-dashboard-tab.jsindex dd1ccf4d342..c81d0e85c02 100644--- a/frontend/discourse/admin/controllers/admin-dashboard-tab.js+++ b/frontend/discourse/admin/controllers/admin-dashboard-tab.js@@ -1,25 +1,38 @@-import { tracked } from "@glimmer/tracking";import Controller from "@ember/controller";import { action } from "@ember/object";import { service } from "@ember/service";-import { TrackedObject } from "@ember-compat/tracked-built-ins";import CustomDateRangeModal from "../components/modal/custom-date-range";export default class AdminDashboardTabController extends Controller {@service modal;- @tracked endDate = moment().locale("en").utc().endOf("day");- @tracked startDate = this.calculateStartDate();- @tracked- filters = new TrackedObject({- startDate: this.startDate,- endDate: this.endDate,- });-- queryParams = ["period"];+ queryParams = ["period", "start_date", "end_date"];period = "monthly";+ start_date = null;+ end_date = null;++ get startDate() {+ if (this.start_date) {+ return moment(this.start_date).locale("en").utc().startOf("day");+ }+ return this.#calculateStartDate();+ }- calculateStartDate() {+ get endDate() {+ if (this.end_date) {+ return moment(this.end_date).locale("en").utc().endOf("day");+ }+ return moment().locale("en").utc().endOf("day");+ }++ get filters() {+ return {+ startDate: this.startDate,+ endDate: this.endDate,+ };+ }++ #calculateStartDate() {const fullDay = moment().locale("en").utc().endOf("day");switch (this.period) {@@ -29,8 +42,6 @@ export default class AdminDashboardTabController extends Controller {return fullDay.subtract(3, "month").startOf("day");case "weekly":return fullDay.subtract(6, "days").startOf("day");- case "monthly":- return fullDay.subtract(1, "month").startOf("day");default:return fullDay.subtract(1, "month").startOf("day");}@@ -38,18 +49,16 @@ export default class AdminDashboardTabController extends Controller {@actionsetCustomDateRange(startDate, endDate) {- this.startDate = startDate;- this.endDate = endDate;- this.filters.startDate = this.startDate;- this.filters.endDate = this.endDate;+ this.setProperties({+ period: "custom",+ start_date: moment(startDate).format("YYYY-MM-DD"),+ end_date: moment(endDate).format("YYYY-MM-DD"),+ });}@actionsetPeriod(period) {- this.set("period", period);- this.startDate = this.calculateStartDate();- this.filters.startDate = this.startDate;- this.filters.endDate = this.endDate;+ this.setProperties({ period, start_date: null, end_date: null });}@actiondiff --git a/frontend/discourse/app/helpers/period-title.js b/frontend/discourse/app/helpers/period-title.jsindex 67d94dcb8b3..62c6fdb0c4d 100644--- a/frontend/discourse/app/helpers/period-title.js+++ b/frontend/discourse/app/helpers/period-title.js@@ -7,9 +7,13 @@ const TITLE_SUBS = {quarterly: "this_quarter",monthly: "this_month",daily: "today",+ custom: "custom",};-export default function periodTitle(period, { showDateRange, fullDay } = {}) {+export default function periodTitle(+ period,+ { showDateRange, fullDay, startDate, endDate } = {}+) {const title = i18n("filters.top." + (TITLE_SUBS[period] || "this_week"));if (!showDateRange) {@@ -26,6 +30,14 @@ export default function periodTitle(period, { showDateRange, fullDay } = {}) {}switch (period) {+ case "custom":+ if (startDate && endDate) {+ dateString =+ moment(startDate).format(i18n("dates.long_with_year_no_time")) ++ " – " ++ moment(endDate).format(i18n("dates.long_with_year_no_time"));+ }+ break;case "yearly":dateString =finishdiff --git a/frontend/discourse/select-kit/components/period-chooser.js b/frontend/discourse/select-kit/components/period-chooser.jsindex 0fafefb6307..a5787c24232 100644--- a/frontend/discourse/select-kit/components/period-chooser.js+++ b/frontend/discourse/select-kit/components/period-chooser.js@@ -16,6 +16,8 @@ import PeriodChooserRow from "./period-chooser/period-chooser-row";customStyle: true,headerComponent: PeriodChooserHeader,headerAriaLabel: i18n("period_chooser.aria_label"),+ startDate: "startDate",+ endDate: "endDate",})export default class PeriodChooser extends DropdownSelectBoxComponent {@oneWay("site.periods") content;diff --git a/frontend/discourse/select-kit/components/period-chooser/period-chooser-header.gjs b/frontend/discourse/select-kit/components/period-chooser/period-chooser-header.gjsindex 7c40a33b674..a34b1aa7e24 100644--- a/frontend/discourse/select-kit/components/period-chooser/period-chooser-header.gjs+++ b/frontend/discourse/select-kit/components/period-chooser/period-chooser-header.gjs@@ -17,6 +17,8 @@ export default class PeriodChooserHeader extends DropdownSelectBoxHeaderComponenthis.valueshowDateRange=truefullDay=this.selectKit.options.fullDay+ startDate=this.selectKit.options.startDate+ endDate=this.selectKit.options.endDate}}</h2>diff --git a/frontend/discourse/tests/acceptance/dashboard-test.js b/frontend/discourse/tests/acceptance/dashboard-test.jsindex ee0e56e9465..4e2472a7d40 100644--- a/frontend/discourse/tests/acceptance/dashboard-test.js+++ b/frontend/discourse/tests/acceptance/dashboard-test.js@@ -1,4 +1,4 @@-import { click, fillIn, visit } from "@ember/test-helpers";+import { click, currentURL, fillIn, visit } from "@ember/test-helpers";import { test } from "qunit";import { acceptance } from "discourse/tests/helpers/qunit-helpers";import selectKit from "discourse/tests/helpers/select-kit-helper";@@ -48,6 +48,51 @@ acceptance("Dashboard", function (needs) {.exists("new-contributors report");});+ test("custom date range updates period display", async function (assert) {+ await visit("/admin");++ const periodChooser = selectKit(".period-chooser");++ assert.strictEqual(periodChooser.header().value(), "monthly");++ await click(".custom-date-range-button");+ await click(".custom-date-range-modal .d-modal__footer .btn");++ assert.strictEqual(periodChooser.header().value(), "custom");+ assert.dom(".period-chooser-header .date-section").hasText("Custom");++ await periodChooser.expand();+ await periodChooser.selectRowByValue("yearly");++ assert.strictEqual(periodChooser.header().value(), "yearly");+ assert.dom(".period-chooser-header .date-section").hasText("Year");+ });++ test("custom date range is reflected in URL query params", async function (assert) {+ await visit("/admin");++ await click(".custom-date-range-button");+ await click(".custom-date-range-modal .d-modal__footer .btn");++ const url = currentURL();+ assert.true(url.includes("period=custom"), "period=custom is in URL");+ assert.true(url.includes("start_date="), "start_date is in URL");+ assert.true(url.includes("end_date="), "end_date is in URL");+ });++ test("URL query params restore custom date range", async function (assert) {+ await visit(+ "/admin?period=custom&start_date=2024-01-01&end_date=2024-01-31"+ );++ const periodChooser = selectKit(".period-chooser");+ assert.strictEqual(periodChooser.header().value(), "custom");+ assert.dom(".period-chooser-header .date-section").hasText("Custom");+ // The date range from query params should be displayed (dates may vary slightly due to UTC)+ assert.dom(".period-chooser-header .top-date-string").exists();+ assert.dom(".period-chooser-header .top-date-string").includesText("–");+ });+test("moderation tab", async function (assert) {await visit("/admin");await click(".dashboard .navigation-item.moderation .navigation-link");