From c118dab903213e293c89348c7573663a859327ba Mon Sep 17 00:00:00 2001 From: Andreas Nedbal Date: Mon, 5 Sep 2022 21:40:26 +0200 Subject: [PATCH] Refactor notifications to use a Turbo Stream-backed pagination --- app/controllers/notifications_controller.rb | 2 +- app/views/notifications/index.haml | 40 +++++++++---------- app/views/notifications/index.js.erb | 8 ---- .../notifications/index.turbo_stream.haml | 12 ++++++ config/routes.rb | 2 +- 5 files changed, 33 insertions(+), 31 deletions(-) delete mode 100644 app/views/notifications/index.js.erb create mode 100644 app/views/notifications/index.turbo_stream.haml diff --git a/app/controllers/notifications_controller.rb b/app/controllers/notifications_controller.rb index 89d6f656..725ef72e 100644 --- a/app/controllers/notifications_controller.rb +++ b/app/controllers/notifications_controller.rb @@ -19,7 +19,7 @@ class NotificationsController < ApplicationController respond_to do |format| format.html - format.js { render layout: false } + format.turbo_stream end end diff --git a/app/views/notifications/index.haml b/app/views/notifications/index.haml index 313c3687..6369aab3 100644 --- a/app/views/notifications/index.haml +++ b/app/views/notifications/index.haml @@ -1,27 +1,25 @@ -%ul.list-unstyled#notifications - - if @notifications.count.zero? - %li.card.text-center - .card-body - .notifications--none - %i.fa.fa-bell-o.notification__bell-icon - %p - - if params[:type] == 'all' - = t(".none_filtered") - - else - = t(".none") - - else - - @notifications.each do |notification| - %li.card += turbo_frame_tag "notification" do + %ul.list-unstyled#notifications + - if @notifications.count.zero? + %li.card.text-center .card-body - .media - = render "notifications/type/#{notification.target.class.name.downcase.split('::').last}", notification: notification - - - unless @notifications.count.zero? - = render 'shared/cursored_pagination_dummy', more_data_available: @more_data_available, last_id: @notifications_last_id, permitted_params: %i[type] + .notifications--none + %i.fa.fa-bell-o.notification__bell-icon + %p + - if params[:type] == 'all' + = t(".none_filtered") + - else + = t(".none") + - else + - @notifications.each do |notification| + %li.card + .card-body + .media + = render "notifications/type/#{notification.target.class.name.downcase.split('::').last}", notification: notification - if @more_data_available - .d-flex.justify-content-center.justify-content-sm-start - %button.btn.btn-light#load-more-btn{ type: :button, data: { last_id: @notifications_last_id } } + .d-flex.justify-content-center.justify-content-sm-start#paginator + = button_to notifications_path(last_id: @notifications_last_id, type: params[:type]), class: "btn btn-light" do = t("voc.load") - provide(:title, generate_title(t(".title"))) diff --git a/app/views/notifications/index.js.erb b/app/views/notifications/index.js.erb deleted file mode 100644 index 6ab1e1fc..00000000 --- a/app/views/notifications/index.js.erb +++ /dev/null @@ -1,8 +0,0 @@ -$('#notifications').append('<% @notifications.each do |notification| - %>
  • <%= j render "notifications/type/#{notification.target_type.downcase}", notification: notification -%>
  • <% end %>'); -<% if @more_data_available %> - $('#pagination').html('<%= j render 'shared/cursored_pagination_dummy', more_data_available: @more_data_available, last_id: @notifications_last_id, permitted_params: %i[type] %>'); -<% else %> - $('#pagination, #load-more-btn').remove(); -<% end %> diff --git a/app/views/notifications/index.turbo_stream.haml b/app/views/notifications/index.turbo_stream.haml new file mode 100644 index 00000000..8c7fb94d --- /dev/null +++ b/app/views/notifications/index.turbo_stream.haml @@ -0,0 +1,12 @@ += turbo_stream.append "notifications" do + - @notifications.each do |notification| + %li.card + .card-body + .media + = render "notifications/type/#{notification.target.class.name.downcase.split('::').last}", notification: notification + += turbo_stream.update "paginator" do + - if @more_data_available + = button_to notifications_path(last_id: @notifications_last_id, type: params[:type]), class: "btn btn-light" do + = t("voc.load") + diff --git a/config/routes.rb b/config/routes.rb index 947d4395..36d48e24 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -141,7 +141,7 @@ Rails.application.routes.draw do get "/public", to: "timeline#public", as: :public_timeline if APP_CONFIG.dig(:features, :public, :enabled) get "/list/:list_name", to: "timeline#list", as: :list_timeline - get "/notifications(/:type)", to: "notifications#index", as: :notifications, defaults: { type: "new" } + match "/notifications(/:type)", to: "notifications#index", via: [:get, :post], as: :notifications, defaults: { type: "new" } match "/inbox(/:author)", via: [:get, :post], to: "inbox#show", as: :inbox