diff --git a/app/assets/stylesheets/pages/_index.scss b/app/assets/stylesheets/pages/_index.scss index 52316dd..96bb455 100644 --- a/app/assets/stylesheets/pages/_index.scss +++ b/app/assets/stylesheets/pages/_index.scss @@ -2,6 +2,7 @@ @import "home"; @import "contacts"; @import "settings"; +@import "messages"; .accept-call-button { border-style: none; diff --git a/app/assets/stylesheets/pages/_messages.scss b/app/assets/stylesheets/pages/_messages.scss new file mode 100644 index 0000000..3f2d4fa --- /dev/null +++ b/app/assets/stylesheets/pages/_messages.scss @@ -0,0 +1,19 @@ +.messages-container-container { + background-color: $card-background; + width: 100%; + height: 100vh; + position: relative; + .chat { + position: absolute; + bottom: 10px; + } +} + +#messages-container { + overflow: scroll; + height: 80%; + padding: 10px; + .message { + color: white; + } +} diff --git a/app/controllers/pages_controller.rb b/app/controllers/pages_controller.rb index 9577412..c1e8bcd 100644 --- a/app/controllers/pages_controller.rb +++ b/app/controllers/pages_controller.rb @@ -39,6 +39,7 @@ class PagesController < ApplicationController chatroom = 'chat_room_' + params[:chat_room_id] puts params user_info = {} + photo_url = current_user.photo.url || "https://upload.wikimedia.org/wikipedia/commons/8/89/Portrait_Placeholder.png" if current_user.first_name.nil? || current_user.last_name.nil? user_info[:name] = current_user.email @@ -50,7 +51,9 @@ class PagesController < ApplicationController chat_message: { message: 'test', user_info: user_info, - time_stamp: Time.now } + time_stamp: Time.now, + photo_url: photo_url + } }) head :ok end diff --git a/app/javascript/packs/chatrooms.js b/app/javascript/packs/chatrooms.js index c5e1267..3a1932a 100644 --- a/app/javascript/packs/chatrooms.js +++ b/app/javascript/packs/chatrooms.js @@ -1,5 +1,5 @@ import ActionCable from 'actioncable' - +// import { scrollLastMessageIntoView } from './chatrooms' // create App object with key cable == new cosumer (function() { window.App || (window.App = {}); @@ -24,11 +24,20 @@ App['chatroom' + chatroomId] = App.cable.subscriptions.create({ const chatMessage = data["chat_message"] const message = `${chatMessage["message"]}` const messagesContainer = document.getElementById('messages-container') + + + const messageDiv = document.createElement("div") + const photo = document.createElement("img") + photo.src = chatMessage["photo_url"] + + // messageDiv.appendChild(photo) + const messageElement = document.createElement("p") + messageElement.classList.add("message") messageElement.innerText = message messagesContainer.appendChild(messageElement) - } - else if (data["chat_message"] && data["chat_message"]["userId"] != userId) { + + } else if (data["chat_message"] && data["chat_message"]["userId"] != userId) { const chatMessage = data["chat_message"] const target = document.getElementById('language-1').value @@ -51,9 +60,11 @@ App['chatroom' + chatroomId] = App.cable.subscriptions.create({ } } else if (data["translated_message"] && data["userId"] == userId) { const messagesContainer = document.getElementById('messages-container') - const message = document.createElement("p") - message.innerText = data["translated_message"] - messagesContainer.appendChild(message) + const messageElement = document.createElement("p") + messageElement.classList.add("message") + messageElement.innerText = data["translated_message"] + messagesContainer.appendChild(messageElement) + scrollLastMessageIntoView(); } else { // console.log(data) } diff --git a/app/javascript/packs/scroll.js b/app/javascript/packs/scroll.js new file mode 100644 index 0000000..9a8f982 --- /dev/null +++ b/app/javascript/packs/scroll.js @@ -0,0 +1,10 @@ +function scrollLastMessageIntoView() { + const messages = document.querySelectorAll('.message'); + const lastMessage = messages[messages.length - 1]; + + if (lastMessage !== undefined) { + lastMessage.scrollIntoView(); + } +} + +export { scrollLastMessageIntoView } diff --git a/app/views/chat_rooms/show.html.erb b/app/views/chat_rooms/show.html.erb index 304a277..a7b7ea0 100644 --- a/app/views/chat_rooms/show.html.erb +++ b/app/views/chat_rooms/show.html.erb @@ -4,6 +4,11 @@
+ +
@@ -13,23 +18,17 @@
-
- -
-
+
- -
+
- -
- -
+ +