polyglot/app/javascript/packs/application.js

106 lines
2.8 KiB
JavaScript
Raw Permalink Normal View History

import "bootstrap";
2018-09-04 10:23:45 +00:00
import ActionCable from 'actioncable'
2018-08-31 09:17:26 +00:00
import { triggerModalEvent } from "../components/modal.js";
triggerModalEvent();
2018-09-01 17:27:12 +00:00
const settingsPage = document.getElementById('settings-page');
const contactsPage = document.getElementById('contacts-page');
2018-09-01 17:27:12 +00:00
const getSiblings = (element) => {
const siblings = [];
2018-09-01 17:27:12 +00:00
let sibling = element.parentNode.firstChild;
const skipMe = element;
for ( ; sibling; sibling = sibling.nextSibling )
2018-09-01 17:27:12 +00:00
if ( sibling.nodeType == 1 && sibling != element )
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()
2018-09-06 13:07:15 +00:00
const photo = document.getElementById('caller-photo');
2018-09-04 10:23:45 +00:00
document.getElementById('caller-name').innerHTML = data["message"]["caller_info"]
2018-09-06 13:07:15 +00:00
photo.src = data["message"]["caller_photo"];
photo.style.objectFit = "cover";
2018-09-04 10:23:45 +00:00
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
})
}