So I am following this tutorial https://www.freecodecamp.org/news/lets-create-an-intermediate-level-ruby-on-rails-application-d7c6e997c63f/ and although many of the technologies used in it are deprecated or changed, I wanted to give it a try. I am in the part that tries to render a private pop-up conversation window.
When you click on another user's post, you can send them a message and start a new conversation
After you send a message, a pop-up conversation is shown
The **problem ** is that even though the conversation window is added, it is minimized and i can't see its content.
The conversation window's content is loaded inside the html as you can see
For the addition of a conversation window to the app, we use a .js.erb file inside the app/views/private/conversations folder called _open.js.erb .
The code for the _open.js.eb:
var conversation = $('body').find("[data-pconversation-id='" +
"<%= @conversation.id %>" +
"']");
var chat_windows_count = $('.conversation-window').length + 1;
if (conversation.length !== 1) {
$('body').append("<%= j(render 'private/conversations/conversation',\
conversation: @conversation,\
user: current_user) %>");
conversation = $('body').find("[data-conversation-id='" +
"<%= @conversation.id %>" +
"']");
}
// Toggle conversation window after its creation
$('.conversation-window:nth-of-type(' + chat_windows_count + ')\
.conversation-heading').click();
// mark as seen by clicking it
setTimeout(function(){
$('.conversation-window:nth-of-type(' + chat_windows_count + ')').click();
}, 1000);
// focus textarea
$('.conversation-window:nth-of-type(' + chat_windows_count + ')\
form\
textarea').focus();
// repositions all conversation windows
positionChatWindows();
In the javascript directory i have create a file which will take care of the conversations’ windows visibility and positioning.
The file (app/javascript/conversations/position_and_visibility.js):
import "jquery"
$(document).on('turbolinks:load', function() {
console.log("hello from the other side");
chat_windows_count = $('.conversation-window').length;
// if the last visible chat window is not set and conversation windows exist
// set the last_visible_chat_window variable
if (gon.last_visible_chat_window == null && chat_windows_count > 0) {
gon.last_visible_chat_window = chat_windows_count;
}
// if gon.hidden_chats doesn't exist, set its value
if (gon.hidden_chats == null) {
gon.hidden_chats = 0;
}
window.addEventListener('resize', hideShowChatWindow);
positionChatWindows();
hideShowChatWindow();
});
function positionChatWindows() {
chat_windows_count = $('.conversation-window').length;
// if a new conversation window was added,
// set it as the last visible conversation window
// so the hideShowChatWindow function can hide or show it,
// depending on the viewport's width
if (gon.hidden_chats + gon.last_visible_chat_window !== chat_windows_count) {
if (gon.hidden_chats == 0) {
gon.last_visible_chat_window = chat_windows_count;
}
}
// when a new chat window is added, position it to the most left of the list
for (i = 0; i < chat_windows_count; i++ ) {
var right_position = i * 410;
var chat_window = i + 1;
$('.conversation-window:nth-of-type(' + chat_window + ')')
.css('right', '' + right_position + 'px');
}
}
// Hides last conversation window whenever it is close to viewport's left side
function hideShowChatWindow() {
// if there are no conversation windows, stop the function
if ($('.conversation-window').length < 1) {
return;
}
// get an offsset of the most left conversation window
var offset = $('.conversation-window:nth-of-type(' + gon.last_visible_chat_window + ')').offset();
// if the left offset of the conversation window is less than 50,
// hide this conversation window
if (offset.left < 50 && gon.last_visible_chat_window !== 1) {
$('.conversation-window:nth-of-type(' + gon.last_visible_chat_window + ')')
.css('display', 'none');
gon.hidden_chats++;
gon.last_visible_chat_window--;
}
// if the offset of the most left conversation window is more than 550
// and there is a hidden conversation, show the hidden conversation
if (offset.left > 550 && gon.hidden_chats !== 0) {
gon.hidden_chats--;
gon.last_visible_chat_window++;
$('.conversation-window:nth-of-type(' + gon.last_visible_chat_window + ')')
.css('display', 'initial');
}
}
I would like to add that i am using importmaps config/importmap.rb:
# Pin npm packages by running ./bin/importmap
pin "application",preload: true
pin "@hotwired/turbo-rails", to: "turbo.min.js", preload: true
pin "@hotwired/stimulus", to: "stimulus.min.js", preload: true
pin "@hotwired/stimulus-loading", to: "stimulus-loading.js", preload: true
pin "jquery", to: "jquery.min.js", preload: true
pin "jquery_ujs", to: "jquery_ujs.js", preload: true
pin "popper", to: 'popper.js', preload: true
pin "bootstrap", to: 'bootstrap.min.js', preload: true
pin_all_from "app/javascript/controllers", under: "controllers"
pin_all_from "app/javascript/posts", under: "posts"
pin_all_from "app/javascript/conversations", under: "conversations"
pin "@rails/actioncable", to: "actioncable.esm.js"
pin_all_from "app/javascript/channels", under: "channels"
app/javascript/application.js:
import "@hotwired/turbo-rails"
import "controllers"
import "conversations/position_and_visibility"
import "conversations/toggle_window"
import "conversations/conversation"
import "conversations/messages_infinite_scroll"
import "jquery"
import "jquery_ujs"
import "popper"
import "bootstrap"
import "channels"
and also it seems that the js file is imported

If you'd like additional info, please let me know.
