Merge pull request #269 from Retrospring/feature/mod-pagination

Pagination in moderation panel
This commit is contained in:
Karina Kwiatek 2022-01-06 14:05:12 +01:00 committed by GitHub
commit 75d24db1b9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 52 additions and 7 deletions

View File

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

View File

@ -13,4 +13,19 @@ class Report < ApplicationRecord
def votes def votes
moderation_votes.where(upvote: true).count - moderation_votes.where(upvote: false).count moderation_votes.where(upvote: true).count - moderation_votes.where(upvote: false).count
end 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 end

View File

@ -1,2 +1,11 @@
- @reports.each do |r| #reports
= render 'moderation/moderationbox', report: r - @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'

View File

@ -0,0 +1,6 @@
$('#reports').append('<% @reports.each do |report| %><%= j render "moderation/moderationbox", report: report %><% end %>');
<% if @more_data_available %>
$('#pagination').html('<%= j render 'shared/cursored_pagination_dummy', more_data_available: @more_data_available, last_id: @reports_last_id, permitted_params: %i[type] %>');
<% else %>
$('#pagination, #load-more-btn').remove();
<% end %>