2018-08-30 02:00:09 +00:00
|
|
|
import ActionCable from 'actioncable'
|
|
|
|
|
|
|
|
// create App object with key cable == new cosumer
|
|
|
|
(function() {
|
|
|
|
window.App || (window.App = {});
|
|
|
|
|
|
|
|
App.cable = ActionCable.createConsumer();
|
|
|
|
|
|
|
|
}).call(this);
|
|
|
|
|
|
|
|
// find chatroom id
|
|
|
|
const chatroomId = document.getElementById('chatroom-hook').dataset["chatroomId"]
|
|
|
|
|
|
|
|
// create subsciptions
|
|
|
|
App['chatroom' + chatroomId] = App.cable.subscriptions.create({
|
|
|
|
channel: 'ChatRoomsChannel',
|
|
|
|
room: chatroomId
|
|
|
|
}, {
|
|
|
|
connected: () => {
|
|
|
|
},
|
|
|
|
received: data => {
|
2018-09-04 12:50:44 +00:00
|
|
|
if (data.hangUp) {
|
|
|
|
document.location.pathname = '/contacts'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
disconnected: () => {
|
|
|
|
document.location.pathname = '/contacts'
|
2018-08-30 02:00:09 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2018-09-04 12:50:44 +00:00
|
|
|
const hangUpIcon = document.querySelector('.fa-hand-paper')
|
|
|
|
hangUpIcon.addEventListener('click', event => {
|
|
|
|
fetch(`/chat_rooms/${chatroomId}`, {
|
|
|
|
method: 'DELETE',
|
|
|
|
headers: {
|
|
|
|
'X-CSRF-Token': document.querySelector('meta[name=csrf-token]').content
|
|
|
|
}
|
|
|
|
})
|
|
|
|
document.location.pathname = '/contacts'
|
|
|
|
})
|
|
|
|
|
2018-08-30 02:00:09 +00:00
|
|
|
// Testing ActionCable
|
2018-09-04 10:49:18 +00:00
|
|
|
const testBtn = document.getElementById('test-btn')
|
|
|
|
testBtn.addEventListener('click', event => {
|
|
|
|
fetch(`/chat_rooms/${chatroomId}/cable_testing` , {
|
|
|
|
method: 'POST',
|
|
|
|
body: JSON.stringify({})
|
|
|
|
})
|
|
|
|
})
|
2018-08-30 02:00:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
|