discourse/app/assets/javascripts/admin/addon/components/permalink-form.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

94 lines
2.5 KiB
JavaScript
Raw Normal View History

import Component from "@ember/component";
import { action } from "@ember/object";
import { schedule } from "@ember/runloop";
import { service } from "@ember/service";
import { tagName } from "@ember-decorators/component";
import { fmt } from "discourse/lib/computed";
import discourseComputed, { bind } from "discourse-common/utils/decorators";
import I18n from "discourse-i18n";
import Permalink from "admin/models/permalink";
2017-07-06 04:47:01 +08:00
@tagName("")
export default class PermalinkForm extends Component {
@service dialog;
formSubmitted = false;
permalinkType = "topic_id";
@fmt("permalinkType", "admin.permalink.%@") permalinkTypePlaceholder;
action = null;
permalinkTypeValue = null;
2015-07-15 20:54:28 +08:00
@discourseComputed
permalinkTypes() {
2015-07-15 20:54:28 +08:00
return [
{ id: "topic_id", name: I18n.t("admin.permalink.topic_id") },
{ id: "post_id", name: I18n.t("admin.permalink.post_id") },
{ id: "category_id", name: I18n.t("admin.permalink.category_id") },
2020-05-25 17:48:54 +08:00
{ id: "tag_name", name: I18n.t("admin.permalink.tag_name") },
2015-07-15 20:54:28 +08:00
{ id: "external_url", name: I18n.t("admin.permalink.external_url") },
2024-02-06 00:31:31 +08:00
{ id: "user_id", name: I18n.t("admin.permalink.user_id") },
2015-07-15 20:54:28 +08:00
];
}
2015-07-15 20:54:28 +08:00
@bind
focusPermalink() {
schedule("afterRender", () =>
2022-09-28 02:47:13 +08:00
document.querySelector(".permalink-url")?.focus()
);
}
2015-07-15 20:54:28 +08:00
@action
submitFormOnEnter(event) {
if (event.key === "Enter") {
this.onSubmit();
}
}
@action
onSubmit() {
if (!this.formSubmitted) {
this.set("formSubmitted", true);
Permalink.create({
url: this.url,
permalink_type: this.permalinkType,
permalink_type_value: this.permalinkTypeValue,
})
.save()
.then(
(result) => {
this.setProperties({
url: "",
permalinkTypeValue: "",
formSubmitted: false,
});
this.action(Permalink.create(result.permalink));
this.focusPermalink();
},
(e) => {
this.set("formSubmitted", false);
let error;
if (e?.jqXHR?.responseJSON?.errors) {
error = I18n.t("generic_error_with_reason", {
error: e.jqXHR.responseJSON.errors.join(". "),
});
} else {
error = I18n.t("generic_error");
2018-06-15 23:03:24 +08:00
}
2022-09-28 02:47:13 +08:00
this.dialog.alert({
message: error,
didConfirm: () => this.focusPermalink(),
didCancel: () => this.focusPermalink(),
});
}
);
}
}
}