2018-08-30 02:00:09 +00:00
|
|
|
import ActionCable from 'actioncable'
|
2018-09-04 09:29:56 +00:00
|
|
|
// import { triggerCalleeModalEvent } from "./application";
|
|
|
|
|
|
|
|
const triggerCalleeModalEvent = () => {
|
|
|
|
$("#calleeModal").modal('show');
|
|
|
|
}
|
2018-08-30 02:00:09 +00:00
|
|
|
|
2018-09-01 16:36:46 +00:00
|
|
|
// create App object with key cable == new consumer
|
2018-08-30 02:00:09 +00:00
|
|
|
(function() {
|
|
|
|
window.App || (window.App = {});
|
|
|
|
|
|
|
|
App.cable = ActionCable.createConsumer();
|
|
|
|
|
|
|
|
}).call(this);
|
|
|
|
|
|
|
|
|
|
|
|
const userId = parseInt(document.getElementById("my-user-id").dataset["userId"])
|
2018-08-30 13:18:26 +00:00
|
|
|
let chatRoomId = null
|
|
|
|
|
2018-08-30 02:00:09 +00:00
|
|
|
App.cable.subscriptions.create({
|
|
|
|
channel: 'NotificationsChannel'
|
|
|
|
}, {
|
|
|
|
connected: () => {
|
|
|
|
console.log('Connected to NotificationsChannel')
|
|
|
|
},
|
|
|
|
received: data => {
|
2018-08-30 13:18:26 +00:00
|
|
|
console.log("received broadcast")
|
2018-09-03 17:32:03 +00:00
|
|
|
|
2018-08-31 20:24:38 +00:00
|
|
|
if (data.head === 302 && data.body["caller"] === userId && data.path) {
|
2018-08-30 13:18:26 +00:00
|
|
|
window.location.pathname = data.path
|
2018-09-03 17:32:03 +00:00
|
|
|
} else if (data["message"]["user_id"] === userId) {
|
|
|
|
|
|
|
|
// DISPLAY ACCEPT BUTTON
|
2018-08-30 02:00:09 +00:00
|
|
|
const acceptButton = document.getElementById('accept-button')
|
|
|
|
acceptButton.style.display = "block"
|
|
|
|
|
2018-09-03 17:32:03 +00:00
|
|
|
triggerCalleeModalEvent()
|
|
|
|
document.getElementById('caller-name').innerHTML = data["message"]["caller_info"]
|
|
|
|
document.getElementById('caller-photo').src = data["message"]["caller_photo"]
|
|
|
|
|
2018-08-30 13:18:26 +00:00
|
|
|
chatRoomId = data["message"]["chat_room_id"]
|
|
|
|
console.log(`user with id: ${userId} needs to subscribe to chatroom ${[chatRoomId]}`)
|
|
|
|
} else {
|
|
|
|
console.log(data)
|
2018-08-30 02:00:09 +00:00
|
|
|
}
|
2018-08-30 13:18:26 +00:00
|
|
|
|
|
|
|
|
2018-08-30 02:00:09 +00:00
|
|
|
}
|
|
|
|
})
|
2018-08-30 13:18:26 +00:00
|
|
|
|
2018-09-01 16:36:46 +00:00
|
|
|
// Receive information from index.html.erb
|
2018-08-30 13:18:26 +00:00
|
|
|
const acceptButton = document.getElementById('accept-button')
|
|
|
|
|
|
|
|
acceptButton.addEventListener('click', event => {
|
|
|
|
document.getElementById('chat-room-id').value = chatRoomId
|
|
|
|
})
|