Flashes message with sweetalert2 not working

108 Views Asked by At

I have an issue with my flashes messages, it's worked well but now all my alerts/notices doesn't work except Devise ones

I didn't get errors in console and changed nothing about it And because its work with Devise sign in/logout i don't see how fix it

I tried:

  • Use basic flashes instead of sweetalert2 => still doesn't appear but remove custom
  • Add binding.pry in flashes => it's work and stop there

So i think my flashes are calls but nothing appear at screen when it doesn't from Devise

My flashes:

<% if notice %>
  <div data-controller="flash" data-flash-text-value="<%= notice %>" data-flash-type-value="success"></div>
<% end %>
<% if alert %>
  <div data-controller="flash" data-flash-text-value="<%= alert %>" data-flash-type-value="warning"></div>
<% end %>

my flash_controller.js:

import { Controller } from "@hotwired/stimulus"
import Swal from 'sweetalert2'
window.Swal = Swal;


// Connects to data-controller="flash"
export default class extends Controller {
  static values = {
    text: String,
    type: String,
  }

  connect() {
    const Toast = Swal.mixin({
      toast: true,
      position: 'top-end',
      showConfirmButton: false,
      showCancelButton: true,
      cancelButtonText: 'Fermer',
      timer: 6000,
      timerProgressBar: true,
      didOpen: (toast) => {
        toast.addEventListener('mouseenter', Swal.stopTimer)
        toast.addEventListener('mouseleave', Swal.resumeTimer)
      }
    })

    Toast.fire({
      icon: this.typeValue,
      title: this.textValue,
      background: "#F1EEFD"
    })
  }
}

My application.html.erb

<body>
    <% no_promo = current_user || ['devise/registrations', 'devise/sessions'].include?(params[:controller]) %>
    <% session_show = params[:action] == 'show' && params[:controller] == 'workshop_sessions' %>
    <%= render 'shared/navbar' unless session_show %>
    <%= render 'shared/flashes' unless session_show %>
    <div class="wrapping-container" style="margin-top: 80px;">
      <%= render 'shared/promo' unless no_promo || session_show %>
      <%= yield %>
    </div>
    <%= render 'shared/footer' unless session_show %>

UPDATE:

The issue from all my redirect_to and link_to are call twice

1

There are 1 best solutions below

1
Akeel Ahmed Qureshi On

The same happened to me. when I forgot to write this in the application's layout file

<html>
<body>
 .....

  <%= render 'sweetify/alert' %>
 
</body>
</html>

Then restarting the system works for me.