From 0b812ac1227305e06a33969b452b40f553c8ebf8 Mon Sep 17 00:00:00 2001 From: pvincent Date: Wed, 10 Jul 2024 05:07:45 +0400 Subject: [PATCH] reminiscicent --- app/controllers/flash_controller.rb | 22 ++++++++++++++----- .../controllers/flash_toast_controller.js | 14 +++++++----- app/views/flash/index.html.erb | 11 ++++++---- config/routes.rb | 5 +++-- 4 files changed, 34 insertions(+), 18 deletions(-) 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 @@ \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index e26e58e..8387cd5 100644 --- a/config/routes.rb +++ b/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