diff --git a/app/controllers/flash_controller.rb b/app/controllers/flash_controller.rb index 9f309f8..458f096 100644 --- a/app/controllers/flash_controller.rb +++ b/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 diff --git a/app/javascript/controllers/flash_toast_controller.js b/app/javascript/controllers/flash_toast_controller.js index db34d90..028ecd2 100644 --- a/app/javascript/controllers/flash_toast_controller.js +++ b/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}`) } } diff --git a/app/views/flash/index.html.erb b/app/views/flash/index.html.erb index 8c2e3bc..094db2d 100644 --- a/app/views/flash/index.html.erb +++ b/app/views/flash/index.html.erb @@ -2,15 +2,18 @@