polyglot/app/javascript/packs/chatrooms.js

178 lines
6.4 KiB
JavaScript
Raw Permalink Normal View History

import ActionCable from 'actioncable'
2018-09-06 00:59:16 +00:00
// import { scrollLastMessageIntoView } from './chatrooms'
// 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"]
const userId = document.getElementById('current-user').innerText
2018-09-05 13:59:39 +00:00
console.log(userId)
// create subsciptions
App['chatroom' + chatroomId] = App.cable.subscriptions.create({
channel: 'ChatRoomsChannel',
room: chatroomId
}, {
connected: () => {},
received: data => {
2018-09-05 13:59:39 +00:00
if (data["chat_message"] && data["chat_message"]["userId"] == userId) {
const chatMessage = data["chat_message"]
2018-09-05 13:59:39 +00:00
const message = `${chatMessage["message"]}`
const messagesContainer = document.getElementById('messages-container')
2018-09-06 00:59:16 +00:00
// Div Settings
const messageDiv = document.createElement("div")
messageDiv.classList.add("messageDiv")
messageDiv.classList.add("right")
// Photo Settings
const photo = document.createElement("img")
photo.src = chatMessage["photo_url"]
photo.classList.add("img-circle")
photo.classList.add("avatar-sm")
photo.classList.add("margin-left")
// Text Settings
2018-09-05 13:59:39 +00:00
const messageElement = document.createElement("p")
2018-09-06 00:59:16 +00:00
messageElement.classList.add("message")
messageElement.classList.add("no-margin")
2018-09-05 13:59:39 +00:00
messageElement.innerText = message
2018-09-06 00:59:16 +00:00
// Add message and photo to div
messageDiv.appendChild(photo)
messageDiv.appendChild(messageElement)
// Add message to container
messagesContainer.appendChild(messageDiv)
2018-09-06 15:13:49 +00:00
messagesContainer.scrollTop = div.scrollHeight
2018-09-06 00:59:16 +00:00
} else if (data["chat_message"] && data["chat_message"]["userId"] != userId) {
2018-09-06 13:17:24 +00:00
// const callerName = document.getElementById('caller-name')
// callerName.innerText = data["chat_message"]["user_info"]
// console.log(data["chat_message"]["user_info"]["name"])
// const photo = data["chat_message"]["photo_url"]
// photo.height = '20px'
// photo.width = '20px'
// const chatInfo = document.querySelector('chat-information')
// chatInfo.insertBefore(photo, chatInfo.firstChild)
2018-09-05 13:59:39 +00:00
const chatMessage = data["chat_message"]
const target = document.getElementById('language-1').value
const message = `${chatMessage["message"]}`
fetch(`/chat_rooms/${chatroomId}/translate_message` , {
method: 'POST',
body: JSON.stringify({
message: message,
target: target,
2018-09-06 00:59:16 +00:00
userId: userId,
photo_url: chatMessage["photo_url"]
2018-09-05 13:59:39 +00:00
}),
headers: { "content-type": "application/json", "X-CSRF-Token": document.querySelector('meta[name=csrf-token]').content }
})
2018-09-06 15:13:49 +00:00
const messagesContainer = document.getElementById('messages-container')
messagesContainer.scrollTop = div.scrollHeight
2018-09-05 13:59:39 +00:00
} else if (data["translated_message"] && data["userId"] == userId) {
const messagesContainer = document.getElementById('messages-container')
2018-09-06 00:59:16 +00:00
const messageDiv = document.createElement("div")
messageDiv.classList.add("messageDiv")
const photo = document.createElement("img")
photo.src = data["photo_url"]
photo.classList.add("img-circle")
photo.classList.add("avatar-sm")
photo.classList.add("margin-right")
const messageElement = document.createElement("p")
messageElement.classList.add("message")
messageElement.classList.add("no-margin")
messageElement.innerText = data["translated_message"]
messageElement.setAttribute('data-original', `${data["original_message"]}`)
messageElement.setAttribute('data-translated', `${data["translated_message"]}`)
2018-09-06 00:59:16 +00:00
messageDiv.appendChild(photo)
messageDiv.appendChild(messageElement)
messageDiv.addEventListener('mouseover', event => {
messageElement.innerText = data["original_message"]
})
messageDiv.addEventListener('mouseout', event => {
messageElement.innerText = data["translated_message"]
})
2018-09-06 00:59:16 +00:00
messagesContainer.appendChild(messageDiv)
2018-09-06 15:13:49 +00:00
messagesContainer.scrollTop = div.scrollHeight
} else {
// console.log(data)
2018-09-06 15:13:49 +00:00
const messagesContainer = document.getElementById('messages-container')
messagesContainer.scrollTop = div.scrollHeight
}
if (data.hangUp) {
document.location.pathname = '/contacts'
}
},
disconnected: () => {
2018-09-04 12:50:44 +00:00
document.location.pathname = '/contacts'
}
})
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'
})
// Testing ActionCable
// const testBtn = document.getElementById('test-btn')
// testBtn.addEventListener('click', event => {
// fetch(`/chat_rooms/${chatroomId}/cable_testing` , {
// method: 'POST',
// body: JSON.stringify({})
// })
// })
2018-09-06 11:45:57 +00:00
// const originalBtn = document.getElementById('original-btn')
// originalBtn.addEventListener('click', event => {
// originalBtn.classList.toggle('original')
// const allMessages = document.querySelectorAll('.messageDiv:not(.right) > p')
// if (originalBtn.classList.contains('original')) {
// originalBtn.innerText = "Show Original"
// allMessages.forEach(message => {
// message.innerText = message.dataset.translated
// })
// } else {
// originalBtn.innerText = "Show Translated"
// allMessages.forEach(message => {
// message.innerText = message.dataset.original
// })
// }
// })
const sendBtn = document.getElementById('send-btn')
sendBtn.addEventListener('click', event => {
const chatInput = document.getElementById('chat-input')
if (chatInput && chatInput.value != chatInput.value.match(/^\s*$/g)) {
fetch(`/chat_rooms/${chatroomId}/send_message` , {
method: 'POST',
2018-09-05 13:59:39 +00:00
body: JSON.stringify({message: chatInput.value, userId: userId}),
headers: { "content-type": "application/json", "X-CSRF-Token": document.querySelector('meta[name=csrf-token]').content }
})
}
chatInput.value = ""
})