DEV: Use ResultSet with staff action logs. (#7661)

DEV: Use ResultSet with staff action logs. (#7661)

diff --git a/app/assets/javascripts/admin/adapters/staff-action-log.js.es6 b/app/assets/javascripts/admin/adapters/staff-action-log.js.es6
new file mode 100644
index 0000000..8e0f087
--- /dev/null
+++ b/app/assets/javascripts/admin/adapters/staff-action-log.js.es6
@@ -0,0 +1,7 @@
+import RestAdapter from "discourse/adapters/rest";
+
+export default RestAdapter.extend({
+  basePath() {
+    return "/admin/logs/";
+  }
+});
diff --git a/app/assets/javascripts/admin/controllers/admin-logs-staff-action-logs.js.es6 b/app/assets/javascripts/admin/controllers/admin-logs-staff-action-logs.js.es6
index c1cd77b..a3d332c 100644
--- a/app/assets/javascripts/admin/controllers/admin-logs-staff-action-logs.js.es6
+++ b/app/assets/javascripts/admin/controllers/admin-logs-staff-action-logs.js.es6
@@ -1,21 +1,15 @@
 import { exportEntity } from "discourse/lib/export-csv";
 import { outputExportResult } from "discourse/lib/export-result";
-import StaffActionLog from "admin/models/staff-action-log";
 import {
   default as computed,
   on
 } from "ember-addons/ember-computed-decorators";
 
 export default Ember.Controller.extend({
-  loading: false,
-  filters: null,
-  userHistoryActions: [],
   model: null,
-  nextPage: 0,
-  lastPage: null,
-
+  filters: null,
   filtersExists: Ember.computed.gt("filterCount", 0),
-  showTable: Ember.computed.gt("model.length", 0),
+  userHistoryActions: null,
 
   @computed("filters.action_name")
   actionFilter(name) {
@@ -25,34 +19,21 @@ export default Ember.Controller.extend({
   @on("init")
   resetFilters() {
     this.setProperties({
-      filters: Ember.Object.create(),
-      model: [],
-      nextPage: 0,
-      lastPage: null
+      model: Ember.Object.create({ loadingMore: true }),
+      filters: Ember.Object.create()
     });
     this.scheduleRefresh();
   },
 
   _changeFilters(props) {
+    this.set("model", Ember.Object.create({ loadingMore: true }));
     this.filters.setProperties(props);
-    this.setProperties({
-      model: [],
-      nextPage: 0,
-      lastPage: null
-    });
     this.scheduleRefresh();
   },
 
   _refresh() {
-    if (this.lastPage && this.nextPage >= this.lastPage) {
-      return;
-    }
-
-    this.set("loading", true);
-
-    const page = this.nextPage;
     let filters = this.filters;
-    let params = { page };
+    let params = {};
     let count = 0;
 
     // Don't send null values
@@ -65,32 +46,23 @@ export default Ember.Controller.extend({
     });
     this.set("filterCount", count);
 
-    StaffActionLog.findAll(params)
-      .then(result => {
-        this.setProperties({
-          model: this.model.concat(result.staff_action_logs),
-          nextPage: page + 1
-        });
-
-        if (result.staff_action_logs.length === 0) {
-          this.set("lastPage", page);
-        }
-
-        if (this.userHistoryActions.length === 0) {
-          this.set(
-            "userHistoryActions",
-            result.user_history_actions
-              .map(action => ({
-                id: action.id,
-                action_id: action.action_id,
-                name: I18n.t("admin.logs.staff_actions.actions." + action.id),
-                name_raw: action.id
-              }))
-              .sort((a, b) => (a.name > b.name ? 1 : -1))
-          );
-        }
-      })
-      .finally(() => this.set("loading", false));
+    this.store.findAll("staff-action-log", params).then(result => {
+      this.set("model", result);
+
+      if (!this.userHistoryActions) {
+        this.set(
+          "userHistoryActions",
+          result.extras.user_history_actions
+            .map(action => ({
+              id: action.id,
+              action_id: action.action_id,
+              name: I18n.t("admin.logs.staff_actions.actions." + action.id),
+              name_raw: action.id
+            }))
+            .sort((a, b) => a.name.localeCompare(b.name))
+        );
+      }
+    });
   },
 
   scheduleRefresh() {
@@ -153,7 +125,7 @@ export default Ember.Controller.extend({
     },
 
     loadMore() {
-      this._refresh();
+      this.model.loadMore();
     }
   }
 });
diff --git a/app/assets/javascripts/admin/models/staff-action-log.js.es6 b/app/assets/javascripts/admin/models/staff-action-log.js.es6
index 3451cff..2d63019 100644
--- a/app/assets/javascripts/admin/models/staff-action-log.js.es6
+++ b/app/assets/javascripts/admin/models/staff-action-log.js.es6
@@ -2,6 +2,7 @@ import computed from "ember-addons/ember-computed-decorators";
 import { ajax } from "discourse/lib/ajax";
 import AdminUser from "admin/models/admin-user";
 import { escapeExpression } from "discourse/lib/utilities";
+import RestModel from "discourse/models/rest";
 
 function format(label, value, escape = true) {
   return value
@@ -9,7 +10,7 @@ function format(label, value, escape = true) {
     : "";
 }
 
-const StaffActionLog = Discourse.Model.extend({
+const StaffActionLog = RestModel.extend({
   showFullDetails: false,
 
   @computed("action_name")
@@ -80,16 +81,14 @@ const StaffActionLog = Discourse.Model.extend({
 });
 
 StaffActionLog.reopenClass({
-  create(attrs) {
-    attrs = attrs || {};
-
-    if (attrs.acting_user) {
-      attrs.acting_user = AdminUser.create(attrs.acting_user);
+  munge(json) {
+    if (json.acting_user) {
+      json.acting_user = AdminUser.create(json.acting_user);
     }
-    if (attrs.target_user) {
-      attrs.target_user = AdminUser.create(attrs.target_user);
+    if (json.target_user) {
+      json.target_user = AdminUser.create(json.target_user);
     }
-    return this._super(attrs);
+    return json;
   },
 
   findAll(data) {
diff --git a/app/assets/javascripts/admin/templates/logs/staff-action-logs.hbs b/app/assets/javascripts/admin/templates/logs/staff-action-logs.hbs
index d072bfe..bba9a72 100644
--- a/app/assets/javascripts/admin/templates/logs/staff-action-logs.hbs
+++ b/app/assets/javascripts/admin/templates/logs/staff-action-logs.hbs
@@ -39,70 +39,66 @@
 
 {{#staff-actions}}
 
-{{#load-more selector=".staff-logs tr" action=(action "loadMore")}}
-  {{#if showTable}}
-    <table class='table staff-logs grid'>
-
-      <thead>
-        <th>{{i18n 'admin.logs.staff_actions.staff_user'}}</th>
-        <th>{{i18n 'admin.logs.action'}}</th>
-        <th>{{i18n 'admin.logs.staff_actions.subject'}}</th>
-        <th>{{i18n 'admin.logs.staff_actions.when'}}</th>
-        <th>{{i18n 'admin.logs.staff_actions.details'}}</th>
-        <th>{{i18n 'admin.logs.staff_actions.context'}}</th>
-      </thead>
-
-      <tbody>
-        {{#each model as |item|}}
-          <tr class='admin-list-item'>
-            <td class="staff-users">
-              <div class="staff-user">
-                {{#if item.acting_user}}
-                  {{#link-to 'adminUser' item.acting_user}}{{avatar item.acting_user imageSize="tiny"}}{{/link-to}}
-                  <a {{action "filterByStaffUser" item.acting_user}}>{{item.acting_user.username}}</a>
-                {{else}}
-                  <span class="deleted-user" title="{{i18n 'admin.user.deleted'}}">
-                    {{d-icon "far-trash-alt"}}
-                  </span>
+  {{#load-more selector=".staff-logs tr" action=(action "loadMore")}}
+    {{#if model.content}}
+      <table class='table staff-logs grid'>
+        <thead>
+          <th>{{i18n 'admin.logs.staff_actions.staff_user'}}</th>
+          <th>{{i18n 'admin.logs.action'}}</th>
+          <th>{{i18n 'admin.logs.staff_actions.subject'}}</th>
+          <th>{{i18n 'admin.logs.staff_actions.when'}}</th>
+          <th>{{i18n 'admin.logs.staff_actions.details'}}</th>
+          <th>{{i18n 'admin.logs.staff_actions.context'}}</th>
+        </thead>
+        <tbody>
+          {{#each model.content as |item|}}
+            <tr class='admin-list-item'>
+              <td class="staff-users">
+                <div class="staff-user">
+                  {{#if item.acting_user}}
+                    {{#link-to 'adminUser' item.acting_user}}{{avatar item.acting_user imageSize="tiny"}}{{/link-to}}

[... diff too long, it was truncated ...]

GitHub sha: ba396a53

2 Likes