From 8c14fb54230764ea1d8d76f85d4a19805f964131 Mon Sep 17 00:00:00 2001 From: Andreas Nedbal Date: Sun, 4 Sep 2022 23:22:51 +0200 Subject: [PATCH] Refactor mod inbox to use a Turbo Stream-backed pagination --- app/controllers/moderation/inbox_controller.rb | 2 +- app/views/moderation/inbox/index.haml | 18 ++++++++---------- app/views/moderation/inbox/index.js.erb | 8 -------- .../moderation/inbox/index.turbo_stream.haml | 8 ++++++++ config/routes.rb | 2 +- 5 files changed, 18 insertions(+), 20 deletions(-) delete mode 100644 app/views/moderation/inbox/index.js.erb create mode 100644 app/views/moderation/inbox/index.turbo_stream.haml diff --git a/app/controllers/moderation/inbox_controller.rb b/app/controllers/moderation/inbox_controller.rb index a71ee529..deca65d0 100644 --- a/app/controllers/moderation/inbox_controller.rb +++ b/app/controllers/moderation/inbox_controller.rb @@ -12,7 +12,7 @@ class Moderation::InboxController < ApplicationController respond_to do |format| format.html - format.js { render layout: false } + format.turbo_stream end end end diff --git a/app/views/moderation/inbox/index.haml b/app/views/moderation/inbox/index.haml index c80f0f79..05198968 100644 --- a/app/views/moderation/inbox/index.haml +++ b/app/views/moderation/inbox/index.haml @@ -4,14 +4,12 @@ = render "header", user: @user, hidden: true .container-lg.container--main - #entries - - @inboxes.each do |i| - = render "inbox/entry", i: i + = turbo_frame_tag "moderation_inbox" do + #entries + - @inboxes.each do |i| + = render "inbox/entry", i: i - = render "shared/cursored_pagination_dummy", more_data_available: @more_data_available, last_id: @inbox_last_id - - - 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: @inbox_last_id } } - = t("voc.load") + - if @more_data_available + .d-flex.justify-content-center.justify-content-sm-start#paginator + = button_to mod_inbox_index_path(@user, last_id: @inbox_last_id), class: "btn btn-light" do + = t("voc.load") diff --git a/app/views/moderation/inbox/index.js.erb b/app/views/moderation/inbox/index.js.erb deleted file mode 100644 index 7bab73b8..00000000 --- a/app/views/moderation/inbox/index.js.erb +++ /dev/null @@ -1,8 +0,0 @@ -$('#entries').append('<% @inboxes.each do |i| - %><%= j render 'inbox/entry', i: i -%><% end %>'); -<% if @more_data_available %> -$('#pagination').html('<%= j render 'shared/cursored_pagination_dummy', more_data_available: @more_data_available, last_id: @inbox_last_id %>'); -<% else %> -$('#pagination, #load-more-btn').remove(); -<% end %> diff --git a/app/views/moderation/inbox/index.turbo_stream.haml b/app/views/moderation/inbox/index.turbo_stream.haml new file mode 100644 index 00000000..c2115434 --- /dev/null +++ b/app/views/moderation/inbox/index.turbo_stream.haml @@ -0,0 +1,8 @@ += turbo_stream.append "entries" do + - @inboxes.each do |i| + = render "inbox/entry", i: i + += turbo_stream.update "paginator" do + - if @more_data_available + = button_to mod_inbox_index_path(@user, last_id: @inbox_last_id), class: "btn btn-light" do + = t("voc.load") diff --git a/config/routes.rb b/config/routes.rb index 379d3030..947d4395 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -25,7 +25,7 @@ Rails.application.routes.draw do post "/moderation/unmask", to: "moderation#toggle_unmask", as: :moderation_toggle_unmask get "/moderation/blocks", to: "moderation/anonymous_block#index", as: :mod_anon_block_index get "/moderation/reports(/:type)", to: "moderation/reports#index", as: :moderation_reports, defaults: { type: "all" } - get "/moderation/inbox/:user", to: "moderation/inbox#index", as: :mod_inbox_index + match "/moderation/inbox/:user", to: "moderation/inbox#index", via: [:get, :post], as: :mod_inbox_index get "/moderation/questions/:author_identifier", to: "moderation/questions#show", as: :moderation_questions namespace :ajax do post "/mod/destroy_report", to: "moderation#destroy_report", as: :mod_destroy_report