diff --git a/app/controllers/notifications/mark_all_reads_controller.rb b/app/controllers/notifications/mark_all_reads_controller.rb new file mode 100644 index 000000000..2aad42028 --- /dev/null +++ b/app/controllers/notifications/mark_all_reads_controller.rb @@ -0,0 +1,5 @@ +class Notifications::MarkAllReadsController < ApplicationController + def create + Current.user.notifications.unread.read_all + end +end diff --git a/app/controllers/readings_controller.rb b/app/controllers/readings_controller.rb index 1bf106efd..fb7112b76 100644 --- a/app/controllers/readings_controller.rb +++ b/app/controllers/readings_controller.rb @@ -8,6 +8,6 @@ class ReadingsController < ApplicationController private def mark_bubble_notifications_read - Current.user.notifications.where(bubble: @bubble).update(read_at: Time.current) + Current.user.notifications.where(bubble: @bubble).read_all end end diff --git a/app/models/notification.rb b/app/models/notification.rb index da1cc9614..05d38c940 100644 --- a/app/models/notification.rb +++ b/app/models/notification.rb @@ -12,6 +12,12 @@ class Notification < ApplicationRecord broadcasts_to ->(notification) { [ notification.user, :notifications ] }, inserts_by: :prepend + class << self + def read_all + update!(read_at: Time.current) + end + end + def read? read_at.present? end diff --git a/app/views/notifications/_tray.html.erb b/app/views/notifications/_tray.html.erb index fa4c650b4..476f0bc78 100644 --- a/app/views/notifications/_tray.html.erb +++ b/app/views/notifications/_tray.html.erb @@ -4,9 +4,9 @@ data: { controller: "dialog", turbo_permanent: true, dialog_modal_value: false, dialog_target: "dialog", action: "keydown.esc->dialog#close:stop click@document->dialog#closeOnClickOutside" } do %> <%= turbo_frame_tag("notifications", src: notifications_tray_path) -%>
- + <% end %> <%= link_to notifications_path, class: "notification-tray__all_action btn flex-item-justify-end" do %> <%= image_tag "bell.svg", aria: { hidden: true }, size: 24 %> diff --git a/app/views/notifications/mark_all_reads/create.html.erb b/app/views/notifications/mark_all_reads/create.html.erb new file mode 100644 index 000000000..52d6bcd10 --- /dev/null +++ b/app/views/notifications/mark_all_reads/create.html.erb @@ -0,0 +1 @@ +<%= turbo_frame_tag "notifications" %> diff --git a/config/routes.rb b/config/routes.rb index 6860a6cf0..bea4b1c2a 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -21,6 +21,7 @@ Rails.application.routes.draw do resources :notifications, only: :index namespace :notifications do resource :tray, only: :show + resource :mark_all_read, only: :create end resources :buckets do diff --git a/test/controllers/notifications/mark_all_reads_controller_test.rb b/test/controllers/notifications/mark_all_reads_controller_test.rb new file mode 100644 index 000000000..ea43e1be6 --- /dev/null +++ b/test/controllers/notifications/mark_all_reads_controller_test.rb @@ -0,0 +1,17 @@ +require "test_helper" + +class Notifications::MarkAllReadsControllerTest < ActionDispatch::IntegrationTest + setup do + sign_in_as :kevin + end + + test "show" do + assert_changes -> { notifications(:logo_published_kevin).reload.read? }, from: false, to: true do + assert_changes -> { notifications(:layout_commented_kevin).reload.read? }, from: false, to: true do + post notifications_mark_all_read_url + end + end + + assert_response :success + end +end