From a2a6fb04f37c08b2744ce96400e9ebcb32689844 Mon Sep 17 00:00:00 2001 From: Wuletaw Wonte Date: Wed, 18 Sep 2024 17:10:54 +0300 Subject: [PATCH 1/5] add undo button in the flash partial --- app/controllers/attendees_controller.rb | 2 +- app/views/layouts/_flash_message.html.erb | 3 ++- config/routes.rb | 4 +++- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/app/controllers/attendees_controller.rb b/app/controllers/attendees_controller.rb index ff075e42..4671285c 100644 --- a/app/controllers/attendees_controller.rb +++ b/app/controllers/attendees_controller.rb @@ -1,5 +1,5 @@ class AttendeesController < ApplicationController - before_action :set_session + before_action :set_session, only: [:create, :destroy] def create @session.attendees.push(current_user) diff --git a/app/views/layouts/_flash_message.html.erb b/app/views/layouts/_flash_message.html.erb index 76be3926..9f1a05af 100644 --- a/app/views/layouts/_flash_message.html.erb +++ b/app/views/layouts/_flash_message.html.erb @@ -1,7 +1,8 @@ <%# locals: (message:) %>
-
+
<%= message %> + <%= button_tag "Undo", class: "italic", method: :post, url: undo_session_attendee_path(session_id: session.id) %>
diff --git a/config/routes.rb b/config/routes.rb index 798c0ab1..fefaeb21 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -21,7 +21,9 @@ resource :schedule, only: [:show] resources :sessions, only: [:index, :show] do - resource :attendee, only: [:create, :destroy] + resource :attendee, only: [:create, :destroy] do + post "undo", on: :collection + end end resources :speakers, only: [:show] resources :profiles, only: [:show, :edit, :update], param: :uuid From 36eb3bf2b48669f9a50ec6bbc703ed394383e1e3 Mon Sep 17 00:00:00 2001 From: Wuletaw Wonte Date: Thu, 19 Sep 2024 08:52:20 +0300 Subject: [PATCH 2/5] create render notice partial view helper --- app/controllers/attendees_controller.rb | 13 ++++++++++++- app/helpers/application_helper.rb | 9 +++++++++ app/views/layouts/_flash_message.html.erb | 6 ++++-- app/views/layouts/application.html.erb | 6 +----- 4 files changed, 26 insertions(+), 8 deletions(-) diff --git a/app/controllers/attendees_controller.rb b/app/controllers/attendees_controller.rb index 4671285c..457d83e9 100644 --- a/app/controllers/attendees_controller.rb +++ b/app/controllers/attendees_controller.rb @@ -1,10 +1,13 @@ class AttendeesController < ApplicationController - before_action :set_session, only: [:create, :destroy] + before_action :set_session def create @session.attendees.push(current_user) flash[:notice] = I18n.t("controllers.attendees.add_user.notice") + session[:last_action] = {action: "create", user_id: current_user.id, session_id: @session.id} + flash[:undo_path] = undo_session_attendee_path(session_id: @session.id) + redirect_back_or_to(sessions_path, params: params[:starts_at]) end @@ -12,6 +15,14 @@ def destroy @session.attendees.delete(current_user) flash[:notice] = I18n.t("controllers.attendees.remove_user.notice") + session[:last_action] = {action: "destroy", user_id: current_user.id, session_id: @session.id} + flash[:undo_path] = undo_session_attendee_path(session_id: @session.id) + + redirect_back_or_to(sessions_path, params: params[:starts_at]) + end + + def undo + binding.pry redirect_back_or_to(sessions_path, params: params[:starts_at]) end diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index ed6a048d..a0e6904d 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -1,3 +1,12 @@ module ApplicationHelper include Pagy::Frontend + + def render_notice_flash + notice_message = flash[:notice] + undo_path = flash[:undo_path] + + return if notice_message.blank? + + render partial: "layouts/flash_message", locals: {message: notice_message, undo_path: undo_path} + end end diff --git a/app/views/layouts/_flash_message.html.erb b/app/views/layouts/_flash_message.html.erb index 9f1a05af..c51092fa 100644 --- a/app/views/layouts/_flash_message.html.erb +++ b/app/views/layouts/_flash_message.html.erb @@ -1,8 +1,10 @@ -<%# locals: (message:) %> +<%# locals: (message:, undo_path:) %>
<%= message %> - <%= button_tag "Undo", class: "italic", method: :post, url: undo_session_attendee_path(session_id: session.id) %> + <% if undo_path %> + <%= button_to "Undo", undo_path, class: "italic font-bold" %> + <% end %>
diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 69e57924..9ec6b98b 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -39,11 +39,7 @@ <%= render partial: "layouts/header" %> <% end %> - <% flash.each do |type, message| %> - <% if message.present? && message.is_a?(String) %> - <%= render partial: "layouts/flash_message", locals: { message: message } %> - <% end %> - <% end %> + <%= render_notice_flash %> <%= yield %> From 2352c2b6555872ee999376c9d127bcf44e9fc320 Mon Sep 17 00:00:00 2001 From: Wuletaw Wonte Date: Thu, 19 Sep 2024 09:43:23 +0300 Subject: [PATCH 3/5] add the undo logic --- app/controllers/attendees_controller.rb | 19 ++++++++++++++++--- config/locales/en.yml | 2 ++ 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/app/controllers/attendees_controller.rb b/app/controllers/attendees_controller.rb index 457d83e9..fea7bb91 100644 --- a/app/controllers/attendees_controller.rb +++ b/app/controllers/attendees_controller.rb @@ -5,7 +5,7 @@ def create @session.attendees.push(current_user) flash[:notice] = I18n.t("controllers.attendees.add_user.notice") - session[:last_action] = {action: "create", user_id: current_user.id, session_id: @session.id} + session[:last_action] = "create" flash[:undo_path] = undo_session_attendee_path(session_id: @session.id) redirect_back_or_to(sessions_path, params: params[:starts_at]) @@ -15,14 +15,27 @@ def destroy @session.attendees.delete(current_user) flash[:notice] = I18n.t("controllers.attendees.remove_user.notice") - session[:last_action] = {action: "destroy", user_id: current_user.id, session_id: @session.id} + session[:last_action] = "destroy" flash[:undo_path] = undo_session_attendee_path(session_id: @session.id) redirect_back_or_to(sessions_path, params: params[:starts_at]) end def undo - binding.pry + if session[:last_action].present? + last_action = session[:last_action] + + if last_action == "create" + @session.attendees.delete(current_user) + elsif last_action == "destroy" + @session.attendees.push(current_user) + end + + session[:last_action] = nil + else + flash[:notice] = I18n.t("controllers.attendees.undo.expired") + end + redirect_back_or_to(sessions_path, params: params[:starts_at]) end diff --git a/config/locales/en.yml b/config/locales/en.yml index ae8f29bd..019a974f 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -29,6 +29,8 @@ en: notice: "Session was added to My Schedule" remove_user: notice: "Session was removed from My Schedule" + undo: + expired: "The undo action has expired." views: status_filters: past: "Past" From 844ab75b634d3fb89fcd0a58a482a5795248bf78 Mon Sep 17 00:00:00 2001 From: Wuletaw Wonte Date: Thu, 19 Sep 2024 12:03:46 +0300 Subject: [PATCH 4/5] revert adding view helper --- app/controllers/attendees_controller.rb | 4 ++-- app/helpers/application_helper.rb | 9 --------- app/views/layouts/_flash_message.html.erb | 6 +++--- app/views/layouts/application.html.erb | 6 +++++- 4 files changed, 10 insertions(+), 15 deletions(-) diff --git a/app/controllers/attendees_controller.rb b/app/controllers/attendees_controller.rb index fea7bb91..25197ebe 100644 --- a/app/controllers/attendees_controller.rb +++ b/app/controllers/attendees_controller.rb @@ -6,7 +6,7 @@ def create flash[:notice] = I18n.t("controllers.attendees.add_user.notice") session[:last_action] = "create" - flash[:undo_path] = undo_session_attendee_path(session_id: @session.id) + flash[:undo] = {path: undo_session_attendee_path(session_id: @session.id)} redirect_back_or_to(sessions_path, params: params[:starts_at]) end @@ -16,7 +16,7 @@ def destroy flash[:notice] = I18n.t("controllers.attendees.remove_user.notice") session[:last_action] = "destroy" - flash[:undo_path] = undo_session_attendee_path(session_id: @session.id) + flash[:undo] = {path: undo_session_attendee_path(session_id: @session.id)} redirect_back_or_to(sessions_path, params: params[:starts_at]) end diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index a0e6904d..ed6a048d 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -1,12 +1,3 @@ module ApplicationHelper include Pagy::Frontend - - def render_notice_flash - notice_message = flash[:notice] - undo_path = flash[:undo_path] - - return if notice_message.blank? - - render partial: "layouts/flash_message", locals: {message: notice_message, undo_path: undo_path} - end end diff --git a/app/views/layouts/_flash_message.html.erb b/app/views/layouts/_flash_message.html.erb index c51092fa..edfa7901 100644 --- a/app/views/layouts/_flash_message.html.erb +++ b/app/views/layouts/_flash_message.html.erb @@ -1,10 +1,10 @@ -<%# locals: (message:, undo_path:) %> +<%# locals: (message:, type:) %>
<%= message %> - <% if undo_path %> - <%= button_to "Undo", undo_path, class: "italic font-bold" %> + <% if type == "notice" && flash[:undo].present? %> + <%= button_to "Undo", flash[:undo]["path"], class: "italic font-bold" %> <% end %>
diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 17c65dba..2f1d140c 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -46,7 +46,11 @@
- <%= render_notice_flash %> + <% flash.each do |type, message| %> + <% if message.present? && message.is_a?(String) %> + <%= render partial: "layouts/flash_message", locals: { message: message, type: type } %> + <% end %> + <% end %>
<%= yield %> From 75f824f231205948d9bf6b0326d76b8fb693d45d Mon Sep 17 00:00:00 2001 From: Wuletaw Wonte Date: Fri, 20 Sep 2024 17:28:40 +0300 Subject: [PATCH 5/5] fix: linter error --- app/views/layouts/_flash_message.html.erb | 1 - 1 file changed, 1 deletion(-) diff --git a/app/views/layouts/_flash_message.html.erb b/app/views/layouts/_flash_message.html.erb index 11685998..9e09a98d 100644 --- a/app/views/layouts/_flash_message.html.erb +++ b/app/views/layouts/_flash_message.html.erb @@ -1,6 +1,5 @@ <%# locals: (message:, type:) %> -
<%= message %>