Skip to content

Commit

Permalink
update the subscription when conversation is created
Browse files Browse the repository at this point in the history
  • Loading branch information
rockwellll committed Jan 13, 2025
1 parent 0ca1e76 commit 326d99c
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 50 deletions.
2 changes: 1 addition & 1 deletion dist/hellotext.js

Large diffs are not rendered by default.

26 changes: 7 additions & 19 deletions lib/channels/webchat_channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,43 +23,30 @@ function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.g
var WebchatChannel = /*#__PURE__*/function (_ApplicationChannel) {
_inherits(WebchatChannel, _ApplicationChannel);
var _super = _createSuper(WebchatChannel);
function WebchatChannel(id, session) {
function WebchatChannel(id, session, conversation) {
var _this;
_classCallCheck(this, WebchatChannel);
_this = _super.call(this);
_this.id = id;
_this.session = session;
_this.conversation = conversation;
_this.subscribe();
return _this;
}
_createClass(WebchatChannel, [{
key: "subscribe",
value: function subscribe(conversation) {
value: function subscribe() {
var params = {
channel: "WebchatChannel",
id: this.id,
session: this.session,
conversation
conversation: this.conversation
};
console.log(params);
this.send({
command: 'subscribe',
identifier: params
});
}
}, {
key: "unsubscribe",
value: function unsubscribe() {
var params = {
channel: "WebchatChannel",
id: this.id,
session: this.session
};
this.send({
command: 'unsubscribe',
identifier: params
});
}
}, {
key: "onMessage",
value: function onMessage(callback) {
Expand Down Expand Up @@ -96,8 +83,9 @@ var WebchatChannel = /*#__PURE__*/function (_ApplicationChannel) {
});
}
}, {
key: "updateSubscription",
value: function updateSubscription() {
key: "updateSubscriptionWith",
value: function updateSubscriptionWith(conversation) {
this.conversation = conversation;
this.subscribe();
}
}]);
Expand Down
11 changes: 5 additions & 6 deletions lib/controllers/webchat_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ var _default = /*#__PURE__*/function (_Controller) {
key: "initialize",
value: function initialize() {
this.messagesAPI = new _messages.default(this.idValue);
this.webChatChannel = new _webchat_channel.default(this.idValue, _hellotext.default.session);
this.webChatChannel = new _webchat_channel.default(this.idValue, _hellotext.default.session, this.conversationIdValue);
this.files = [];
this.onMessageReceived = this.onMessageReceived.bind(this);
this.onConversationAssignment = this.onConversationAssignment.bind(this);
Expand Down Expand Up @@ -241,7 +241,6 @@ var _default = /*#__PURE__*/function (_Controller) {
} else {
this.onlineStatusTarget.style.display = 'none';
}
this.webChatChannel.updateSubscription();
}
}, {
key: "onAgentOnline",
Expand Down Expand Up @@ -288,13 +287,13 @@ var _default = /*#__PURE__*/function (_Controller) {
return element.classList.add('failed');
}
var data = yield response.json();
console.log(data);
element.setAttribute('data-id', data.id);
message.id = data.id;
_hellotext.default.eventEmitter.dispatch('webchat:message:sent', message);
this.conversationIdValue = data.conversation;
console.log(this.conversationIdValue);
this.webChatChannel.subscribe(this.conversationIdValue);
if (data.conversation !== this.conversationIdValue) {
this.conversationIdValue = data.conversation;
this.webChatChannel.subscribe(this.conversationIdValue);
}
});
function sendMessage() {
return _sendMessage.apply(this, arguments);
Expand Down
22 changes: 6 additions & 16 deletions src/channels/webchat_channel.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,27 @@
import ApplicationChannel from './application_channel'

class WebchatChannel extends ApplicationChannel {
constructor(id, session) {
constructor(id, session, conversation) {
super()

this.id = id
this.session = session
this.conversation = conversation

this.subscribe()
}

subscribe(conversation) {
subscribe() {
const params = {
channel: "WebchatChannel",
id: this.id,
session: this.session,
conversation,
conversation: this.conversation,
}

console.log(params)

this.send( { command: 'subscribe', identifier: params })
}

unsubscribe() {
const params = {
channel: "WebchatChannel",
id: this.id,
session: this.session
}

this.send({ command: 'unsubscribe', identifier: params })
}

onMessage(callback) {
super.onMessage((message) => {
if(message.type !== 'message') return
Expand Down Expand Up @@ -64,7 +53,8 @@ class WebchatChannel extends ApplicationChannel {
})
}

updateSubscription() {
updateSubscriptionWith(conversation) {
this.conversation = conversation
this.subscribe()
}
}
Expand Down
13 changes: 5 additions & 8 deletions src/controllers/webchat_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export default class extends Controller {

initialize() {
this.messagesAPI = new WebchatMessagesAPI(this.idValue)
this.webChatChannel = new WebchatChannel(this.idValue, Hellotext.session)
this.webChatChannel = new WebchatChannel(this.idValue, Hellotext.session, this.conversationIdValue)
this.files = []

this.onMessageReceived = this.onMessageReceived.bind(this)
Expand Down Expand Up @@ -261,8 +261,6 @@ export default class extends Controller {
} else {
this.onlineStatusTarget.style.display = 'none'
}

this.webChatChannel.updateSubscription()
}

onAgentOnline() {
Expand Down Expand Up @@ -319,16 +317,15 @@ export default class extends Controller {
}

const data = await response.json()

console.log(data)
element.setAttribute('data-id', data.id)
message.id = data.id

Hellotext.eventEmitter.dispatch('webchat:message:sent', message)

this.conversationIdValue = data.conversation
console.log(this.conversationIdValue)
this.webChatChannel.subscribe(this.conversationIdValue)
if(data.conversation !== this.conversationIdValue) {
this.conversationIdValue = data.conversation
this.webChatChannel.subscribe(this.conversationIdValue)
}
}

openAttachment() {
Expand Down

0 comments on commit 326d99c

Please sign in to comment.