diff --git a/app/controllers/cards/readings_controller.rb b/app/controllers/cards/readings_controller.rb index 669d959d8..bbc4d53c0 100644 --- a/app/controllers/cards/readings_controller.rb +++ b/app/controllers/cards/readings_controller.rb @@ -2,12 +2,7 @@ class Cards::ReadingsController < ApplicationController include CardScoped def create - mark_card_notifications_read - @notifications = Current.user.notifications.unread.ordered.limit(20) + @notification = Current.user.notifications.find_by(card: @card) + @notification.read end - - private - def mark_card_notifications_read - Current.user.notifications.unread.where(card: @card).read_all - end end diff --git a/app/controllers/notifications/mark_all_reads_controller.rb b/app/controllers/notifications/mark_all_reads_controller.rb deleted file mode 100644 index 8b6450515..000000000 --- a/app/controllers/notifications/mark_all_reads_controller.rb +++ /dev/null @@ -1,18 +0,0 @@ -class Notifications::MarkAllReadsController < ApplicationController - def create - Current.user.notifications.unread.read_all - - set_page_and_extract_portion_from Current.user.notifications.read.ordered if request.format.turbo_stream? - - Turbo::StreamsChannel.broadcast_update_to( - [ Current.user, :notifications ], - target: "notifications", - html: "" - ) - - respond_to do |format| - format.turbo_stream - format.html - end - end -end diff --git a/app/controllers/notifications/readings_controller.rb b/app/controllers/notifications/readings_controller.rb new file mode 100644 index 000000000..5f593fa95 --- /dev/null +++ b/app/controllers/notifications/readings_controller.rb @@ -0,0 +1,11 @@ +class Notifications::ReadingsController < ApplicationController + def create + @notification = Current.user.notifications.find(params[:id]) + @notification.read + end + + def create_all + Current.user.notifications.unread.read_all + redirect_to notifications_path + end +end diff --git a/app/controllers/notifications_controller.rb b/app/controllers/notifications_controller.rb index 403828c2b..d188e3a1e 100644 --- a/app/controllers/notifications_controller.rb +++ b/app/controllers/notifications_controller.rb @@ -1,19 +1,11 @@ class NotificationsController < ApplicationController def index + @unread = Current.user.notifications.unread.ordered unless current_page_param set_page_and_extract_portion_from Current.user.notifications.read.ordered - if @page.first? - @unread = Current.user.notifications.unread.ordered - end - end - - def mark_read - @notification = Current.user.notifications.find(params[:id]) - @notification.update!(read_at: Time.current) - respond_to do |format| - format.html { redirect_back fallback_location: notifications_path } - format.turbo_stream + format.turbo_stream if current_page_param # Allows read-all action to side step pagination + format.html end end end diff --git a/app/helpers/notifications_helper.rb b/app/helpers/notifications_helper.rb index ca62626cf..656edad54 100644 --- a/app/helpers/notifications_helper.rb +++ b/app/helpers/notifications_helper.rb @@ -32,7 +32,7 @@ module NotificationsHelper end def notification_mark_read_button(notification) - button_to mark_read_notification_path(notification), + button_to read_notification_path(notification), class: "notification__unread_indicator btn borderless", title: "Mark as read", data: { turbo_frame: "_top" } do diff --git a/app/models/notification.rb b/app/models/notification.rb index 3980c3d8d..9fbb94a5b 100644 --- a/app/models/notification.rb +++ b/app/models/notification.rb @@ -9,16 +9,22 @@ class Notification < ApplicationRecord scope :ordered, -> { order(read_at: :desc, created_at: :desc) } delegate :creator, to: :event + after_create_commit :broadcast_unread - broadcasts_to ->(notification) { [ notification.user, :notifications ] }, inserts_by: :prepend + def self.read_all + update!(read_at: Time.current) + end - class << self - def read_all - update!(read_at: Time.current) - end + def read + update!(read_at: Time.current) end def read? read_at.present? end + + private + def broadcast_unread + broadcast_prepend_later_to user, :notifications, target: "notifications" + end end diff --git a/app/views/cards/readings/create.turbo_stream.erb b/app/views/cards/readings/create.turbo_stream.erb index 8f758752e..1f3b07b47 100644 --- a/app/views/cards/readings/create.turbo_stream.erb +++ b/app/views/cards/readings/create.turbo_stream.erb @@ -1,3 +1 @@ -<%= turbo_stream.update "notifications" do %> - <%= render collection: @notifications, partial: "notifications/notification" %> -<% end %> +<%= turbo_stream.remove @notification %> diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index e886c519f..fd857fafe 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -46,10 +46,12 @@ diff --git a/app/views/notifications/_tray.html.erb b/app/views/notifications/_tray.html.erb index 8e12e58f4..9095f1946 100644 --- a/app/views/notifications/_tray.html.erb +++ b/app/views/notifications/_tray.html.erb @@ -1,14 +1,11 @@ <%= turbo_stream_from Current.user, :notifications %> -<%= tag.dialog id: "notification-tray", class: "tray notification-tray", - 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) -%> + + <%= turbo_frame_tag "notifications", src: tray_notifications_path %> - <%= link_to notifications_path, class: "tray__overflow border-radius txt-normal", data: { action: "click->dialog#close"} do %> + <%= link_to notifications_path, class: "tray__overflow border-radius txt-normal", data: { action: "click->dialog#close" } do %>
+ More… @@ -17,22 +14,18 @@ <% end %>
- <%= button_to notifications_mark_all_read_path, - class: "notification-tray__read_action btn btn--reversed fill-transparent borderless flex-item-justify-start shadow txt-xx-small", - data: { action: "click->dialog#close", turbo_frame: "notifications" } do %> - <%= icon_tag "check" %> - Mark all read + <%= button_to read_all_notifications_path, + class: "notification-tray__read_action btn btn--reversed fill-transparent borderless flex-item-justify-start shadow txt-xx-small", + data: { action: "click->dialog#close", turbo_frame: "notifications" } do %> + <%= icon_tag "check" %> Mark all read <% end %> - <%= link_to notifications_path, - class: "btn btn--reversed borderless fill-transparent translucent shadow center txt-uppercase txt-x-small", - data: { action: "click->dialog#close"} do %> - Notifications - <% end %> + <%= link_to tag.span("Notifications"), notifications_path, + class: "btn btn--reversed borderless fill-transparent translucent shadow center txt-uppercase txt-x-small", + data: { action: "click->dialog#close" } %>
- -<% end %> + +
diff --git a/app/views/notifications/index.html.erb b/app/views/notifications/index.html.erb index bab485363..bf83657ee 100644 --- a/app/views/notifications/index.html.erb +++ b/app/views/notifications/index.html.erb @@ -1,4 +1,5 @@ <% @page_title = "Notifications" %> +<% @hide_footer_frames = true %> <% content_for :header do %> <% end %>
- <%= turbo_frame_tag "notifications_list" do %> - <% if @unread.any? %> -
-

New for you

- <%= button_to notifications_mark_all_read_path, class: "btn txt-small" do %> - Mark all as read - <% end %> -
+ <% if @unread.any? %> +
+

New for you

+ <%= button_to "Mark all as read", read_all_notifications_path, class: "btn txt-small" %> +
- <%= render @unread %> - <% else %> -
- Nothing new for you. -
- <% end %> + <%= render partial: "notifications/notification", collection: @unread, cached: true %> + <% else %> + <%# FIXME: Need to reveal this section if there are unread notification records in it %> +
+ Nothing new for you. +
<% end %>
-<% if @page.records.any? %> -
-

Previously seen

- <%= render @page.records %> -
+<%# FIXME: Need to hide this section if there are no notification records in it, but then it needs to appear when any are added %> +
+

Previously seen

- <%= notifications_next_page_link(@page) %> -<% end %> +
+ <%= render partial: "notifications/notification", collection: @page.records, cached: true %> +
+
+ +<%= notifications_next_page_link(@page) if @page.records.any? %> diff --git a/app/views/notifications/index.turbo_stream.erb b/app/views/notifications/index.turbo_stream.erb index 00d744dcd..4d74e0941 100644 --- a/app/views/notifications/index.turbo_stream.erb +++ b/app/views/notifications/index.turbo_stream.erb @@ -1,7 +1,2 @@ -<%= turbo_stream.append :notifications_list_read do %> - <%= render @page.records %> -<% end %> - -<%= turbo_stream.replace :next_page do %> - <%= notifications_next_page_link(@page) %> -<% end %> +<%= turbo_stream.append :notifications_list_read, partial: "notifications/notification", collection: @page.records %> +<%= turbo_stream.replace :next_page, notifications_next_page_link(@page) %> diff --git a/app/views/notifications/mark_all_reads/create.html.erb b/app/views/notifications/mark_all_reads/create.html.erb deleted file mode 100644 index 52d6bcd10..000000000 --- a/app/views/notifications/mark_all_reads/create.html.erb +++ /dev/null @@ -1 +0,0 @@ -<%= turbo_frame_tag "notifications" %> diff --git a/app/views/notifications/mark_all_reads/create.turbo_stream.erb b/app/views/notifications/mark_all_reads/create.turbo_stream.erb deleted file mode 100644 index 6d8b0f0a6..000000000 --- a/app/views/notifications/mark_all_reads/create.turbo_stream.erb +++ /dev/null @@ -1,14 +0,0 @@ -<%= turbo_stream.update "notifications_list" do %> -
- Nothing new for you. -
-<% end %> - -<% if @page&.records&.any? %> - <%= turbo_stream.update "notifications_list_read" do %> -

Previously seen

- <%= render partial: "notifications/notification", collection: @page.records %> - <% end %> -<% end %> - -<%= turbo_stream.update "notifications" %> \ No newline at end of file diff --git a/app/views/notifications/mark_read.turbo_stream.erb b/app/views/notifications/mark_read.turbo_stream.erb deleted file mode 100644 index 78996e2d3..000000000 --- a/app/views/notifications/mark_read.turbo_stream.erb +++ /dev/null @@ -1,9 +0,0 @@ -<%= turbo_stream.remove @notification %> - -<% if Current.user.notifications.unread.none? %> - <%= turbo_stream.update "notifications_list" do %> -
- Nothing new for you. -
- <% end %> -<% end %> \ No newline at end of file diff --git a/app/views/notifications/readings/create.turbo_stream.erb b/app/views/notifications/readings/create.turbo_stream.erb new file mode 100644 index 000000000..44bf1e545 --- /dev/null +++ b/app/views/notifications/readings/create.turbo_stream.erb @@ -0,0 +1,2 @@ +<%= turbo_stream.remove @notification %> +<%= turbo_stream.prepend :notifications_list_read, partial: "notifications/notification", locals: { notification: @notification } %> diff --git a/app/views/notifications/trays/show.html.erb b/app/views/notifications/trays/show.html.erb index 051055670..f61c52d51 100644 --- a/app/views/notifications/trays/show.html.erb +++ b/app/views/notifications/trays/show.html.erb @@ -1,3 +1,3 @@ <%= turbo_frame_tag "notifications" do %> - <%= render collection: @notifications, partial: "notifications/notification" %> + <%= render partial: "notifications/notification", collection: @notifications, cached: true %> <% end %> diff --git a/config/routes.rb b/config/routes.rb index 6eb05dd50..61271338a 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -46,17 +46,13 @@ Rails.application.routes.draw do end - resources :notifications, only: :index - namespace :notifications do - resource :tray, only: :show - resource :mark_all_read, only: :create - resources :mark_read, only: :create - resource :settings, only: :show - end - resources :notifications do - member do - post :mark_read + scope module: :notifications do + get "tray", to: "trays#show", on: :collection + get "settings", to: "settings#show", on: :collection + + post "readings", to: "readings#create_all", on: :collection, as: :read_all + post "reading", to: "readings#create", on: :member, as: :read end end diff --git a/test/controllers/notifications/mark_all_reads_controller_test.rb b/test/controllers/notifications/mark_all_reads_controller_test.rb deleted file mode 100644 index 2fa423b3a..000000000 --- a/test/controllers/notifications/mark_all_reads_controller_test.rb +++ /dev/null @@ -1,17 +0,0 @@ -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_path - end - end - - assert_response :success - end -end diff --git a/test/controllers/notifications/readings_controller_test.rb b/test/controllers/notifications/readings_controller_test.rb new file mode 100644 index 000000000..75a8a4875 --- /dev/null +++ b/test/controllers/notifications/readings_controller_test.rb @@ -0,0 +1,23 @@ +require "test_helper" + +class Notifications::ReadingsControllerTest < ActionDispatch::IntegrationTest + setup do + sign_in_as :kevin + end + + test "create" do + assert_changes -> { notifications(:logo_published_kevin).reload.read? }, from: false, to: true do + post read_notification_path(notifications(:logo_published_kevin), format: :turbo_stream) + assert_response :success + end + end + + test "create all" 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 read_all_notifications_path + assert_redirected_to notifications_path + end + end + end +end diff --git a/test/controllers/notifications/trays_controller_test.rb b/test/controllers/notifications/trays_controller_test.rb index d49455ae1..07034fff0 100644 --- a/test/controllers/notifications/trays_controller_test.rb +++ b/test/controllers/notifications/trays_controller_test.rb @@ -6,7 +6,7 @@ class Notifications::TraysControllerTest < ActionDispatch::IntegrationTest end test "show" do - get notifications_tray_path + get tray_notifications_path assert_response :success assert_select "div", text: /Layout is broken/