Skip to content

A platform for community discussion. Free, open, simple.

DEV: Clean up watched-words controller/component (#29106)

Jarek Radoszd3793f7aea77
diff --git a/app/assets/javascripts/admin/addon/components/modal/watched-word-test.hbs b/app/assets/javascripts/admin/addon/components/modal/watched-word-test.hbs
deleted file mode 100644
index 4c9f9d073d2..00000000000
--- a/app/assets/javascripts/admin/addon/components/modal/watched-word-test.hbs
+++ /dev/null
@@ -1,45 +0,0 @@
-<DModal
- @title={{i18n
- "admin.watched_words.test.modal_title"
- }}
- @closeModal={{@closeModal}}
- class="watched-words-test-modal"
->
- <:body>
- <p>{{i18n "admin.watched_words.test.description"}}</p>
- <Textarea name="test_value" @value={{this.value}} autofocus="autofocus" />
- {{#if this.matches}}
- <p>
- {{i18n "admin.watched_words.test.found_matches"}}
- <ul>
- {{#if (or this.isReplace this.isLink)}}
- {{#each this.matches as |match|}}
- <li>
- <span class="match">{{match.match}}</span>
- &rarr;
- <span class="replacement">{{match.replacement}}</span>
- </li>
- {{/each}}
- {{else if this.isTag}}
- {{#each this.matches as |match|}}
- <li>
- <span class="match">{{match.match}}</span>
- &rarr;
- {{#each match.tags as |tag|}}
- <span class="tag">{{tag}}</span>
- {{/each}}
- </li>
- {{/each}}
- {{else}}
- {{#each this.matches as |match|}}
- <li>{{match}}</li>
- {{/each}}
- {{/if}}
- </ul>
- </p>
- {{else}}
- <p>{{i18n "admin.watched_words.test.no_matches"}}</p>
- {{/if}}
- </:body>
-</DModal>
\ No newline at end of file
diff --git a/app/assets/javascripts/admin/addon/components/modal/watched-word-test.js b/app/assets/javascripts/admin/addon/components/modal/watched-word-testing.gjs
similarity index 55%
rename from app/assets/javascripts/admin/addon/components/modal/watched-word-test.js
rename to app/assets/javascripts/admin/addon/components/modal/watched-word-testing.gjs
index 67474568c08..45b059ccb11 100644
--- a/app/assets/javascripts/admin/addon/components/modal/watched-word-test.js
+++ b/app/assets/javascripts/admin/addon/components/modal/watched-word-testing.gjs
@@ -1,7 +1,11 @@
import Component from "@glimmer/component";
import { tracked } from "@glimmer/tracking";
+import { Textarea } from "@ember/component";
+import { or } from "truth-helpers";
+import DModal from "discourse/components/d-modal";
+import i18n from "discourse-common/helpers/i18n";
-export default class WatchedWordTest extends Component {
+export default class WatchedWordTesting extends Component {
@tracked value;
get isReplace() {
@@ -81,4 +85,51 @@ export default class WatchedWordTest extends Component {
return matches;
}
+
+ <template>
+ <DModal
+ @title={{i18n
+ "admin.watched_words.test.modal_title"
+ }}
+ @closeModal={{@closeModal}}
+ class="watched-words-test-modal"
+ >
+ <:body>
+ <p>{{i18n "admin.watched_words.test.description"}}</p>
+ <Textarea
+ @value={{this.value}}
+ name="test_value"
+ autofocus="autofocus"
+ />
+
+ {{#if this.matches}}
+ <p>
+ {{i18n "admin.watched_words.test.found_matches"}}
+ <ul>
+ {{#each this.matches as |match|}}
+ <li>
+ {{#if (or this.isReplace this.isLink)}}
+ <span class="match">{{match.match}}</span>
+ &rarr;
+ <span class="replacement">{{match.replacement}}</span>
+ {{else if this.isTag}}
+ <span class="match">{{match.match}}</span>
+ &rarr;
+ {{#each match.tags as |tag|}}
+ <span class="tag">{{tag}}</span>
+ {{/each}}
+ {{else}}
+ {{match}}
+ {{/if}}
+ </li>
+ {{/each}}
+ </ul>
+ </p>
+ {{else}}
+ <p>{{i18n "admin.watched_words.test.no_matches"}}</p>
+ {{/if}}
+ </:body>
+ </DModal>
+ </template>
}
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 d5ad0c497cc..1a7624a3028 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
@@ -7,7 +7,7 @@ import { ajax } from "discourse/lib/ajax";
import { fmt } from "discourse/lib/computed";
import discourseComputed from "discourse-common/utils/decorators";
import I18n from "discourse-i18n";
-import WatchedWordTestModal from "admin/components/modal/watched-word-test";
+import WatchedWordTestingModal from "admin/components/modal/watched-word-testing";
import WatchedWord from "admin/models/watched-word";
export default class AdminWatchedWordsActionController extends Controller {
@@ -45,7 +45,7 @@ export default class AdminWatchedWordsActionController extends Controller {
@discourseComputed("actionNameKey")
actionDescription(actionNameKey) {
- return I18n.t("admin.watched_words.action_descriptions." + actionNameKey);
+ return I18n.t(`admin.watched_words.action_descriptions.${actionNameKey}`);
}
@action
@@ -58,43 +58,37 @@ export default class AdminWatchedWordsActionController extends Controller {
foundAction.words.unshiftObject(arg);
schedule("afterRender", () => {
// remove from other actions lists
- let match = null;
- this.adminWatchedWords.model.forEach((otherAction) => {
- if (match) {
- return;
+ for (const otherAction of this.adminWatchedWords.model) {
+ if (otherAction.nameKey === this.actionNameKey) {
+ continue;
}
- if (otherAction.nameKey !== this.actionNameKey) {
- match = otherAction.words.findBy("id", arg.id);
- if (match) {
- otherAction.words.removeObject(match);
- }
+ const match = otherAction.words.findBy("id", arg.id);
+ if (match) {
+ otherAction.words.removeObject(match);
+ break;
}
- });
+ }
});
}
@action
recordRemoved(arg) {
- if (this.currentAction) {
- this.currentAction.words.removeObject(arg);
- }
+ this.currentAction?.words.removeObject(arg);
}
@action
- uploadComplete() {
- WatchedWord.findAll().then((data) => {
- this.adminWatchedWords.set("model", data);
- });
+ async uploadComplete() {
+ const data = await WatchedWord.findAll();
+ this.adminWatchedWords.set("model", data);
}
@action
- test() {
- WatchedWord.findAll().then((data) => {
- this.adminWatchedWords.set("model", data);
- this.modal.show(WatchedWordTestModal, {
- model: { watchedWord: this.currentAction },
- });
+ async test() {
+ const data = await WatchedWord.findAll();
+ this.adminWatchedWords.set("model", data);
+ this.modal.show(WatchedWordTestingModal, {
+ model: { watchedWord: this.currentAction },
});
}
@@ -103,17 +97,14 @@ export default class AdminWatchedWordsActionController extends Controller {
const actionKey = this.actionNameKey;
this.dialog.yesNoConfirm({
message: I18n.t("admin.watched_words.clear_all_confirm", {
- action: I18n.t("admin.watched_words.actions." + actionKey),
+ action: I18n.t(`admin.watched_words.actions.${actionKey}`),
}),
- didConfirm: () => {
- ajax(`/admin/customize/watched_words/action/${actionKey}.json`, {
+ didConfirm: async () => {
+ await ajax(`/admin/customize/watched_words/action/${actionKey}.json`, {
type: "DELETE",
- }).then(() => {
- const foundAction = this.findAction(actionKey);
- if (foundAction) {
- foundAction.set("words", []);
- }
});
+
+ this.findAction(actionKey)?.set("words", []);
},
});
}
diff --git a/app/assets/javascripts/admin/addon/templates/watched-words-action.hbs b/app/assets/javascripts/admin/addon/templates/watched-words-action.hbs
index c06ae14cda6..587bf0b5968 100644
--- a/app/assets/javascripts/admin/addon/templates/watched-words-action.hbs
+++ b/app/assets/javascripts/admin/addon/templates/watched-words-action.hbs
@@ -14,7 +14,7 @@
@id="watched-word-uploader"
@uploading={{this.uploading}}
@actionKey={{this.actionNameKey}}
- @done={{action "uploadComplete"}}
+ @done={{this.uploadComplete}}
/>
<DButton
@@ -35,14 +35,16 @@
<p class="about">{{this.actionDescription}}</p>
{{#if this.siteSettings.watched_words_regular_expressions}}
- <p>{{html-safe
+ <p>
+ {{html-safe
(i18n "admin.watched_words.regex_warning" basePath=(base-path))
- }}</p>
+ }}
+ </p>
{{/if}}
<WatchedWordForm
@actionKey={{this.actionNameKey}}
- @action={{action "recordAdded"}}
+ @action={{this.recordAdded}}
@filteredContent={{this.currentAction.words}}
/>
@@ -67,7 +69,7 @@
<AdminWatchedWord
@actionKey={{this.actionNameKey}}
@word={{word}}
- @action={{action "recordRemoved"}}
+ @action={{this.recordRemoved}}
/>
</div>
{{/each}}