Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions app/controllers/notifications_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adopted the same lazy-load pattern as StoriesController, PeopleController, etc. — only load data for turbo frame requests, render a skeleton for the initial page load.

@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
Expand Down
2 changes: 2 additions & 0 deletions app/views/notifications/_index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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 %>
<span class="text-gray-400">—</span>
Expand All @@ -54,6 +55,7 @@
<td class="px-4 py-3 text-right whitespace-nowrap">
<%= link_to "View",
notification_path(notification),
data: { turbo_frame: "_top" },
class: "btn btn-secondary-outline" %>
</td>
</tr>
Expand Down
21 changes: 0 additions & 21 deletions app/views/notifications/_notifications_results.html.erb

This file was deleted.

24 changes: 24 additions & 0 deletions app/views/notifications/_results_skeleton.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<div class="overflow-x-auto bg-white border border-gray-200 rounded-xl shadow-sm">
<table class="min-w-full divide-y divide-gray-200 text-sm">
<thead class="bg-gray-50 animate-pulse">
<tr>
<th class="px-4 py-3 text-left"><div class="h-4 w-16 rounded bg-gray-200"></div></th>
<th class="px-4 py-3 text-left"><div class="h-4 w-24 rounded bg-gray-200"></div></th>
<th class="px-4 py-3 text-left"><div class="h-4 w-32 rounded bg-gray-200"></div></th>
<th class="px-4 py-3 text-left"><div class="h-4 w-20 rounded bg-gray-200"></div></th>
<th class="px-4 py-3 text-right"><div class="h-4 w-12 ml-auto rounded bg-gray-200"></div></th>
</tr>
</thead>
<tbody class="divide-y divide-gray-100 animate-pulse">
<% 5.times do %>
<tr>
<td class="px-4 py-3"><div class="h-4 w-24 rounded bg-gray-200"></div></td>
<td class="px-4 py-3"><div class="h-4 w-40 rounded bg-gray-200"></div></td>
<td class="px-4 py-3"><div class="h-4 w-48 rounded bg-gray-200"></div></td>
<td class="px-4 py-3"><div class="h-4 w-28 rounded bg-gray-200"></div></td>
<td class="px-4 py-3 text-right"><div class="h-4 w-12 ml-auto rounded bg-gray-200"></div></td>
</tr>
<% end %>
</tbody>
</table>
</div>
9 changes: 1 addition & 8 deletions app/views/notifications/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,7 @@
<% result_src = notifications_path + "?" + request.query_string %>

<%= turbo_frame_tag "notifications_results", src: result_src do %>
<div class="rounded-xl bg-white p-6">
<%= render "index" %>
</div>

<!-- Pagination -->
<div class="pagination flex justify-center mt-12">
<%= tailwind_paginate @notifications %>
</div>
<%= render "results_skeleton" %>
<% end %>
</section>
</div>
Expand Down
19 changes: 11 additions & 8 deletions spec/requests/notifications_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading