kjdelys
posted on
2025-04-08 22:12:06 UTC
<%= turbo_stream_from @conversation %>
import {Controller} from "@hotwired/stimulus";
import {HSOverlay} from "preline/dist/preline";
export default class extends Controller {
static targets = ["content"];
async fetchData(event) {
event.preventDefault(); // Prevent default behavior
// Get the URL to fetch data from
const url = event.currentTarget.href || event.currentTarget.dataset.url;
// Get the offcanvas ID from the button's data attribute
const offcanvasId = event.currentTarget.dataset.offcanvas;
try {
const response = await fetch(url);
const data = await response.text(); // Assuming the response is HTML
// Find the offcanvas content container
const contentTarget = document.querySelector(`#${offcanvasId} [data-overlay-target="content"]`);
// Update the offcanvas content
if (contentTarget) {
contentTarget.innerHTML = data;
}
// Reinitialize Preline components within the new content
window.HSStaticMethods.autoInit();
// Initialize and open the offcanvas
const offcanvasElement = document.getElementById(offcanvasId);
if (offcanvasElement) {
const offcanvasInstance = new HSOverlay(offcanvasElement);
offcanvasInstance.open();
}
} catch (error) {
console.error("Error fetching data:", error);
}
}
}
What does the view look like?
i don't understand the link between the overlay target and the fact that i would like to enable turbo stream in a modal page. Please can you explain?
I would put the turbo_stream_from in the code where it made sense. If the modal is being dynamically rendered (so once you close it or another modal pops up and replaces this chat one) then it would make sense to have the turbo_stream_from inside of the modal so it will be disposed when the modal is replaced or destroyed.
If you want to receive the streams even when the modal is closed or not preset (updating some other parts of the page like an unread message count) then it should exist outside of the modal code.
Hope this makes sense.