2018-08-30 02:00:09 +00:00
|
|
|
import ActionCable from 'actioncable'
|
2018-09-06 00:58:34 +00:00
|
|
|
|
2018-08-30 02:00:09 +00:00
|
|
|
// 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"]
|
2018-09-04 18:21:25 +00:00
|
|
|
const userId = document.getElementById('current-user').innerText
|
2018-09-05 13:59:39 +00:00
|
|
|
console.log(userId)
|
2018-08-30 02:00:09 +00:00
|
|
|
|
|
|
|
// create subsciptions
|
|
|
|
App['chatroom' + chatroomId] = App.cable.subscriptions.create({
|
2018-09-04 18:33:26 +00:00
|
|
|
channel: 'ChatRoomsChannel',
|
|
|
|
room: chatroomId
|
|
|
|
}, {
|
|
|
|
connected: () => {},
|
|
|
|
received: data => {
|
2018-09-05 13:59:39 +00:00
|
|
|
if (data["chat_message"] && data["chat_message"]["userId"] == userId) {
|
2018-09-04 18:33:26 +00:00
|
|
|
const chatMessage = data["chat_message"]
|
2018-09-05 13:59:39 +00:00
|
|
|
const message = `${chatMessage["message"]}`
|
2018-09-04 18:33:26 +00:00
|
|
|
const messagesContainer = document.getElementById('messages-container')
|
2018-09-05 13:59:39 +00:00
|
|
|
const messageElement = document.createElement("p")
|
|
|
|
messageElement.innerText = message
|
2018-09-06 00:58:34 +00:00
|
|
|
messagesContainer.appendChild(messageElement)
|
|
|
|
}
|
|
|
|
else if (data["chat_message"] && data["chat_message"]["userId"] != userId) {
|
2018-09-05 13:59:39 +00:00
|
|
|
const chatMessage = data["chat_message"]
|
|
|
|
const target = document.getElementById('language-1').value
|
2018-09-06 00:58:34 +00:00
|
|
|
|
2018-09-05 13:59:39 +00:00
|
|
|
const message = `${chatMessage["message"]}`
|
|
|
|
|
|
|
|
fetch(`/chat_rooms/${chatroomId}/translate_message` , {
|
|
|
|
method: 'POST',
|
|
|
|
body: JSON.stringify({
|
|
|
|
message: message,
|
|
|
|
target: target,
|
2018-09-06 00:58:34 +00:00
|
|
|
userId: userId
|
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 00:58:34 +00:00
|
|
|
} else if (data["translation"] && data["userId"] == userId) {
|
|
|
|
if (data["input"] == 1) {
|
|
|
|
document.getElementById('language-2-input').value = data["translation"].text
|
|
|
|
} else {
|
|
|
|
document.getElementById('language-1-input').value = data["translation"].text
|
|
|
|
}
|
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:58:34 +00:00
|
|
|
const message = document.createElement("p")
|
|
|
|
message.innerText = data["translated_message"]
|
|
|
|
messagesContainer.appendChild(message)
|
2018-09-04 18:03:28 +00:00
|
|
|
} else {
|
2018-09-04 18:33:26 +00:00
|
|
|
// console.log(data)
|
2018-09-04 18:03:28 +00:00
|
|
|
}
|
2018-09-04 18:33:26 +00:00
|
|
|
if (data.hangUp) {
|
|
|
|
document.location.pathname = '/contacts'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
disconnected: () => {
|
2018-09-04 12:50:44 +00:00
|
|
|
document.location.pathname = '/contacts'
|
2018-09-04 18:03:28 +00:00
|
|
|
}
|
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 18:03:28 +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
|
|
|
|
2018-09-04 18:03:28 +00:00
|
|
|
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}),
|
2018-09-04 18:03:28 +00:00
|
|
|
headers: { "content-type": "application/json", "X-CSRF-Token": document.querySelector('meta[name=csrf-token]').content }
|
|
|
|
})
|
|
|
|
}
|
|
|
|
chatInput.value = ""
|
|
|
|
})
|
2018-08-30 02:00:09 +00:00
|
|
|
|
|
|
|
|