2018-08-27 14:37:06 +00:00
|
|
|
import "bootstrap";
|
2018-09-04 10:23:45 +00:00
|
|
|
import ActionCable from 'actioncable'
|
2018-08-31 09:17:26 +00:00
|
|
|
|
2018-08-31 10:55:03 +00:00
|
|
|
import { triggerModalEvent } from "../components/modal.js";
|
|
|
|
|
2018-09-04 10:52:10 +00:00
|
|
|
import { loadDynamicBannerText } from '../components/banner';
|
|
|
|
loadDynamicBannerText();
|
|
|
|
|
2018-08-31 10:55:03 +00:00
|
|
|
triggerModalEvent();
|
|
|
|
|
2018-09-01 17:27:12 +00:00
|
|
|
const settingsPage = document.getElementById('settings-page');
|
|
|
|
const contactsPage = document.getElementById('contacts-page');
|
2018-08-31 10:55:03 +00:00
|
|
|
|
2018-09-01 17:27:12 +00:00
|
|
|
const getSiblings = (element) => {
|
2018-09-01 16:36:46 +00:00
|
|
|
const siblings = [];
|
2018-09-01 17:27:12 +00:00
|
|
|
let sibling = element.parentNode.firstChild;
|
|
|
|
const skipMe = element;
|
2018-09-01 16:36:46 +00:00
|
|
|
for ( ; sibling; sibling = sibling.nextSibling )
|
2018-09-01 17:27:12 +00:00
|
|
|
if ( sibling.nodeType == 1 && sibling != element )
|
2018-09-01 16:36:46 +00:00
|
|
|
siblings.push( sibling );
|
|
|
|
return siblings;
|
|
|
|
}
|
|
|
|
|
2018-09-01 17:27:12 +00:00
|
|
|
const removeActiveClass = (element) => {
|
|
|
|
const siblings = getSiblings(element);
|
|
|
|
siblings.forEach(sibling => {
|
|
|
|
sibling.classList.remove('active');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (settingsPage) {
|
|
|
|
const settings = document.getElementById('settings')
|
|
|
|
settings.classList.add('active');
|
|
|
|
removeActiveClass(settings);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (contactsPage) {
|
|
|
|
const contacts = document.getElementById('contacts')
|
|
|
|
contacts.classList.add('active');
|
|
|
|
removeActiveClass(contacts);
|
|
|
|
}
|
|
|
|
|
2018-09-04 10:23:45 +00:00
|
|
|
// const triggerCalleeModalEvent = () => {
|
|
|
|
// $("#calleeModal").modal('show');
|
|
|
|
// }
|
|
|
|
|
|
|
|
// export { triggerCalleeModalEvent }
|
2018-09-03 17:32:03 +00:00
|
|
|
const triggerCalleeModalEvent = () => {
|
|
|
|
$("#calleeModal").modal('show');
|
|
|
|
}
|
|
|
|
|
2018-09-04 10:23:45 +00:00
|
|
|
// create App object with key cable == new consumer
|
|
|
|
(function() {
|
|
|
|
window.App || (window.App = {});
|
|
|
|
|
|
|
|
App.cable = ActionCable.createConsumer();
|
|
|
|
|
|
|
|
}).call(this);
|
|
|
|
|
|
|
|
|
|
|
|
const userIdElement = document.getElementById("my-user-id")
|
|
|
|
let userId = null
|
|
|
|
if (userIdElement) {
|
|
|
|
userId = parseInt(document.getElementById("my-user-id").dataset["userId"])
|
|
|
|
}
|
|
|
|
|
|
|
|
let chatRoomId = null
|
|
|
|
|
|
|
|
App.cable.subscriptions.create({
|
|
|
|
channel: 'NotificationsChannel'
|
|
|
|
}, {
|
|
|
|
connected: () => {
|
|
|
|
console.log('Connected to NotificationsChannel')
|
|
|
|
},
|
|
|
|
received: data => {
|
|
|
|
console.log("received broadcast")
|
|
|
|
|
|
|
|
if (data.head === 302 && data.body["caller"] === userId && data.path) {
|
|
|
|
window.location.pathname = data.path
|
|
|
|
} else if (data["message"]["user_id"] === userId) {
|
|
|
|
|
|
|
|
// DISPLAY ACCEPT BUTTON
|
|
|
|
const acceptButton = document.getElementById('accept-button')
|
|
|
|
acceptButton.style.display = "block"
|
|
|
|
|
|
|
|
triggerCalleeModalEvent()
|
|
|
|
document.getElementById('caller-name').innerHTML = data["message"]["caller_info"]
|
|
|
|
document.getElementById('caller-photo').src = data["message"]["caller_photo"]
|
|
|
|
|
|
|
|
chatRoomId = data["message"]["chat_room_id"]
|
|
|
|
console.log(`user with id: ${userId} needs to subscribe to chatroom ${[chatRoomId]}`)
|
|
|
|
} else {
|
|
|
|
console.log(data)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
// Receive information from index.html.erb
|
|
|
|
const acceptButton = document.getElementById('accept-button')
|
|
|
|
|
|
|
|
if (acceptButton) {
|
|
|
|
acceptButton.addEventListener('click', event => {
|
|
|
|
document.getElementById('chat-room-id').value = chatRoomId
|
|
|
|
})
|
|
|
|
}
|