DEV: Simplify watched word code (#13103)

DEV: Simplify watched word code (#13103)

  • DEV: Use site setting instead

  • DEV: Use .length instead of a different property

  • DEV: Simplify watched word code

diff --git a/app/assets/javascripts/admin/addon/components/watched-word-form.js b/app/assets/javascripts/admin/addon/components/watched-word-form.js
index cf95714..7730b2a 100644
--- a/app/assets/javascripts/admin/addon/components/watched-word-form.js
+++ b/app/assets/javascripts/admin/addon/components/watched-word-form.js
@@ -19,12 +19,13 @@ export default Component.extend({
   canReplace: equal("actionKey", "replace"),
   canTag: equal("actionKey", "tag"),
 
-  @discourseComputed("regularExpressions")
-  placeholderKey(regularExpressions) {
-    return (
-      "admin.watched_words.form.placeholder" +
-      (regularExpressions ? "_regexp" : "")
-    );
+  @discourseComputed("siteSettings.watched_words_regular_expressions")
+  placeholderKey(watchedWordsRegularExpressions) {
+    if (watchedWordsRegularExpressions) {
+      return "admin.watched_words.form.placeholder_regexp";
+    } else {
+      return "admin.watched_words.form.placeholder";
+    }
   },
 
   @observes("word")
diff --git a/app/assets/javascripts/admin/addon/controllers/admin-watched-words-action.js b/app/assets/javascripts/admin/addon/controllers/admin-watched-words-action.js
index 1265e3d..323eaad 100644
--- a/app/assets/javascripts/admin/addon/controllers/admin-watched-words-action.js
+++ b/app/assets/javascripts/admin/addon/controllers/admin-watched-words-action.js
@@ -12,20 +12,14 @@ import showModal from "discourse/lib/show-modal";
 export default Controller.extend({
   adminWatchedWords: controller(),
   actionNameKey: null,
-  showWordsList: or(
-    "adminWatchedWords.filtered",
-    "adminWatchedWords.showWords"
-  ),
   downloadLink: fmt(
     "actionNameKey",
     "/admin/customize/watched_words/action/%@/download"
   ),
+  showWordsList: or("adminWatchedWords.showWords", "adminWatchedWords.filter"),
 
   findAction(actionName) {
-    return (this.get("adminWatchedWords.model") || []).findBy(
-      "nameKey",
-      actionName
-    );
+    return (this.adminWatchedWords.model || []).findBy("nameKey", actionName);
   },
 
   @discourseComputed("actionNameKey", "adminWatchedWords.model")
@@ -33,57 +27,56 @@ export default Controller.extend({
     return this.findAction(actionName);
   },
 
-  @discourseComputed("currentAction.words.[]", "adminWatchedWords.model")
-  filteredContent(words) {
-    return words || [];
-  },
-
   @discourseComputed("actionNameKey")
   actionDescription(actionNameKey) {
     return I18n.t("admin.watched_words.action_descriptions." + actionNameKey);
   },
 
-  @discourseComputed("currentAction.count")
-  wordCount(count) {
-    return count || 0;
-  },
-
   actions: {
     recordAdded(arg) {
-      const a = this.findAction(this.actionNameKey);
-      if (a) {
-        a.words.unshiftObject(arg);
-        a.incrementProperty("count");
-        schedule("afterRender", () => {
-          // remove from other actions lists
-          let match = null;
-          this.get("adminWatchedWords.model").forEach((action) => {
-            if (match) {
-              return;
-            }
+      const action = this.findAction(this.actionNameKey);
+      if (!action) {
+        return;
+      }
 
-            if (action.nameKey !== this.actionNameKey) {
-              match = action.words.findBy("id", arg.id);
-              if (match) {
-                action.words.removeObject(match);
-                action.decrementProperty("count");
-              }
+      action.words.unshiftObject(arg);
+      schedule("afterRender", () => {
+        // remove from other actions lists
+        let match = null;
+        this.adminWatchedWords.model.forEach((otherAction) => {
+          if (match) {
+            return;
+          }
+
+          if (otherAction.nameKey !== this.actionNameKey) {
+            match = otherAction.words.findBy("id", arg.id);
+            if (match) {
+              otherAction.words.removeObject(match);
             }
-          });
+          }
         });
-      }
+      });
     },
 
     recordRemoved(arg) {
       if (this.currentAction) {
         this.currentAction.words.removeObject(arg);
-        this.currentAction.decrementProperty("count");
       }
     },
 
     uploadComplete() {
       WatchedWord.findAll().then((data) => {
-        this.set("adminWatchedWords.model", data);
+        this.adminWatchedWords.set("model", data);
+      });
+    },
+
+    test() {
+      WatchedWord.findAll().then((data) => {
+        this.adminWatchedWords.set("model", data);
+        showModal("admin-watched-word-test", {
+          admin: true,
+          model: this.currentAction,
+        });
       });
     },
 
@@ -102,25 +95,12 @@ export default Controller.extend({
             }).then(() => {
               const action = this.findAction(actionKey);
               if (action) {
-                action.setProperties({
-                  words: [],
-                  count: 0,
-                });
+                action.set("words", []);
               }
             });
           }
         }
       );
     },
-
-    test() {
-      WatchedWord.findAll().then((data) => {
-        this.set("adminWatchedWords.model", data);
-        showModal("admin-watched-word-test", {
-          admin: true,
-          model: this.currentAction,
-        });
-      });
-    },
   },
 });
diff --git a/app/assets/javascripts/admin/addon/controllers/admin-watched-words.js b/app/assets/javascripts/admin/addon/controllers/admin-watched-words.js
index cfd86c6..ce41e74 100644
--- a/app/assets/javascripts/admin/addon/controllers/admin-watched-words.js
+++ b/app/assets/javascripts/admin/addon/controllers/admin-watched-words.js
@@ -1,71 +1,56 @@
 import Controller from "@ember/controller";
-import EmberObject from "@ember/object";
+import EmberObject, { action } from "@ember/object";
 import { INPUT_DELAY } from "discourse-common/config/environment";
-import { alias } from "@ember/object/computed";
 import discourseDebounce from "discourse-common/lib/debounce";
 import { isEmpty } from "@ember/utils";
 import { observes } from "discourse-common/utils/decorators";
 
 export default Controller.extend({
   filter: null,
-  filtered: false,
   showWords: false,
-  disableShowWords: alias("filtered"),
-  regularExpressions: null,
 
-  filterContentNow() {
-    if (!!isEmpty(this.allWatchedWords)) {
+  _filterContent() {
+    if (isEmpty(this.allWatchedWords)) {
       return;
     }
 
-    let filter;
-    if (this.filter) {
-      filter = this.filter.toLowerCase();
-    }
-
-    if (filter === undefined || filter.length < 1) {
+    if (!this.filter) {
       this.set("model", this.allWatchedWords);
       return;
     }
 
-    const matchesByAction = [];
+    const filter = this.filter.toLowerCase();
+    const model = [];
 
     this.allWatchedWords.forEach((wordsForAction) => {
       const wordRecords = wordsForAction.words.filter((wordRecord) => {
         return wordRecord.word.indexOf(filter) > -1;
       });
-      matchesByAction.pushObject(
+
+      model.pushObject(
         EmberObject.create({
           nameKey: wordsForAction.nameKey,
           name: wordsForAction.name,
           words: wordRecords,
-          count: wordRecords.length,
         })
       );
     });
 
-    this.set("model", matchesByAction);
+    this.set("model", model);
   },
 
   @observes("filter")
   filterContent() {
-    discourseDebounce(
-      this,
-      function () {
-        this.filterContentNow();
-        this.set("filtered", !isEmpty(this.filter));
-      },
-      INPUT_DELAY
-    );
+    discourseDebounce(this, this._filterContent, INPUT_DELAY);
   },
 
-  actions: {
-    clearFilter() {
-      this.setProperties({ filter: "" });
-    },
+  @action
+  clearFilter() {
+    this.set("filter", "");
+  },
 
-    toggleMenu() {
-      $(".admin-detail").toggleClass("mobile-closed mobile-open");
-    },
+  @action
+  toggleMenu() {
+    $(".admin-detail").toggleClass("mobile-closed mobile-open");
   },
 });

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

GitHub sha: b56e9ad6

This commit appears in #13103 which was approved by gschlager. It was merged by nbianca.