Implement cursored pagination in Moderation Panel

This commit is contained in:
Karina Kwiatek 2022-01-03 22:43:37 +01:00
parent fab007b2a2
commit 2cab96b9af
3 changed files with 43 additions and 5 deletions

View File

@ -3,10 +3,13 @@ class ModerationController < ApplicationController
def index
@type = params[:type]
@reports = if @type == 'all'
Report.where(deleted: false).reverse_order
else
Report.where(deleted: false).where('LOWER(type) = ?', "reports::#{@type}").reverse_order
@reports = list_reports(type: @type, last_id: params[:last_id])
@reports_last_id = @reports.map(&:id).min
@more_data_available = !list_reports(type: @type, last_id: @reports_last_id, size: 1).count.zero?
respond_to do |format|
format.html
format.js { render layout: false }
end
end
@ -62,4 +65,16 @@ class ModerationController < ApplicationController
render template: 'moderation/priority'
end
private
def list_reports(type:, last_id:, size: nil)
cursor_params = { last_id: last_id, size: size }.compact
if type == 'all'
Report.cursored_reports(**cursor_params)
else
Report.cursored_reports_of_type(type, **cursor_params)
end
end
end

View File

@ -13,4 +13,19 @@ class Report < ApplicationRecord
def votes
moderation_votes.where(upvote: true).count - moderation_votes.where(upvote: false).count
end
class << self
include CursorPaginatable
define_cursor_paginator :cursored_reports, :list_reports
define_cursor_paginator :cursored_reports_of_type, :list_reports_of_type
def list_reports(options = {})
self.where(options.merge!(deleted: false)).reverse_order
end
def list_reports_of_type(type, options = {})
self.where(options.merge!(deleted: false)).where('LOWER(type) = ?', "reports::#{type}").reverse_order
end
end
end

View File

@ -1,2 +1,10 @@
- @reports.each do |r|
= render 'moderation/moderationbox', report: r
- unless @reports.count.zero?
= render 'shared/cursored_pagination_dummy', more_data_available: @more_data_available, last_id: @reports_last_id, permitted_params: %i[type]
- 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: @reports_last_id } }
= t 'views.actions.load'