Skip to content

Commit 326d99c

Browse files
committed
update the subscription when conversation is created
1 parent 0ca1e76 commit 326d99c

File tree

5 files changed

+24
-50
lines changed

5 files changed

+24
-50
lines changed

dist/hellotext.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/channels/webchat_channel.js

+7-19
Original file line numberDiff line numberDiff line change
@@ -23,43 +23,30 @@ function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.g
2323
var WebchatChannel = /*#__PURE__*/function (_ApplicationChannel) {
2424
_inherits(WebchatChannel, _ApplicationChannel);
2525
var _super = _createSuper(WebchatChannel);
26-
function WebchatChannel(id, session) {
26+
function WebchatChannel(id, session, conversation) {
2727
var _this;
2828
_classCallCheck(this, WebchatChannel);
2929
_this = _super.call(this);
3030
_this.id = id;
3131
_this.session = session;
32+
_this.conversation = conversation;
3233
_this.subscribe();
3334
return _this;
3435
}
3536
_createClass(WebchatChannel, [{
3637
key: "subscribe",
37-
value: function subscribe(conversation) {
38+
value: function subscribe() {
3839
var params = {
3940
channel: "WebchatChannel",
4041
id: this.id,
4142
session: this.session,
42-
conversation
43+
conversation: this.conversation
4344
};
44-
console.log(params);
4545
this.send({
4646
command: 'subscribe',
4747
identifier: params
4848
});
4949
}
50-
}, {
51-
key: "unsubscribe",
52-
value: function unsubscribe() {
53-
var params = {
54-
channel: "WebchatChannel",
55-
id: this.id,
56-
session: this.session
57-
};
58-
this.send({
59-
command: 'unsubscribe',
60-
identifier: params
61-
});
62-
}
6350
}, {
6451
key: "onMessage",
6552
value: function onMessage(callback) {
@@ -96,8 +83,9 @@ var WebchatChannel = /*#__PURE__*/function (_ApplicationChannel) {
9683
});
9784
}
9885
}, {
99-
key: "updateSubscription",
100-
value: function updateSubscription() {
86+
key: "updateSubscriptionWith",
87+
value: function updateSubscriptionWith(conversation) {
88+
this.conversation = conversation;
10189
this.subscribe();
10290
}
10391
}]);

lib/controllers/webchat_controller.js

+5-6
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ var _default = /*#__PURE__*/function (_Controller) {
4444
key: "initialize",
4545
value: function initialize() {
4646
this.messagesAPI = new _messages.default(this.idValue);
47-
this.webChatChannel = new _webchat_channel.default(this.idValue, _hellotext.default.session);
47+
this.webChatChannel = new _webchat_channel.default(this.idValue, _hellotext.default.session, this.conversationIdValue);
4848
this.files = [];
4949
this.onMessageReceived = this.onMessageReceived.bind(this);
5050
this.onConversationAssignment = this.onConversationAssignment.bind(this);
@@ -241,7 +241,6 @@ var _default = /*#__PURE__*/function (_Controller) {
241241
} else {
242242
this.onlineStatusTarget.style.display = 'none';
243243
}
244-
this.webChatChannel.updateSubscription();
245244
}
246245
}, {
247246
key: "onAgentOnline",
@@ -288,13 +287,13 @@ var _default = /*#__PURE__*/function (_Controller) {
288287
return element.classList.add('failed');
289288
}
290289
var data = yield response.json();
291-
console.log(data);
292290
element.setAttribute('data-id', data.id);
293291
message.id = data.id;
294292
_hellotext.default.eventEmitter.dispatch('webchat:message:sent', message);
295-
this.conversationIdValue = data.conversation;
296-
console.log(this.conversationIdValue);
297-
this.webChatChannel.subscribe(this.conversationIdValue);
293+
if (data.conversation !== this.conversationIdValue) {
294+
this.conversationIdValue = data.conversation;
295+
this.webChatChannel.subscribe(this.conversationIdValue);
296+
}
298297
});
299298
function sendMessage() {
300299
return _sendMessage.apply(this, arguments);

src/channels/webchat_channel.js

+6-16
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,27 @@
11
import ApplicationChannel from './application_channel'
22

33
class WebchatChannel extends ApplicationChannel {
4-
constructor(id, session) {
4+
constructor(id, session, conversation) {
55
super()
66

77
this.id = id
88
this.session = session
9+
this.conversation = conversation
910

1011
this.subscribe()
1112
}
1213

13-
subscribe(conversation) {
14+
subscribe() {
1415
const params = {
1516
channel: "WebchatChannel",
1617
id: this.id,
1718
session: this.session,
18-
conversation,
19+
conversation: this.conversation,
1920
}
2021

21-
console.log(params)
22-
2322
this.send( { command: 'subscribe', identifier: params })
2423
}
2524

26-
unsubscribe() {
27-
const params = {
28-
channel: "WebchatChannel",
29-
id: this.id,
30-
session: this.session
31-
}
32-
33-
this.send({ command: 'unsubscribe', identifier: params })
34-
}
35-
3625
onMessage(callback) {
3726
super.onMessage((message) => {
3827
if(message.type !== 'message') return
@@ -64,7 +53,8 @@ class WebchatChannel extends ApplicationChannel {
6453
})
6554
}
6655

67-
updateSubscription() {
56+
updateSubscriptionWith(conversation) {
57+
this.conversation = conversation
6858
this.subscribe()
6959
}
7060
}

src/controllers/webchat_controller.js

+5-8
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export default class extends Controller {
4747

4848
initialize() {
4949
this.messagesAPI = new WebchatMessagesAPI(this.idValue)
50-
this.webChatChannel = new WebchatChannel(this.idValue, Hellotext.session)
50+
this.webChatChannel = new WebchatChannel(this.idValue, Hellotext.session, this.conversationIdValue)
5151
this.files = []
5252

5353
this.onMessageReceived = this.onMessageReceived.bind(this)
@@ -261,8 +261,6 @@ export default class extends Controller {
261261
} else {
262262
this.onlineStatusTarget.style.display = 'none'
263263
}
264-
265-
this.webChatChannel.updateSubscription()
266264
}
267265

268266
onAgentOnline() {
@@ -319,16 +317,15 @@ export default class extends Controller {
319317
}
320318

321319
const data = await response.json()
322-
323-
console.log(data)
324320
element.setAttribute('data-id', data.id)
325321
message.id = data.id
326322

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

329-
this.conversationIdValue = data.conversation
330-
console.log(this.conversationIdValue)
331-
this.webChatChannel.subscribe(this.conversationIdValue)
325+
if(data.conversation !== this.conversationIdValue) {
326+
this.conversationIdValue = data.conversation
327+
this.webChatChannel.subscribe(this.conversationIdValue)
328+
}
332329
}
333330

334331
openAttachment() {

0 commit comments

Comments
 (0)