diff --git a/app/controllers/notifications_controller.rb b/app/controllers/notifications_controller.rb index 0b76fd2a0..6c948f914 100644 --- a/app/controllers/notifications_controller.rb +++ b/app/controllers/notifications_controller.rb @@ -3,13 +3,18 @@ class NotificationsController < ApplicationController def index authorize! - per_page = params[:number_of_items_per_page].presence || 25 - base_scope = authorized_scope(Notification.includes(:noticeable)) - filtered = base_scope.search_by_params(params.to_unsafe_h) - @notifications = filtered.order(created_at: :desc) - .paginate(page: params[:page], per_page: per_page) - render turbo_frame_request? ? :notifications_results : :index + if turbo_frame_request? + per_page = params[:number_of_items_per_page].presence || 25 + base_scope = authorized_scope(Notification.includes(:noticeable)) + filtered = base_scope.search_by_params(params.to_unsafe_h) + @notifications = filtered.order(created_at: :desc) + .paginate(page: params[:page], per_page: per_page) + + render :notifications_results + else + render :index + end end def show diff --git a/app/views/notifications/_index.html.erb b/app/views/notifications/_index.html.erb index ad0e3e9bd..22898cca7 100644 --- a/app/views/notifications/_index.html.erb +++ b/app/views/notifications/_index.html.erb @@ -44,6 +44,7 @@ <% if notification.noticeable.present? %> <%= link_to notification.noticeable.class.name, polymorphic_path(notification.noticeable), + data: { turbo_frame: "_top" }, class: "btn btn-secondary-outline" %> <% else %> @@ -54,6 +55,7 @@ <%= link_to "View", notification_path(notification), + data: { turbo_frame: "_top" }, class: "btn btn-secondary-outline" %> diff --git a/app/views/notifications/_notifications_results.html.erb b/app/views/notifications/_notifications_results.html.erb deleted file mode 100644 index 5638b31c2..000000000 --- a/app/views/notifications/_notifications_results.html.erb +++ /dev/null @@ -1,21 +0,0 @@ -<%= turbo_stream.replace("notifications_count", partial: "notifications_count") %> -<% if @notifications.any? %> -
- <%= render "index" %> -
- -
- -
-<% else %> -
-

No notifications yet

- <% if allowed_to?(:new?, Notification) %> -
- <%= link_to "Create a notification", - new_notification_path, - class: "btn btn-primary" %> -
- <% end %> -
-<% end %> diff --git a/app/views/notifications/_results_skeleton.html.erb b/app/views/notifications/_results_skeleton.html.erb new file mode 100644 index 000000000..3f95119aa --- /dev/null +++ b/app/views/notifications/_results_skeleton.html.erb @@ -0,0 +1,24 @@ +
+ + + + + + + + + + + + <% 5.times do %> + + + + + + + + <% end %> + +
+
diff --git a/app/views/notifications/index.html.erb b/app/views/notifications/index.html.erb index 9bde34ce3..25317c709 100644 --- a/app/views/notifications/index.html.erb +++ b/app/views/notifications/index.html.erb @@ -11,14 +11,7 @@ <% result_src = notifications_path + "?" + request.query_string %> <%= turbo_frame_tag "notifications_results", src: result_src do %> -
- <%= render "index" %> -
- - - + <%= render "results_skeleton" %> <% end %> diff --git a/spec/requests/notifications_spec.rb b/spec/requests/notifications_spec.rb index 7cfdc557f..89629af49 100644 --- a/spec/requests/notifications_spec.rb +++ b/spec/requests/notifications_spec.rb @@ -8,22 +8,25 @@ describe "GET /notifications" do before { sign_in admin } + let(:turbo_headers) { { "Turbo-Frame" => "notifications_results" } } let!(:story_notification) { create(:notification, noticeable: create(:story_idea), email_subject: "New story idea") } let!(:user_notification) { create(:notification, noticeable: create(:user), email_subject: "Welcome") } - it "preserves the email_topic filter selection" do - get notifications_path, params: { email_topic: "User: confirm new email" } - expect(response.body).to include('selected="selected"') - expect(response.body).to include("User: confirm new email") + it "filters by email_topic" do + matching = create(:notification, email_subject: "Confirm your new email address") + get notifications_path, params: { email_topic: "User: confirm new email" }, headers: turbo_headers + expect(response.body).to include(matching.recipient_email) + expect(response.body).not_to include(story_notification.recipient_email) end - it "preserves the record_type filter selection" do - get notifications_path, params: { record_type: "StoryIdea" } - expect(response.body).to include('selected="selected"') + it "filters by record_type" do + get notifications_path, params: { record_type: "StoryIdea" }, headers: turbo_headers + expect(response.body).to include(story_notification.recipient_email) + expect(response.body).not_to include(user_notification.recipient_email) end it "wraps results in a turbo frame" do - get notifications_path + get notifications_path, headers: turbo_headers expect(response.body).to include('id="notifications_results"') end end