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
15 changes: 14 additions & 1 deletion engine/app/controllers/coplan/notifications_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,20 @@ def mark_all_read

broadcast_badge_update

redirect_to notifications_path, notice: "All notifications marked as read."
respond_to do |format|
format.turbo_stream {
@notifications = current_user.notifications
.includes(:plan, :comment, comment_thread: [:created_by_user])
.newest_first
.unread
@unread_count = 0
render turbo_stream: [
turbo_stream.update("inbox-badge", "0"),
turbo_stream.replace("inbox-panel", partial: "coplan/notifications/panel")
Comment on lines +54 to +57
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Use actual unread count in turbo-stream response

The turbo-stream branch hard-codes the unread badge and panel state to zero (@unread_count = 0 and turbo_stream.update("inbox-badge", "0")) instead of deriving them from the database after update_all. If a new notification is created for this user during the request window, the response will render unread items while simultaneously setting the badge to 0 and hiding the "Mark all read" button, leaving the dropdown in an inconsistent state until another refresh/broadcast occurs.

Useful? React with 👍 / 👎.

]
}
format.html { redirect_to notifications_path, notice: "All notifications marked as read." }
end
end

private
Expand Down
2 changes: 1 addition & 1 deletion engine/app/views/coplan/notifications/_panel.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<div class="inbox-panel__header">
<span class="inbox-panel__title">Inbox</span>
<% if @unread_count > 0 %>
<%= button_to "Mark all read", mark_all_read_notifications_path, method: :post, class: "btn btn--secondary btn--sm", data: { turbo: false } %>
<%= button_to "Mark all read", mark_all_read_notifications_path, method: :post, class: "btn btn--secondary btn--sm", form: { data: { turbo_stream: true } } %>
<% end %>
</div>

Expand Down
14 changes: 14 additions & 0 deletions spec/requests/notifications_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,5 +84,19 @@
post mark_all_read_notifications_path
expect(other_notification.reload.read_at).to be_nil
end

it "responds with a turbo_stream that updates the badge and replaces the inbox panel" do
create(:notification, user: user, plan: plan, comment_thread: thread)

post mark_all_read_notifications_path, headers: { "Accept" => "text/vnd.turbo-stream.html" }

expect(response).to have_http_status(:ok)
expect(response.media_type).to eq(Mime[:turbo_stream])
expect(response.body).to include('target="inbox-badge"')
expect(response.body).to include('action="update"')
expect(response.body).to include('target="inbox-panel"')
expect(response.body).to include('action="replace"')
expect(user.notifications.unread.count).to eq(0)
end
end
end
Loading