fixed bug where translations were also broadcasted to other client and fixed bug where the same original and target language caused a 500 error

This commit is contained in:
Beatrice Olivera 2018-09-04 19:21:25 +01:00
parent f4467c65fd
commit b19a000729
3 changed files with 36 additions and 24 deletions

View File

@ -29,7 +29,8 @@ class PagesController < ApplicationController
translation.text.gsub!("&#39;", "'") translation.text.gsub!("&#39;", "'")
ActionCable.server.broadcast "chat_room_#{params[:chat_room_id]}", { ActionCable.server.broadcast "chat_room_#{params[:chat_room_id]}", {
translation: translation, translation: translation,
input: params[:input] input: params[:input],
userId: current_user.id
} }
end end

View File

@ -10,6 +10,7 @@ import ActionCable from 'actioncable'
// find chatroom id // find chatroom id
const chatroomId = document.getElementById('chatroom-hook').dataset["chatroomId"] const chatroomId = document.getElementById('chatroom-hook').dataset["chatroomId"]
const userId = document.getElementById('current-user').innerText
// create subsciptions // create subsciptions
App['chatroom' + chatroomId] = App.cable.subscriptions.create({ App['chatroom' + chatroomId] = App.cable.subscriptions.create({
@ -25,14 +26,14 @@ App['chatroom' + chatroomId] = App.cable.subscriptions.create({
const message = document.createElement("p") const message = document.createElement("p")
message.innerText = `${chatMessage["time_stamp"]} ${chatMessage["user_info"]["name"]}: ${chatMessage["message"]}` message.innerText = `${chatMessage["time_stamp"]} ${chatMessage["user_info"]["name"]}: ${chatMessage["message"]}`
messagesContainer.appendChild(message) messagesContainer.appendChild(message)
} else if (data["translation"]) { } else if (data["translation"] && data["userId"] == userId) {
if (data["input"] == 1) { if (data["input"] == 1) {
document.getElementById('language-2-input').value = data["translation"].text document.getElementById('language-2-input').value = data["translation"].text
} else { } else {
document.getElementById('language-1-input').value = data["translation"].text document.getElementById('language-1-input').value = data["translation"].text
} }
} else { } else {
console.log(data) // console.log(data)
} }
} }
}) })

View File

@ -1,3 +1,4 @@
const userId = document.getElementById('current-user').innerText
const languageForm1 = document.getElementById('lang-1') const languageForm1 = document.getElementById('lang-1')
const languageForm2 = document.getElementById('lang-2') const languageForm2 = document.getElementById('lang-2')
const chatroomId = document.getElementById('chatroom-hook').dataset["chatroomId"] const chatroomId = document.getElementById('chatroom-hook').dataset["chatroomId"]
@ -8,16 +9,21 @@ languageForm1.addEventListener('submit', event => {
const target = document.getElementById('language-2').value const target = document.getElementById('language-2').value
const text = document.getElementById('language-1-input').value const text = document.getElementById('language-1-input').value
if (original !== target) {
fetch(`/chat_rooms/${chatroomId}/translate` , { fetch(`/chat_rooms/${chatroomId}/translate` , {
method: 'POST', method: 'POST',
body: JSON.stringify({ body: JSON.stringify({
original, original,
target, target,
text, text,
input: 1 input: 1,
userId
}), }),
headers: { "content-type": "application/json", "X-CSRF-Token": document.querySelector('meta[name=csrf-token]').content } headers: { "content-type": "application/json", "X-CSRF-Token": document.querySelector('meta[name=csrf-token]').content }
}) })
} else {
document.getElementById('language-2-input').value = text
}
// post request and change form 1 // post request and change form 1
}) })
@ -27,15 +33,19 @@ languageForm2.addEventListener('submit', event => {
const target = document.getElementById('language-1').value const target = document.getElementById('language-1').value
const text = document.getElementById('language-2-input').value const text = document.getElementById('language-2-input').value
// post request and change form 2 // post request and change form 2
if (original !== target) {
fetch(`/chat_rooms/${chatroomId}/translate` , { fetch(`/chat_rooms/${chatroomId}/translate` , {
method: 'POST', method: 'POST',
body: JSON.stringify({ body: JSON.stringify({
original: original, original: original,
target: target, target: target,
text: text, text: text,
input: 2 input: 2,
userId
}), }),
headers: { "content-type": "application/json", "X-CSRF-Token": document.querySelector('meta[name=csrf-token]').content } headers: { "content-type": "application/json", "X-CSRF-Token": document.querySelector('meta[name=csrf-token]').content }
}) })
} else {
document.getElementById('language-1-input').value = text
}
}) })