created rooms and redirects users to correct room

This commit is contained in:
Beatrice Olivera
2018-08-30 14:18:26 +01:00
parent 405d2cd5df
commit 5245f2257d
6 changed files with 80 additions and 519 deletions

View File

@@ -38,4 +38,35 @@ class PagesController < ApplicationController
})
end
def accept_call
puts "-----------------------------------------"
puts params
puts "IT WORKED"
chat_room = ChatRoom.find(params[:chat_room_id])
request = Request.where("user_id = ? AND chat_room_id = ?", current_user.id, chat_room.id)
request[0].accepted = true
puts "create new chat room participation"
chat_room_participation = ChatRoomParticipation.create!(chat_room: chat_room, user: current_user)
puts "Created chat room participation with user: #{current_user.email} assigned to chat_room #{chat_room.id}"
other_caller = chat_room.users.find { |u| u != current_user } # remember to update this later
puts ">>>>>>>>>>>>>>>>>>>>>>>>>>>>..HHHHHHHHH"
puts other_caller
puts ">>>>>>>>>>>>>>>>>>>>>>>>>>>>..HHHHHHHHH"
# redirect caller to chat room
ActionCable.server.broadcast('notifications', {
head: 302, # redirection code, just to make it clear what you're doing
path: chat_room_path(chat_room), # you'll need to use url_helpers, so include them in your file
body: { caller: other_caller.id }
}
# other_caller, # or however you identify your subscriber
)
# redirect callee to chat room
redirect_to chat_room_path(chat_room)
# broadcast another message to caller
# head: 302
end
end

View File

@@ -1,7 +1,6 @@
class RequestsController < ApplicationController
def update
request.accepted = true
def accept
# Create new Chat Room
end

View File

@@ -10,6 +10,8 @@ import ActionCable from 'actioncable'
const userId = parseInt(document.getElementById("my-user-id").dataset["userId"])
let chatRoomId = null
App.cable.subscriptions.create({
channel: 'NotificationsChannel'
@@ -18,9 +20,13 @@ App.cable.subscriptions.create({
console.log('Connected to NotificationsChannel')
},
received: data => {
console.log(data["message"]["user_id"])
console.log(userId)
if (data["message"]["user_id"] === userId) {
// console.log(data["message"]["user_id"])
// console.log(userId)
console.log("received broadcast")
// console.log(data.body)
if (data.head === 302 && data.body["caller"] === userId && data.path ) {
window.location.pathname = data.path
} else if (data["message"]["user_id"] === userId) {
console.log("TRIGGER MODAL")
const acceptButton = document.getElementById('accept-button')
acceptButton.style.display = "block"
@@ -31,8 +37,20 @@ App.cable.subscriptions.create({
// const calleeModal = document.getElementById('calleeModal')
// calleeModal.modal("show")
console.log(`user with id: ${userId} needs to subscribe to chatroom ${data["message"]["chat_room_id"]}`)
chatRoomId = data["message"]["chat_room_id"]
console.log(`user with id: ${userId} needs to subscribe to chatroom ${[chatRoomId]}`)
} else {
console.log(data)
}
}
})
const acceptButton = document.getElementById('accept-button')
acceptButton.addEventListener('click', event => {
// event.preventDefault()
document.getElementById('chat-room-id').value = chatRoomId
})

View File

@@ -15,6 +15,12 @@
</div>
<div id="my-user-id" data-user-id="<%= current_user.id %>"></div>
<form action="/accept_call" method="post">
<input type="hidden" id="chat-room-id" name="chat_room_id" value=""/>
<input type="submit" id="accept-button" class="btn btn-primary" id="accept-button" style="display: none;" value="ACCEPT CALL"/>
</form>
<%= javascript_pack_tag 'notifications' %>
<button class="btn btn-primary" id="accept-button" style="display: none;">ACCEPT CALL</button>