Wire up notifications "Mark all as read" button

This commit is contained in:
Kevin McConnell
2025-01-21 13:56:53 +00:00
parent 404c420a58
commit 5d5dc8fb73
7 changed files with 33 additions and 3 deletions
@@ -0,0 +1,5 @@
class Notifications::MarkAllReadsController < ApplicationController
def create
Current.user.notifications.unread.read_all
end
end
+1 -1
View File
@@ -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
+6
View File
@@ -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
+2 -2
View File
@@ -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) -%>
<div class="notification-tray__actions flex align-center gap-half">
<button class="notification-tray__read_action btn flex-item-justify-start">
<%= button_to notifications_mark_all_read_path, class: "notification-tray__read_action btn flex-item-justify-start", data: { turbo_frame: "notifications" } do %>
Mark all as read
</button>
<% 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 %>
@@ -0,0 +1 @@
<%= turbo_frame_tag "notifications" %>
+1
View File
@@ -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
@@ -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