This repository has been archived on 2023-03-18. You can view files and clone it, but cannot push or open issues or pull requests.
osr-discourse-src/plugins/chat/assets/javascripts/discourse/controllers/chat-channel-edit-name-slug.js
Martin Brennan db5ad34508
FEATURE: Allow editing channel slug (#19948)
This commit introduces the ability to edit the channel
slug from the About tab for the chat channel when the user
is admin. Similar to the create channel modal functionality
introduced in 641e94f, if
the slug is left empty then we autogenerate a slug based
on the channel name, and if the user just changes the slug
manually we use that instead.

We do not do any link remapping or anything else of the
sort, when the category slug is changed that does not happen
either.
2023-01-30 13:18:34 +10:00

95 lines
2.4 KiB
JavaScript

import Controller from "@ember/controller";
import discourseDebounce from "discourse-common/lib/debounce";
import { ajax } from "discourse/lib/ajax";
import { cancel } from "@ember/runloop";
import { action, computed } from "@ember/object";
import { extractError } from "discourse/lib/ajax-error";
import ModalFunctionality from "discourse/mixins/modal-functionality";
import { inject as service } from "@ember/service";
export default class ChatChannelEditTitleController extends Controller.extend(
ModalFunctionality
) {
@service chatApi;
editedName = "";
editedSlug = "";
autoGeneratedSlug = "";
@computed("model.title", "editedName", "editedSlug")
get isSaveDisabled() {
return (
(this.model.title === this.editedName &&
this.model.slug === this.editedSlug) ||
this.editedName?.length > this.siteSettings.max_topic_title_length
);
}
onShow() {
this.setProperties({
editedName: this.model.title,
editedSlug: this.model.slug,
});
}
onClose() {
this.setProperties({
editedName: "",
editedSlug: "",
});
this.clearFlash();
}
@action
onSaveChatChannelName() {
return this.chatApi
.updateChannel(this.model.id, {
name: this.editedName,
slug: this.editedSlug || this.autoGeneratedSlug || this.model.slug,
})
.then((result) => {
this.model.set("title", result.channel.title);
this.send("closeModal");
})
.catch((event) => {
this.flash(extractError(event), "error");
});
}
@action
onChangeChatChannelName(title) {
this.clearFlash();
this._debouncedGenerateSlug(title);
}
@action
onChangeChatChannelSlug() {
this.clearFlash();
this._debouncedGenerateSlug(this.editedName);
}
_clearAutoGeneratedSlug() {
this.set("autoGeneratedSlug", "");
}
_debouncedGenerateSlug(name) {
cancel(this.generateSlugHandler);
this._clearAutoGeneratedSlug();
if (!name) {
return;
}
this.generateSlugHandler = discourseDebounce(
this,
this._generateSlug,
name,
300
);
}
// intentionally not showing AJAX error for this, we will autogenerate
// the slug server-side if they leave it blank
_generateSlug(name) {
ajax("/slugs.json", { type: "POST", data: { name } }).then((response) => {
this.set("autoGeneratedSlug", response.slug);
});
}
}