discourse/app/assets/javascripts/admin/components/watched-word-form.js

Failed to ignore revisions in .git-blame-ignore-revs.

99 lines
2.6 KiB
JavaScript
Raw Normal View History

import { isEmpty } from "@ember/utils";
import { schedule } from "@ember/runloop";
import Component from "@ember/component";
2018-06-15 11:03:24 -04:00
import WatchedWord from "admin/models/watched-word";
import discourseComputed, {
2018-06-15 11:03:24 -04:00
on,
observes
} from "discourse-common/utils/decorators";
export default Component.extend({
2018-06-15 11:03:24 -04:00
classNames: ["watched-word-form"],
formSubmitted: false,
actionKey: null,
showMessage: false,
@discourseComputed("regularExpressions")
placeholderKey(regularExpressions) {
2018-06-15 11:03:24 -04:00
return (
"admin.watched_words.form.placeholder" +
(regularExpressions ? "_regexp" : "")
);
},
2018-06-15 11:03:24 -04:00
@observes("word")
removeMessage() {
if (this.showMessage && !isEmpty(this.word)) {
2018-06-15 11:03:24 -04:00
this.set("showMessage", false);
}
},
@discourseComputed("word")
isUniqueWord(word) {
const words = this.filteredContent || [];
const filtered = words.filter(content => content.action === this.actionKey);
2018-06-15 11:03:24 -04:00
return filtered.every(
content => content.word.toLowerCase() !== word.toLowerCase()
);
},
actions: {
submit() {
if (!this.isUniqueWord) {
2018-06-15 11:03:24 -04:00
this.setProperties({
showMessage: true,
message: I18n.t("admin.watched_words.form.exists")
});
return;
}
if (!this.formSubmitted) {
2018-06-15 11:03:24 -04:00
this.set("formSubmitted", true);
2018-06-15 11:03:24 -04:00
const watchedWord = WatchedWord.create({
word: this.word,
action: this.actionKey
});
2018-06-15 11:03:24 -04:00
watchedWord
.save()
.then(result => {
this.setProperties({
word: "",
formSubmitted: false,
showMessage: true,
message: I18n.t("admin.watched_words.form.success")
});
this.action(WatchedWord.create(result));
schedule("afterRender", () =>
this.element.querySelector(".watched-word-input").focus()
2018-06-15 11:03:24 -04:00
);
})
.catch(e => {
this.set("formSubmitted", false);
const msg =
e.jqXHR.responseJSON && e.jqXHR.responseJSON.errors
? I18n.t("generic_error_with_reason", {
error: e.jqXHR.responseJSON.errors.join(". ")
})
: I18n.t("generic_error");
bootbox.alert(msg, () =>
this.element.querySelector(".watched-word-input").focus()
);
2018-06-15 11:03:24 -04:00
});
}
}
},
@on("didInsertElement")
_init() {
schedule("afterRender", () => {
$(this.element.querySelector(".watched-word-input")).keydown(e => {
if (e.keyCode === 13) {
2018-06-15 11:03:24 -04:00
this.send("submit");
}
});
});
}
});