2019-10-24 00:30:52 +08:00
|
|
|
import Component from "@ember/component";
|
2021-11-16 17:25:54 +08:00
|
|
|
import { action } from "@ember/object";
|
2023-10-11 02:38:59 +08:00
|
|
|
import { schedule } from "@ember/runloop";
|
2024-03-07 01:05:11 +08:00
|
|
|
import { service } from "@ember/service";
|
2023-10-11 02:38:59 +08:00
|
|
|
import { tagName } from "@ember-decorators/component";
|
|
|
|
import { fmt } from "discourse/lib/computed";
|
|
|
|
import discourseComputed, { bind } from "discourse-common/utils/decorators";
|
2023-10-18 18:07:09 +08:00
|
|
|
import I18n from "discourse-i18n";
|
2023-10-11 02:38:59 +08:00
|
|
|
import Permalink from "admin/models/permalink";
|
2017-07-06 04:47:01 +08:00
|
|
|
|
2023-02-23 23:32:53 +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
|
|
|
|
2019-11-08 05:38:28 +08:00
|
|
|
@discourseComputed
|
2019-01-22 22:09:21 +08:00
|
|
|
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
|
|
|
];
|
2023-02-23 23:32:53 +08:00
|
|
|
}
|
2015-07-15 20:54:28 +08:00
|
|
|
|
2021-11-16 17:25:54 +08:00
|
|
|
@bind
|
2019-01-22 22:09:21 +08:00
|
|
|
focusPermalink() {
|
2019-10-30 21:48:24 +08:00
|
|
|
schedule("afterRender", () =>
|
2022-09-28 02:47:13 +08:00
|
|
|
document.querySelector(".permalink-url")?.focus()
|
2019-07-16 18:45:15 +08:00
|
|
|
);
|
2023-02-23 23:32:53 +08:00
|
|
|
}
|
2015-07-15 20:54:28 +08:00
|
|
|
|
2021-11-16 17:25:54 +08:00
|
|
|
@action
|
|
|
|
submitFormOnEnter(event) {
|
|
|
|
if (event.key === "Enter") {
|
|
|
|
this.onSubmit();
|
|
|
|
}
|
2023-02-23 23:32:53 +08:00
|
|
|
}
|
2021-11-16 17:25:54 +08:00
|
|
|
|
|
|
|
@action
|
|
|
|
onSubmit() {
|
|
|
|
if (!this.formSubmitted) {
|
|
|
|
this.set("formSubmitted", true);
|
2019-01-22 22:09:21 +08:00
|
|
|
|
2021-11-16 17:25:54 +08:00
|
|
|
Permalink.create({
|
|
|
|
url: this.url,
|
|
|
|
permalink_type: this.permalinkType,
|
|
|
|
permalink_type_value: this.permalinkTypeValue,
|
|
|
|
})
|
|
|
|
.save()
|
|
|
|
.then(
|
|
|
|
(result) => {
|
|
|
|
this.setProperties({
|
|
|
|
url: "",
|
|
|
|
permalinkTypeValue: "",
|
|
|
|
formSubmitted: false,
|
|
|
|
});
|
2019-01-22 22:09:21 +08:00
|
|
|
|
2021-11-16 17:25:54 +08:00
|
|
|
this.action(Permalink.create(result.permalink));
|
2019-01-22 22:09:21 +08:00
|
|
|
|
2021-11-16 17:25:54 +08:00
|
|
|
this.focusPermalink();
|
|
|
|
},
|
|
|
|
(e) => {
|
|
|
|
this.set("formSubmitted", false);
|
2019-01-22 22:09:21 +08:00
|
|
|
|
2021-11-16 17:25:54 +08:00
|
|
|
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(),
|
|
|
|
});
|
2021-11-16 17:25:54 +08:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2023-02-23 23:32:53 +08:00
|
|
|
}
|
|
|
|
}
|