A platform for community discussion. Free, open, simple.
DEV: Replace period mixin with a component (#25292)
Jarek Radoszb2b32d6af88e
diff --git a/app/assets/javascripts/admin/addon/components/dashboard-period-selector.gjs b/app/assets/javascripts/admin/addon/components/dashboard-period-selector.gjsnew file mode 100644index 00000000000..d6519749b24--- /dev/null+++ b/app/assets/javascripts/admin/addon/components/dashboard-period-selector.gjs@@ -0,0 +1,40 @@+import Component from "@glimmer/component";+import { action } from "@ember/object";+import { inject as service } from "@ember/service";+import DButton from "discourse/components/d-button";+import PeriodChooser from "select-kit/components/period-chooser";+import CustomDateRangeModal from "../components/modal/custom-date-range";++export default class DashboardPeriodSelector extends Component {+ @service modal;++ availablePeriods = ["yearly", "quarterly", "monthly", "weekly"];++ @action+ openCustomDateRangeModal() {+ this.modal.show(CustomDateRangeModal, {+ model: {+ startDate: this.args.startDate,+ endDate: this.args.endDate,+ setCustomDateRange: this.args.setCustomDateRange,+ },+ });+ }++ <template>+ <div>+ <PeriodChooser+ @period={{@period}}+ @action={{@setPeriod}}+ @content={{this.availablePeriods}}+ @fullDay={{false}}+ />+ <DButton+ @icon="cog"+ @action={{this.openCustomDateRangeModal}}+ @title="admin.dashboard.custom_date_range"+ class="custom-date-range-button"+ />+ </div>+ </template>+}diff --git a/app/assets/javascripts/admin/addon/controllers/admin-dashboard-general.js b/app/assets/javascripts/admin/addon/controllers/admin-dashboard-general.jsindex dcc8deca526..80cacc2cd14 100644--- a/app/assets/javascripts/admin/addon/controllers/admin-dashboard-general.js+++ b/app/assets/javascripts/admin/addon/controllers/admin-dashboard-general.js@@ -1,15 +1,14 @@-import Controller, { inject as controller } from "@ember/controller";-import { action, computed } from "@ember/object";+import { inject as controller } from "@ember/controller";+import { computed } from "@ember/object";import { inject as service } from "@ember/service";import { setting } from "discourse/lib/computed";import getURL from "discourse-common/lib/get-url";import { makeArray } from "discourse-common/lib/helpers";import discourseComputed from "discourse-common/utils/decorators";import I18n from "discourse-i18n";-import PeriodComputationMixin from "admin/mixins/period-computation";import AdminDashboard from "admin/models/admin-dashboard";import Report from "admin/models/report";-import CustomDateRangeModal from "../components/modal/custom-date-range";+import AdminDashboardTabController from "./admin-dashboard-tab";function staticReport(reportType) {return computed("reports.[]", function () {@@ -17,10 +16,7 @@ function staticReport(reportType) {});}-export default class AdminDashboardGeneralController extends Controller.extend(- PeriodComputationMixin-) {- @service modal;+export default class AdminDashboardGeneralController extends AdminDashboardTabController {@service router;@service siteSettings;@controller("exception") exceptionController;@@ -74,10 +70,26 @@ export default class AdminDashboardGeneralController extends Controller.extend(].some((x) => !this.hiddenReports.includes(x));}+ @discourseComputed+ today() {+ return moment().locale("en").utc().endOf("day");+ }++ @computed("startDate", "endDate")+ get filters() {+ return { startDate: this.startDate, endDate: this.endDate };+ }+@discourseComputedactivityMetricsFilters() {+ const lastMonth = moment()+ .locale("en")+ .utc()+ .startOf("day")+ .subtract(1, "month");+return {- startDate: this.lastMonth,+ startDate: lastMonth,endDate: this.today,};}@@ -147,29 +159,4 @@ export default class AdminDashboardGeneralController extends Controller.extend(.finally(() => this.set("isLoading", false));}}-- @discourseComputed("startDate", "endDate")- filters(startDate, endDate) {- return { startDate, endDate };- }-- _reportsForPeriodURL(period) {- return getURL(`/admin?period=${period}`);- }-- @action- setCustomDateRange(startDate, endDate) {- this.setProperties({ startDate, endDate });- }-- @action- openCustomDateRangeModal() {- this.modal.show(CustomDateRangeModal, {- model: {- startDate: this.startDate,- endDate: this.endDate,- setCustomDateRange: this.setCustomDateRange,- },- });- }}diff --git a/app/assets/javascripts/admin/addon/controllers/admin-dashboard-moderation.js b/app/assets/javascripts/admin/addon/controllers/admin-dashboard-moderation.jsindex b4e79e41b07..a5fe8a27446 100644--- a/app/assets/javascripts/admin/addon/controllers/admin-dashboard-moderation.js+++ b/app/assets/javascripts/admin/addon/controllers/admin-dashboard-moderation.js@@ -1,16 +1,8 @@-import Controller from "@ember/controller";-import { action, computed } from "@ember/object";-import { inject as service } from "@ember/service";-import getURL from "discourse-common/lib/get-url";+import { computed } from "@ember/object";import discourseComputed from "discourse-common/utils/decorators";-import PeriodComputationMixin from "admin/mixins/period-computation";-import CustomDateRangeModal from "../components/modal/custom-date-range";--export default class AdminDashboardModerationController extends Controller.extend(- PeriodComputationMixin-) {- @service modal;+import AdminDashboardTabController from "./admin-dashboard-tab";+export default class AdminDashboardModerationController extends AdminDashboardTabController {@discourseComputedflagsStatusOptions() {return {@@ -39,33 +31,19 @@ export default class AdminDashboardModerationController extends Controller.exten};}- @discourseComputed("startDate", "endDate")- filters(startDate, endDate) {- return { startDate, endDate };- }-- @discourseComputed("lastWeek", "endDate")- lastWeekfilters(startDate, endDate) {- return { startDate, endDate };+ @computed("startDate", "endDate")+ get filters() {+ return { startDate: this.startDate, endDate: this.endDate };}- _reportsForPeriodURL(period) {- return getURL(`/admin/dashboard/moderation?period=${period}`);- }+ @discourseComputed("endDate")+ lastWeekFilters(endDate) {+ const lastWeek = moment()+ .locale("en")+ .utc()+ .endOf("day")+ .subtract(1, "week");- @action- setCustomDateRange(startDate, endDate) {- this.setProperties({ startDate, endDate });- }-- @action- openCustomDateRangeModal() {- this.modal.show(CustomDateRangeModal, {- model: {- startDate: this.startDate,- endDate: this.endDate,- setCustomDateRange: this.setCustomDateRange,- },- });+ return { lastWeek, endDate };}}diff --git a/app/assets/javascripts/admin/addon/controllers/admin-dashboard-tab.js b/app/assets/javascripts/admin/addon/controllers/admin-dashboard-tab.jsnew file mode 100644index 00000000000..1432adbd270--- /dev/null+++ b/app/assets/javascripts/admin/addon/controllers/admin-dashboard-tab.js@@ -0,0 +1,57 @@+import Controller from "@ember/controller";+import { action, computed } from "@ember/object";+import { inject as service } from "@ember/service";+import CustomDateRangeModal from "../components/modal/custom-date-range";++export default class AdminDashboardTabController extends Controller {+ @service modal;++ queryParams = ["period"];+ period = "monthly";++ endDate = moment().locale("en").utc().endOf("day");+ _startDate;++ @computed("_startDate", "period")+ get startDate() {+ if (this._startDate) {+ return this._startDate;+ }++ const fullDay = moment().locale("en").utc().endOf("day");++ switch (this.period) {+ case "yearly":+ return fullDay.subtract(1, "year").startOf("day");+ case "quarterly":+ 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");+ }+ }++ @action+ setCustomDateRange(_startDate, endDate) {+ this.setProperties({ _startDate, endDate });+ }++ @action+ setPeriod(period) {+ this.setProperties({ period, _startDate: null });+ }++ @action+ openCustomDateRangeModal() {+ this.modal.show(CustomDateRangeModal, {+ model: {+ startDate: this.startDate,+ endDate: this.endDate,+ setCustomDateRange: this.setCustomDateRange,+ },+ });+ }+}diff --git a/app/assets/javascripts/admin/addon/mixins/period-computation.js b/app/assets/javascripts/admin/addon/mixins/period-computation.jsindex 1968df7c2c3..5a97d298272 100644--- a/app/assets/javascripts/admin/addon/mixins/period-computation.js+++ b/app/assets/javascripts/admin/addon/mixins/period-computation.js@@ -1,5 +1,6 @@import Mixin from "@ember/object/mixin";import DiscourseURL from "discourse/lib/url";+import deprecated from "discourse-common/lib/deprecated";import discourseComputed from "discourse-common/utils/decorators";export default Mixin.create({@@ -9,6 +10,13 @@ export default Mixin.create({init() {this._super(...arguments);+ deprecated(+ "PeriodComputation mixin is deprecated. Use AdminDashboardTabController instead.",+ {+ id: "discourse.period-mixin",+ since: "3.2.0.beta5-dev",+ }+ );this.availablePeriods = ["yearly", "quarterly", "monthly", "weekly"];},diff --git a/app/assets/javascripts/admin/addon/templates/dashboard_general.hbs b/app/assets/javascripts/admin/addon/templates/dashboard_general.hbsindex 6215319581d..702bed7c1c0 100644--- a/app/assets/javascripts/admin/addon/templates/dashboard_general.hbs+++ b/app/assets/javascripts/admin/addon/templates/dashboard_general.hbs@@ -1,7 +1,5 @@<ConditionalLoadingSpinner @condition={{this.isLoading}}>- <span>- <PluginOutlet @name="admin-dashboard-general-top" @connectorTagName="div" />- </span>+ <PluginOutlet @name="admin-dashboard-general-top" @connectorTagName="div" />{{#if this.isCommunityHealthVisible}}<div class="community-health section">@@ -12,20 +10,14 @@{{i18n "admin.dashboard.community_health"}}</a></h2>- <span>- <PeriodChooser- @period={{this.period}}- @action={{action "changePeriod"}}- @content={{this.availablePeriods}}- @fullDay={{false}}- />- <DButton- @icon="cog"- class="custom-date-range-button"- @action={{this.openCustomDateRangeModal}}- @title="admin.dashboard.custom_date_range"- />- </span>++ <DashboardPeriodSelector+ @period={{this.period}}+ @setPeriod={{this.setPeriod}}+ @startDate={{this.startDate}}+ @endDate={{this.endDate}}+ @setCustomDateRange={{this.setCustomDateRange}}+ /></div><div class="section-body">@@ -203,11 +195,9 @@{{/if}}</div>- <span>- <PluginOutlet- @name="admin-dashboard-general-bottom"- @connectorTagName="div"- @outletArgs={{hash filters=this.filters}}- />- </span>+ <PluginOutlet+ @name="admin-dashboard-general-bottom"+ @connectorTagName="div"+ @outletArgs={{hash filters=this.filters}}+ /></ConditionalLoadingSpinner>\ No newline at end of filediff --git a/app/assets/javascripts/admin/addon/templates/dashboard_moderation.hbs b/app/assets/javascripts/admin/addon/templates/dashboard_moderation.hbsindex 3a1ef805dd3..bb3d1f2c9f3 100644--- a/app/assets/javascripts/admin/addon/templates/dashboard_moderation.hbs+++ b/app/assets/javascripts/admin/addon/templates/dashboard_moderation.hbs@@ -1,10 +1,8 @@<div class="sections">- <span>- <PluginOutlet- @name="admin-dashboard-moderation-top"- @connectorTagName="div"- />- </span>+ <PluginOutlet+ @name="admin-dashboard-moderation-top"+ @connectorTagName="div"+ />{{#if this.isModeratorsActivityVisible}}<div class="moderators-activity section">@@ -14,20 +12,14 @@{{i18n "admin.dashboard.moderators_activity"}}</a></h2>- <span>- <PeriodChooser- @period={{this.period}}- @action={{action "changePeriod"}}- @content={{this.availablePeriods}}- @fullDay={{false}}- />- <DButton- @icon="cog"- class="custom-date-range-button"- @action={{this.openCustomDateRangeModal}}- @title="admin.dashboard.custom_date_range"- />- </span>++ <DashboardPeriodSelector+ @period={{this.period}}+ @setPeriod={{this.setPeriod}}+ @startDate={{this.startDate}}+ @endDate={{this.endDate}}+ @setCustomDateRange={{this.setCustomDateRange}}+ /></div><div class="section-body">@@ -44,26 +36,24 @@<AdminReport@dataSourceName="flags_status"@reportOptions={{this.flagsStatusOptions}}- @filters={{this.lastWeekfilters}}+ @filters={{this.lastWeekFilters}}/><AdminReport@dataSourceName="post_edits"- @filters={{this.lastWeekfilters}}+ @filters={{this.lastWeekFilters}}/><AdminReport@dataSourceName="user_flagging_ratio"- @filters={{this.lastWeekfilters}}+ @filters={{this.lastWeekFilters}}@reportOptions={{this.userFlaggingRatioOptions}}/>- <span>- <PluginOutlet- @name="admin-dashboard-moderation-bottom"- @connectorTagName="div"- @outletArgs={{hash filters=this.lastWeekfilters}}- />- </span>+ <PluginOutlet+ @name="admin-dashboard-moderation-bottom"+ @connectorTagName="div"+ @outletArgs={{hash filters=this.lastWeekFilters}}+ /></div></div>\ No newline at end of filediff --git a/app/assets/javascripts/admin/addon/templates/dashboard_security.hbs b/app/assets/javascripts/admin/addon/templates/dashboard_security.hbsindex 887cdb3c0fb..d91ce4dcbfc 100644--- a/app/assets/javascripts/admin/addon/templates/dashboard_security.hbs+++ b/app/assets/javascripts/admin/addon/templates/dashboard_security.hbs@@ -1,27 +1,20 @@<div class="sections">- <span>- <PluginOutlet- @name="admin-dashboard-security-top"- @connectorTagName="div"- />- </span>+ <PluginOutlet @name="admin-dashboard-security-top" @connectorTagName="div" /><div class="main-section"><AdminReport@dataSourceName="suspicious_logins"- @filters={{this.lastWeekfilters}}+ @filters={{this.lastWeekFilters}}/><AdminReport@dataSourceName="staff_logins"- @filters={{this.lastWeekfilters}}+ @filters={{this.lastWeekFilters}}/>- <span>- <PluginOutlet- @name="admin-dashboard-security-bottom"- @connectorTagName="div"- />- </span>+ <PluginOutlet+ @name="admin-dashboard-security-bottom"+ @connectorTagName="div"+ /></div></div>\ No newline at end of file