Browse Source

reminiscicent

main
pvincent 2 months ago
parent
commit
0b812ac122
  1. 22
      app/controllers/flash_controller.rb
  2. 14
      app/javascript/controllers/flash_toast_controller.js
  3. 11
      app/views/flash/index.html.erb
  4. 5
      config/routes.rb

22
app/controllers/flash_controller.rb

@ -5,15 +5,25 @@ class FlashController < ApplicationController
def redirect_with_notice = redirect_to(flash_path, notice:)
def redirect_with_alert = redirect_to(flash_path, alert:)
def render_notice
flash[:notice] = 'From Render'
flash[:alert] = 'Alert Render'
def redirect_with_both
flash[:notice] = notice
flash[:alert] = alert
redirect_to flash_path
end
def from_render
session[:flash_iteration] = session[:flash_iteration].to_i + 1
flash.now[:notice] = "From Render #{session[:flash_iteration]}"
flash.now[:alert] = "Alert Render #{session[:flash_iteration]}"
logger.info(flash.object_id)
render :index
end
def turbo_notice
flash[:notice] = 'From Turbo'
flash[:alert] = 'Alert Turbo'
def from_turbo
flash.now[:notice] = 'From Turbo'
flash.now[:alert] = 'Alert Turbo'
render turbo_stream: flash_stream
end

14
app/javascript/controllers/flash_toast_controller.js

@ -15,7 +15,7 @@ export default class extends Controller {
const commonOptions = { gravity: 'top', position: 'center', duration: 4000, offset: { y: '1em' }, close: true, escapeMarkup: false }
this.toastifyNotice = Toastify({ className: 'toastify-notice', ...commonOptions })
this.toastifyAlert = Toastify({ className: 'toastify-alert', ...commonOptions })
this.lastId = 0
this.idQueue = Array(5)
}
initialize() {
@ -23,14 +23,16 @@ export default class extends Controller {
}
connect() {
const id = this.element.dataset.id
if (id != this.constructor.lastId) { // test whether duplicates
this.constructor.lastId = id
const id = Number(this.element.dataset.id)
const queue = this.constructor.idQueue
if (!this.constructor.idQueue.includes(id)) { // test whether duplicates from the 5 latest items
queue.splice(0, 0, id)
queue.splice(5)
this.clearChildrenElement()
this.showMessage(this.constructor.toastifyNotice, this.element.dataset.notice)
this.showMessage(this.constructor.toastifyAlert, this.element.dataset.alert)
}
// else console.warn(`reminiscient id <${id}> from queue <${queue}`)
}
showMessage(toastify, message) {
@ -40,7 +42,7 @@ export default class extends Controller {
toastify.showToast()
const lastClassNameWord = toastify.options.className.split('-').pop()
console.info(`TOAST ${lastClassNameWord}: ${message}`)
console.info(`TOAST#${this.constructor.idQueue[0]} ${lastClassNameWord}: ${message}`)
}
}

11
app/views/flash/index.html.erb

@ -2,15 +2,18 @@
<ul>
<li>
<%= link_to 'redirect with flash notice', flash_redirect_with_notice_path, class: :button %>
<%= link_to 'redirect with notice', flash_redirect_with_notice_path, class: :button %>
</li>
<li>
<%= link_to 'redirect with flash alert', flash_redirect_with_alert_path, class: :button %>
<%= link_to 'redirect with alert', flash_redirect_with_alert_path, class: :button %>
</li>
<li>
<%= link_to 'render notice', flash_render_notice_path, class: :button %>
<%= link_to 'redirect with both', flash_redirect_with_both_path, class: :button %>
</li>
<li>
<%= link_to 'turbo notice', flash_turbo_notice_path, class: :button, 'data-turbo-method': :post %>
<%= link_to 'from render', flash_from_render_path, class: :button %>
</li>
<li>
<%= link_to 'from turbo', flash_from_turbo_path, class: :button, 'data-turbo-method': :post %>
</li>
</ul>

5
config/routes.rb

@ -10,8 +10,9 @@ Rails.application.routes.draw do
root 'scores#index'
get 'flash' => 'flash#index'
get 'flash/render_notice' => 'flash#render_notice', as: :flash_render_notice
get 'flash/redirect_with_notice' => 'flash#redirect_with_notice', as: :flash_redirect_with_notice
get 'flash/redirect_with_alert' => 'flash#redirect_with_alert', as: :flash_redirect_with_alert
post 'flash/turbo_notice' => 'flash#turbo_notice', as: :flash_turbo_notice
get 'flash/redirect_with_both' => 'flash#redirect_with_both', as: :flash_redirect_with_both
get 'flash/from_render' => 'flash#from_render', as: :flash_from_render
post 'flash/from_turbo' => 'flash#from_turbo', as: :flash_from_turbo
end
Loading…
Cancel
Save