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 Component from "@ember/component";
import I18n from "I18n"; import I18n from "I18n";
import ScreenedIpAddress from "admin/models/screened-ip-address"; import ScreenedIpAddress from "admin/models/screened-ip-address";
@ -17,6 +17,7 @@ import { schedule } from "@ember/runloop";
**/ **/
export default Component.extend({ export default Component.extend({
tagName: "form",
classNames: ["screened-ip-address-form", "inline-form"], classNames: ["screened-ip-address-form", "inline-form"],
formSubmitted: false, formSubmitted: false,
actionName: "block", actionName: "block",
@ -60,33 +61,23 @@ export default Component.extend({
this.setProperties({ ip_address: "", formSubmitted: false }); this.setProperties({ ip_address: "", formSubmitted: false });
this.action(ScreenedIpAddress.create(result.screened_ip_address)); this.action(ScreenedIpAddress.create(result.screened_ip_address));
schedule("afterRender", () => schedule("afterRender", () =>
this.element.querySelector(".ip-address-input").focus() this.element.querySelector("input").focus()
); );
}) })
.catch((e) => { .catch((e) => {
this.set("formSubmitted", false); this.set("formSubmitted", false);
const msg = const msg = e.jqXHR.responseJSON?.errors
e.jqXHR.responseJSON && e.jqXHR.responseJSON.errors ? I18n.t("generic_error_with_reason", {
? I18n.t("generic_error_with_reason", { error: e.jqXHR.responseJSON.errors.join(". "),
error: e.jqXHR.responseJSON.errors.join(". "), })
}) : I18n.t("generic_error");
: I18n.t("generic_error");
bootbox.alert(msg, () => 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, { import discourseComputed, { observes } from "discourse-common/utils/decorators";
observes,
on,
} from "discourse-common/utils/decorators";
import Component from "@ember/component"; import Component from "@ember/component";
import I18n from "I18n"; import I18n from "I18n";
import WatchedWord from "admin/models/watched-word"; import WatchedWord from "admin/models/watched-word";
@ -11,6 +8,7 @@ import { isEmpty } from "@ember/utils";
import { schedule } from "@ember/runloop"; import { schedule } from "@ember/runloop";
export default Component.extend({ export default Component.extend({
tagName: "form",
classNames: ["watched-word-form"], classNames: ["watched-word-form"],
formSubmitted: false, formSubmitted: false,
actionKey: null, actionKey: null,
@ -95,33 +93,23 @@ export default Component.extend({
}); });
this.action(WatchedWord.create(result)); this.action(WatchedWord.create(result));
schedule("afterRender", () => schedule("afterRender", () =>
this.element.querySelector(".watched-word-input").focus() this.element.querySelector("input").focus()
); );
}) })
.catch((e) => { .catch((e) => {
this.set("formSubmitted", false); this.set("formSubmitted", false);
const msg = const msg = e.jqXHR.responseJSON?.errors
e.jqXHR.responseJSON && e.jqXHR.responseJSON.errors ? I18n.t("generic_error_with_reason", {
? I18n.t("generic_error_with_reason", { error: e.jqXHR.responseJSON.errors.join(". "),
error: e.jqXHR.responseJSON.errors.join(". "), })
}) : I18n.t("generic_error");
: I18n.t("generic_error");
bootbox.alert(msg, () => 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)}} /> <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> </div>
{{/if}} {{/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}} {{#if this.showMessage}}
<span class="success-message">{{this.message}}</span> <span class="success-message">{{this.message}}</span>