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";
|
2024-11-20 04:45:18 +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 [
|
2024-11-20 04:45:18 +08:00
|
|
|
{ id: "topic_id", name: i18n("admin.permalink.topic_id") },
|
|
|
|
{ id: "post_id", name: i18n("admin.permalink.post_id") },
|
|
|
|
{ id: "category_id", name: i18n("admin.permalink.category_id") },
|
|
|
|
{ id: "tag_name", name: i18n("admin.permalink.tag_name") },
|
|
|
|
{ id: "external_url", name: i18n("admin.permalink.external_url") },
|
|
|
|
{ id: "user_id", name: i18n("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) {
|
2024-11-20 04:45:18 +08:00
|
|
|
error = i18n("generic_error_with_reason", {
|
2021-11-16 17:25:54 +08:00
|
|
|
error: e.jqXHR.responseJSON.errors.join(". "),
|
|
|
|
});
|
|
|
|
} else {
|
2024-11-20 04:45:18 +08:00
|
|
|
error = i18n("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
|
|
|
}
|
|
|
|
}
|