Skip to content

Commit

Permalink
Split update and update_all notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
ncounter committed Jun 6, 2024
1 parent 4117ff6 commit 28db08d
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,9 @@
disabled: 'disabled', 'data-disable-with': disable_with_content, value: 'unread')
- if @show_read_all_button
.ms-4
= link_to(button_text(all: true), @update_path, method: :put, remote: true,
class: 'btn btn-sm btn-outline-secondary px-3', 'data-disable-with': disable_with_content(all: true))
- if state != 'read'
= link_to("Mark all as 'Read'", "#{@update_all_path}&button=read", method: :put, remote: true,
class: 'btn btn-sm btn-outline-secondary px-3', 'data-disable-with': disable_with_content(all: true))
- if state != 'unread'
= link_to("Mark all as 'Unread'", "#{@update_all_path}&button=unread", method: :put, remote: true,
class: 'btn btn-sm btn-outline-secondary px-3', 'data-disable-with': disable_with_content(all: true))
14 changes: 3 additions & 11 deletions src/api/app/components/notification_action_bar_component.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# frozen_string_literal: true

class NotificationActionBarComponent < ApplicationComponent
attr_accessor :state, :update_path, :show_read_all_button
attr_accessor :state, :update_all_path, :show_read_all_button

def initialize(state:, update_path:, show_read_all_button: false)
def initialize(state:, update_all_path:, show_read_all_button: false)
super

@state = state
@update_path = add_params(update_path)
@update_all_path = update_all_path
@show_read_all_button = show_read_all_button
end

Expand All @@ -24,12 +24,4 @@ def disable_with_content(all: false)
spinner = tag.i(class: 'fas fa-spinner fa-spin ms-2')
button_text(all: all) + spinner
end

private

def add_params(path)
return "#{path}&update_all=true" if path.include?('?')

"#{path}?update_all=true"
end
end
10 changes: 5 additions & 5 deletions src/api/app/components/notification_mark_button_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ def icon
end

def update_path
my_notifications_path(notification_ids: [@notification.id], kind: @selected_filter[:kind],
state: @selected_filter[:state],
button: @notification.unread? ? 'read' : 'unread',
project: @selected_filter[:project], group: @selected_filter[:group],
page: @page, show_more: @show_more)
update_my_notifications_path(notification_ids: [@notification.id], kind: @selected_filter[:kind],
state: @selected_filter[:state],
button: @notification.unread? ? 'read' : 'unread',
project: @selected_filter[:project], group: @selected_filter[:group],
page: @page, show_more: @show_more)
end
end
46 changes: 32 additions & 14 deletions src/api/app/controllers/webui/users/notifications_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ class Webui::Users::NotificationsController < Webui::WebuiController
before_action :require_login
before_action :set_filter_kind, :set_filter_state, :set_filter_project, :set_filter_group
before_action :set_notifications
before_action :set_notifications_to_be_updated, only: :update
before_action :set_counted_notifications
before_action :filter_notifications, only: :index
before_action :filter_notifications, only: %i[index update_all]
before_action :set_show_read_all_button, only: :index
before_action :set_selected_filter
before_action :paginate_notifications, only: :index
Expand All @@ -20,10 +19,12 @@ def index
end

def update
notification_ids = params[:notification_ids]

# The button value specifies whether we selected read or unread
deliver = params[:button] == 'read'
# rubocop:disable Rails/SkipsModelValidations
@count = @notifications.where(id: @notification_ids, delivered: !deliver).update_all(delivered: deliver)
@count = @notifications.where(id: notification_ids, delivered: !deliver).update_all(delivered: deliver)
# rubocop:enable Rails/SkipsModelValidations

# manually update the count and the filtered subset after the update
Expand All @@ -48,6 +49,34 @@ def update
end
end

def update_all
# The button value specifies whether we selected read or unread
deliver = params[:button] == 'read'
# rubocop:disable Rails/SkipsModelValidations
@count = @notifications.update_all(delivered: deliver)
# rubocop:enable Rails/SkipsModelValidations

# manually update the count and the filtered subset after the update
update_counted_notifications
set_show_read_all_button
paginate_notifications

respond_to do |format|
format.html { redirect_to my_notifications_path }
format.js do
render partial: 'update', locals: {
notifications: @notifications,
unread_notifications: User.session.unread_notifications,
selected_filter: @selected_filter,
counted_notifications: @counted_notifications,
show_read_all_button: @show_read_all_button,
user: User.session
}
end
send_notifications_information_rabbitmq(deliver, @count)
end
end

private

def set_filter_kind
Expand Down Expand Up @@ -101,17 +130,6 @@ def filter_notifications
@notifications = filter_notifications_by_kind(@notifications, @filter_kind)
end

def set_notifications_to_be_updated
@notification_ids = []

if params[:update_all]
filter_notifications
@notification_ids = @notifications.map(&:id)
elsif params[:notification_ids]
@notification_ids = @notifications.where(id: params[:notification_ids]).map(&:id)
end
end

def set_show_read_all_button
@show_read_all_button = @counted_notifications['all'] > Notification::MAX_PER_PAGE
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,16 @@

- else
:ruby
update_path = my_notifications_path(kind: selected_filter[:kind], state: selected_filter[:state],
project: selected_filter[:project], group: selected_filter[:group],
page: params[:page], show_more: params[:show_more])
update_path = update_my_notifications_path(kind: selected_filter[:kind], state: selected_filter[:state],
project: selected_filter[:project], group: selected_filter[:group],
page: params[:page], show_more: params[:show_more])
update_all_path = update_all_my_notifications_path(kind: selected_filter[:kind], state: selected_filter[:state],
project: selected_filter[:project], group: selected_filter[:group],
page: params[:page], show_more: params[:show_more])
= form_tag(update_path, method: :put, remote: true) do
= render(NotificationActionBarComponent.new(state: selected_filter[:state], update_path: update_path, show_read_all_button: show_read_all_button))
= render(NotificationActionBarComponent.new(state: selected_filter[:state],
update_all_path: update_all_path,
show_read_all_button: show_read_all_button))
.card
.card-body
.text-center
Expand Down
3 changes: 2 additions & 1 deletion src/api/config/routes/webui.rb
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,8 @@
resources :notifications, only: [:index], controller: 'webui/users/notifications', as: :my_notifications do
collection do
# We allow updating multiple notifications in a single HTTP request
put :update
put 'update', as: :update
put 'update_all', as: :update_all
end
end

Expand Down

0 comments on commit 28db08d

Please sign in to comment.