FIX: prevents double network calls and other quirks on admin emails (#7074)

FIX: prevents double network calls and other quirks on admin emails (#7074)

diff --git a/app/assets/javascripts/admin/controllers/admin-email-bounced.js.es6 b/app/assets/javascripts/admin/controllers/admin-email-bounced.js.es6
index c2ec582..535fa4b 100644
--- a/app/assets/javascripts/admin/controllers/admin-email-bounced.js.es6
+++ b/app/assets/javascripts/admin/controllers/admin-email-bounced.js.es6
@@ -1,9 +1,8 @@
 import AdminEmailLogsController from "admin/controllers/admin-email-logs";
 import debounce from "discourse/lib/debounce";
-import EmailLog from "admin/models/email-log";
 
 export default AdminEmailLogsController.extend({
   filterEmailLogs: debounce(function() {
-    EmailLog.findAll(this.get("filter")).then(logs => this.set("model", logs));
-  }, 250).observes("filter.{user,address,type}")
+    this.loadLogs();
+  }, 250).observes("filter.{status,user,address,type}")
 });
diff --git a/app/assets/javascripts/admin/controllers/admin-email-incomings.js.es6 b/app/assets/javascripts/admin/controllers/admin-email-incomings.js.es6
deleted file mode 100644
index 9dc5958..0000000
--- a/app/assets/javascripts/admin/controllers/admin-email-incomings.js.es6
+++ /dev/null
@@ -1,25 +0,0 @@
-import IncomingEmail from "admin/models/incoming-email";
-
-export default Ember.Controller.extend({
-  loading: false,
-
-  actions: {
-    loadMore() {
-      if (this.get("loading") || this.get("model.allLoaded")) {
-        return;
-      }
-      this.set("loading", true);
-
-      IncomingEmail.findAll(this.get("filter"), this.get("model.length"))
-        .then(incoming => {
-          if (incoming.length < 50) {
-            this.get("model").set("allLoaded", true);
-          }
-          this.get("model").addObjects(incoming);
-        })
-        .finally(() => {
-          this.set("loading", false);
-        });
-    }
-  }
-});
diff --git a/app/assets/javascripts/admin/controllers/admin-email-logs.js.es6 b/app/assets/javascripts/admin/controllers/admin-email-logs.js.es6
index a86ae42..b08a511 100644
--- a/app/assets/javascripts/admin/controllers/admin-email-logs.js.es6
+++ b/app/assets/javascripts/admin/controllers/admin-email-logs.js.es6
@@ -3,23 +3,34 @@ import EmailLog from "admin/models/email-log";
 export default Ember.Controller.extend({
   loading: false,
 
-  actions: {
-    loadMore() {
-      if (this.get("loading") || this.get("model.allLoaded")) {
-        return;
-      }
+  loadLogs(sourceModel, loadMore) {
+    if ((loadMore && this.get("loading")) || this.get("model.allLoaded")) {
+      return;
+    }
+
+    this.set("loading", true);
+
+    sourceModel = sourceModel || EmailLog;
 
-      this.set("loading", true);
-      return EmailLog.findAll(this.get("filter"), this.get("model.length"))
-        .then(logs => {
-          if (logs.length < 50) {
-            this.get("model").set("allLoaded", true);
-          }
+    return sourceModel
+      .findAll(this.get("filter"), loadMore ? this.get("model.length") : null)
+      .then(logs => {
+        if (this.get("model") && loadMore && logs.length < 50) {
+          this.get("model").set("allLoaded", true);
+        }
+
+        if (this.get("model") && loadMore) {
           this.get("model").addObjects(logs);
-        })
-        .finally(() => {
-          this.set("loading", false);
-        });
+        } else {
+          this.set("model", logs);
+        }
+      })
+      .finally(() => this.set("loading", false));
+  },
+
+  actions: {
+    loadMore() {
+      this.loadLogs(EmailLog, true);
     }
   }
 });
diff --git a/app/assets/javascripts/admin/controllers/admin-email-received.js.es6 b/app/assets/javascripts/admin/controllers/admin-email-received.js.es6
index a5f240f..7659e61 100644
--- a/app/assets/javascripts/admin/controllers/admin-email-received.js.es6
+++ b/app/assets/javascripts/admin/controllers/admin-email-received.js.es6
@@ -1,11 +1,15 @@
-import AdminEmailIncomingsController from "admin/controllers/admin-email-incomings";
+import AdminEmailLogsController from "admin/controllers/admin-email-logs";
 import debounce from "discourse/lib/debounce";
 import IncomingEmail from "admin/models/incoming-email";
 
-export default AdminEmailIncomingsController.extend({
+export default AdminEmailLogsController.extend({
   filterIncomingEmails: debounce(function() {
-    IncomingEmail.findAll(this.get("filter")).then(incomings =>
-      this.set("model", incomings)
-    );
-  }, 250).observes("filter.{from,to,subject}")
+    this.loadLogs(IncomingEmail);
+  }, 250).observes("filter.{status,from,to,subject}"),
+
+  actions: {
+    loadMore() {
+      this.loadLogs(IncomingEmail, true);
+    }
+  }
 });
diff --git a/app/assets/javascripts/admin/controllers/admin-email-rejected.js.es6 b/app/assets/javascripts/admin/controllers/admin-email-rejected.js.es6
index b9341dd..602bb05 100644
--- a/app/assets/javascripts/admin/controllers/admin-email-rejected.js.es6
+++ b/app/assets/javascripts/admin/controllers/admin-email-rejected.js.es6
@@ -1,11 +1,15 @@
-import AdminEmailIncomingsController from "admin/controllers/admin-email-incomings";
+import AdminEmailLogsController from "admin/controllers/admin-email-logs";
 import debounce from "discourse/lib/debounce";
 import IncomingEmail from "admin/models/incoming-email";
 
-export default AdminEmailIncomingsController.extend({
+export default AdminEmailLogsController.extend({
   filterIncomingEmails: debounce(function() {
-    IncomingEmail.findAll(this.get("filter")).then(incomings =>
-      this.set("model", incomings)
-    );
-  }, 250).observes("filter.{from,to,subject,error}")
+    this.loadLogs(IncomingEmail);
+  }, 250).observes("filter.{status,from,to,subject,error}"),
+
+  actions: {
+    loadMore() {
+      this.loadLogs(IncomingEmail, true);
+    }
+  }
 });
diff --git a/app/assets/javascripts/admin/controllers/admin-email-sent.js.es6 b/app/assets/javascripts/admin/controllers/admin-email-sent.js.es6
index 691b017..83f52d3 100644
--- a/app/assets/javascripts/admin/controllers/admin-email-sent.js.es6
+++ b/app/assets/javascripts/admin/controllers/admin-email-sent.js.es6
@@ -1,9 +1,8 @@
 import AdminEmailLogsController from "admin/controllers/admin-email-logs";
 import debounce from "discourse/lib/debounce";
-import EmailLog from "admin/models/email-log";
 
 export default AdminEmailLogsController.extend({
   filterEmailLogs: debounce(function() {
-    EmailLog.findAll(this.get("filter")).then(logs => this.set("model", logs));
-  }, 250).observes("filter.{user,address,type,reply_key}")
+    this.loadLogs();
+  }, 250).observes("filter.{status,user,address,type,reply_key}")
 });
diff --git a/app/assets/javascripts/admin/controllers/admin-email-skipped.js.es6 b/app/assets/javascripts/admin/controllers/admin-email-skipped.js.es6
index c2ec582..535fa4b 100644
--- a/app/assets/javascripts/admin/controllers/admin-email-skipped.js.es6
+++ b/app/assets/javascripts/admin/controllers/admin-email-skipped.js.es6
@@ -1,9 +1,8 @@
 import AdminEmailLogsController from "admin/controllers/admin-email-logs";
 import debounce from "discourse/lib/debounce";
-import EmailLog from "admin/models/email-log";
 
 export default AdminEmailLogsController.extend({
   filterEmailLogs: debounce(function() {
-    EmailLog.findAll(this.get("filter")).then(logs => this.set("model", logs));
-  }, 250).observes("filter.{user,address,type}")
+    this.loadLogs();
+  }, 250).observes("filter.{status,user,address,type}")
 });
diff --git a/app/assets/javascripts/admin/routes/admin-email-logs.js.es6 b/app/assets/javascripts/admin/routes/admin-email-logs.js.es6
index b61b630..35d2c51 100644
--- a/app/assets/javascripts/admin/routes/admin-email-logs.js.es6
+++ b/app/assets/javascripts/admin/routes/admin-email-logs.js.es6
@@ -1,12 +1,8 @@
-import EmailLog from "admin/models/email-log";
-
 export default Discourse.Route.extend({
-  model() {
-    return EmailLog.findAll({ status: this.get("status") });
-  },
-
-  setupController(controller, model) {
-    controller.set("model", model);
-    controller.set("filter", { status: this.get("status") });
+  setupController(controller) {
+    controller.setProperties({

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

GitHub sha: fbedaea5

1 Like

This commit has been mentioned on Discourse Meta. There might be relevant details there: