Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support warnings with Turbo Streams/Frames #725

Open
wants to merge 9 commits into
base: main
Choose a base branch
from

Conversation

tpaulshippy
Copy link

Resolves #724

Purpose

To provide bullet warnings in rails applications that use Turbo Streams and Turbo Frames.

Approach

Use the rack middleware to detect if a request is related to Turbo and inject the Bullet warnings appropriately.

Note

This does not add support for the javascript alert(). A quick and dirty hack to make the alert() show up is to add script like this to to the <head> of my application.html.erb (although the messages aren't quite as nice looking as the alerts from uniform_notifier):

    <% if Rails.env.development? %>
    <script>
      function alertIfBulletWarning(bulletFooterDiv) {
        if (bulletFooterDiv) {
            const textWithoutSpan = Array.from(bulletFooterDiv.childNodes)
  .filter(node => node.nodeType === Node.TEXT_NODE)
  .map(node => node.textContent.trim())
  .join(' ');
            alert(textWithoutSpan);
          }
      }
      document.addEventListener("turbo:frame-render", function(event) {
        event.detail.fetchResponse.responseHTML.then(html => {
          const bulletFooterDiv = document.querySelector('details#bullet-footer > div');
          alertIfBulletWarning(bulletFooterDiv);          
        });
      });

      document.addEventListener("turbo:before-stream-render", function(event) {
        const template = event.detail.newStream.querySelector("template");
        const templateContent = template.content;
        const bulletFooterDiv = templateContent.querySelector("details#bullet-footer > div");
        alertIfBulletWarning(bulletFooterDiv);
      });
    </script>
    <% end %>
image

@tpaulshippy
Copy link
Author

Found an issue with frames. It is now fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Not being notified when N+1 is detected in turbo frame or turbo stream
1 participant