Skip to content

Commit

Permalink
add the undo logic
Browse files Browse the repository at this point in the history
  • Loading branch information
wuletawwonte committed Sep 19, 2024
1 parent 36eb3bf commit 2352c2b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
19 changes: 16 additions & 3 deletions app/controllers/attendees_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand All @@ -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

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

0 comments on commit 2352c2b

Please sign in to comment.