Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"Undo" functionality in flash messages (issue #64) #182

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Prev Previous commit
Next Next commit
add the undo logic
wuletawwonte committed Sep 19, 2024
commit 2352c2b6555872ee999376c9d127bcf44e9fc320
19 changes: 16 additions & 3 deletions app/controllers/attendees_controller.rb
Original file line number Diff line number Diff line change
@@ -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

2 changes: 2 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
@@ -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"