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/components/chat-draft-channel-screen.js
Roman Rizzi 0a5f548635
DEV: Move discourse-chat to the core repo. (#18776)
As part of this move, we are also renaming `discourse-chat` to `chat`.
2022-11-02 10:41:30 -03:00

54 lines
1.3 KiB
JavaScript

import ChatChannel from "discourse/plugins/chat/discourse/models/chat-channel";
import { inject as service } from "@ember/service";
import Component from "@ember/component";
import { action } from "@ember/object";
import { cloneJSON } from "discourse-common/lib/object";
export default class ChatDraftChannelScreen extends Component {
@service chat;
@service router;
tagName = "";
onSwitchChannel = null;
@action
onCancelChatDraft() {
return this.router.transitionTo("chat.index");
}
@action
onChangeSelectedUsers(users) {
this._fetchPreviewedChannel(users);
}
@action
onSwitchFromDraftChannel(channel) {
channel.set("isDraft", false);
this.onSwitchChannel?.(channel);
}
_fetchPreviewedChannel(users) {
this.set("previewedChannel", null);
return this.chat
.getDmChannelForUsernames(users.mapBy("username"))
.then((response) => {
this.set(
"previewedChannel",
ChatChannel.create(
Object.assign({}, response.chat_channel, { isDraft: true })
)
);
})
.catch((error) => {
if (error?.jqXHR?.status === 404) {
this.set(
"previewedChannel",
ChatChannel.create({
chatable: { users: cloneJSON(users) },
isDraft: true,
})
);
}
});
}
}