DEV: jquery removal/listeners/cleanup of permalink form (#14944)

- Removes jquery
- Removes a not unregistered listener and uses component event
- Removes external-url class as it was only valid in one case of the dropdown
- Uses @action
- Tagless
- Other minor changes
This commit is contained in:
Joffrey JAFFEUX 2021-11-16 10:25:54 +01:00 committed by GitHub
parent c9a84d8067
commit 43659a6de2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 80 additions and 89 deletions

View File

@ -2,15 +2,18 @@ import Component from "@ember/component";
import I18n from "I18n"; import I18n from "I18n";
import Permalink from "admin/models/permalink"; import Permalink from "admin/models/permalink";
import bootbox from "bootbox"; import bootbox from "bootbox";
import discourseComputed from "discourse-common/utils/decorators"; import discourseComputed, { bind } from "discourse-common/utils/decorators";
import { fmt } from "discourse/lib/computed"; import { fmt } from "discourse/lib/computed";
import { schedule } from "@ember/runloop"; import { schedule } from "@ember/runloop";
import { action } from "@ember/object";
export default Component.extend({ export default Component.extend({
classNames: ["permalink-form"], tagName: "",
formSubmitted: false, formSubmitted: false,
permalinkType: "topic_id", permalinkType: "topic_id",
permalinkTypePlaceholder: fmt("permalinkType", "admin.permalink.%@"), permalinkTypePlaceholder: fmt("permalinkType", "admin.permalink.%@"),
action: null,
permalinkTypeValue: null,
@discourseComputed @discourseComputed
permalinkTypes() { permalinkTypes() {
@ -23,40 +26,36 @@ export default Component.extend({
]; ];
}, },
didInsertElement() { @bind
this._super(...arguments);
schedule("afterRender", () => {
$(this.element.querySelector(".external-url")).keydown((e) => {
if (e.key === "Enter") {
this.send("submit");
}
});
});
},
focusPermalink() { focusPermalink() {
schedule("afterRender", () => schedule("afterRender", () =>
this.element.querySelector(".permalink-url").focus() this.element.querySelector(".permalink-url")?.focus()
); );
}, },
actions: { @action
submit() { submitFormOnEnter(event) {
if (event.key === "Enter") {
this.onSubmit();
}
},
@action
onSubmit() {
if (!this.formSubmitted) { if (!this.formSubmitted) {
this.set("formSubmitted", true); this.set("formSubmitted", true);
Permalink.create({ Permalink.create({
url: this.url, url: this.url,
permalink_type: this.permalinkType, permalink_type: this.permalinkType,
permalink_type_value: this.permalink_type_value, permalink_type_value: this.permalinkTypeValue,
}) })
.save() .save()
.then( .then(
(result) => { (result) => {
this.setProperties({ this.setProperties({
url: "", url: "",
permalink_type_value: "", permalinkTypeValue: "",
formSubmitted: false, formSubmitted: false,
}); });
@ -68,25 +67,16 @@ export default Component.extend({
this.set("formSubmitted", false); this.set("formSubmitted", false);
let error; let error;
if ( if (e?.jqXHR?.responseJSON?.errors) {
e.jqXHR &&
e.jqXHR.responseJSON &&
e.jqXHR.responseJSON.errors
) {
error = I18n.t("generic_error_with_reason", { error = I18n.t("generic_error_with_reason", {
error: e.jqXHR.responseJSON.errors.join(". "), error: e.jqXHR.responseJSON.errors.join(". "),
}); });
} else { } else {
error = I18n.t("generic_error"); error = I18n.t("generic_error");
} }
bootbox.alert(error, () => this.focusPermalink()); bootbox.alert(error, this.focusPermalink);
} }
); );
} }
}, },
onChangePermalinkType(type) {
this.set("permalinkType", type);
},
},
}); });

View File

@ -1,4 +1,5 @@
<div class="inline-form"> <div class="permalink-form">
<div class="inline-form">
<label>{{i18n "admin.permalink.form.label"}}</label> <label>{{i18n "admin.permalink.form.label"}}</label>
{{text-field {{text-field
@ -18,18 +19,18 @@
}} }}
{{text-field {{text-field
value=permalink_type_value value=permalinkTypeValue
disabled=formSubmitted disabled=formSubmitted
class="external-url"
placeholderKey=permalinkTypePlaceholder placeholderKey=permalinkTypePlaceholder
autocorrect="off" autocorrect="off"
autocapitalize="off" autocapitalize="off"
keyDown=(action "submitFormOnEnter")
}} }}
{{d-button {{d-button
class="btn-default" action=(action "onSubmit")
action=(action "submit")
disabled=formSubmitted disabled=formSubmitted
label="admin.permalink.form.add" label="admin.permalink.form.add"
}} }}
</div>
</div> </div>