DEV: Fix submit-on-enter and de-jQ two components (#17484)

This commit is contained in:
Jarek Radosz 2022-07-14 02:59:33 +02:00 committed by GitHub
parent 3ada82f713
commit ad2c0aaee3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 24 additions and 45 deletions

View File

@ -1,4 +1,4 @@
import discourseComputed, { on } from "discourse-common/utils/decorators";
import discourseComputed from "discourse-common/utils/decorators";
import Component from "@ember/component";
import I18n from "I18n";
import ScreenedIpAddress from "admin/models/screened-ip-address";
@ -17,6 +17,7 @@ import { schedule } from "@ember/runloop";
**/
export default Component.extend({
tagName: "form",
classNames: ["screened-ip-address-form", "inline-form"],
formSubmitted: false,
actionName: "block",
@ -60,33 +61,23 @@ export default Component.extend({
this.setProperties({ ip_address: "", formSubmitted: false });
this.action(ScreenedIpAddress.create(result.screened_ip_address));
schedule("afterRender", () =>
this.element.querySelector(".ip-address-input").focus()
this.element.querySelector("input").focus()
);
})
.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");
const msg = 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(".ip-address-input").focus()
schedule("afterRender", () =>
this.element.querySelector("input").focus()
)
);
});
}
},
},
@on("didInsertElement")
_init() {
schedule("afterRender", () => {
$(this.element.querySelector(".ip-address-input")).keydown((e) => {
if (e.key === "Enter") {
this.send("submit");
}
});
});
},
});

View File

@ -1,7 +1,4 @@
import discourseComputed, {
observes,
on,
} from "discourse-common/utils/decorators";
import discourseComputed, { observes } from "discourse-common/utils/decorators";
import Component from "@ember/component";
import I18n from "I18n";
import WatchedWord from "admin/models/watched-word";
@ -11,6 +8,7 @@ import { isEmpty } from "@ember/utils";
import { schedule } from "@ember/runloop";
export default Component.extend({
tagName: "form",
classNames: ["watched-word-form"],
formSubmitted: false,
actionKey: null,
@ -95,33 +93,23 @@ export default Component.extend({
});
this.action(WatchedWord.create(result));
schedule("afterRender", () =>
this.element.querySelector(".watched-word-input").focus()
this.element.querySelector("input").focus()
);
})
.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");
const msg = 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()
schedule("afterRender", () =>
this.element.querySelector("input").focus()
)
);
});
}
},
},
@on("didInsertElement")
_init() {
schedule("afterRender", () => {
$(this.element.querySelector(".watched-word-input")).keydown((e) => {
if (e.key === "Enter") {
this.send("submit");
}
});
});
},
});

View File

@ -3,4 +3,4 @@
<ComboBox @content={{this.actionNames}} @value={{this.actionName}} @onChange={{action (mut this.actionName)}} />
<DButton @class="btn-default" @action={{action "submit"}} @disabled={{this.formSubmitted}} @label="admin.logs.screened_ips.form.add" />
<DButton @type="submit" @class="btn-default" @action={{action "submit"}} @disabled={{this.formSubmitted}} @label="admin.logs.screened_ips.form.add" />

View File

@ -27,7 +27,7 @@
</div>
{{/if}}
<DButton @class="btn btn-primary" @action={{action "submit"}} @disabled={{this.formSubmitted}} @label="admin.watched_words.form.add" />
<DButton @type="submit" @class="btn btn-primary" @action={{action "submit"}} @disabled={{this.formSubmitted}} @label="admin.watched_words.form.add" />
{{#if this.showMessage}}
<span class="success-message">{{this.message}}</span>