From 72c972c90d984def06b32d89c041e7bef63ddd76 Mon Sep 17 00:00:00 2001 From: wninohira Date: Fri, 1 Jul 2022 14:31:22 -0400 Subject: [PATCH] Adds pagination for reports list view --- app/controllers/reports_controller.rb | 41 +++++++++++++-------------- app/views/reports/index.html.erb | 6 +++- 2 files changed, 25 insertions(+), 22 deletions(-) diff --git a/app/controllers/reports_controller.rb b/app/controllers/reports_controller.rb index 396dcacb..03695ff9 100644 --- a/app/controllers/reports_controller.rb +++ b/app/controllers/reports_controller.rb @@ -1,25 +1,24 @@ class ReportsController < ApplicationController - + layout "backstage", only: [ :index, :show ] - + before_action :authenticate_user!, only: [ :index, :show, :dismiss, :undismiss ] before_action :ensure_staff, only: [ :index, :show, :dismiss, :undismiss ] before_action :find_report, only: [ :show, :dismiss, :undismiss ] before_action :find_reported_twitch_user, only: [ :show ] around_action :display_timezone - + def index # f is used to filter reports by scope if params[:f].present? && Report::AVAILABLE_SCOPES.key?(params[:f].to_sym) - @reports = eval("Report."+params[:f]+".all.order(created_at: :desc)") - # TODO add: paginate(page: params[:page], per_page: 30) + @reports = eval("Report."+params[:f]+".all.order(created_at: :desc).paginate(page: params[:page], per_page: 30)") @filter_category = params[:f] else - @reports = Report.unresolved.all.order(created_at: :desc) + @reports = Report.unresolved.all.order(created_at: :desc).paginate(page: params[:page], per_page: 30) @filter_category = "unresolved" end end - + def show # Create keybot advice message if @reported_twitch_user == nil @@ -29,30 +28,30 @@ def show else @message = "The reported Twitch user did not sign the pledge." end - + # TODO: check if reporter has pledged (lookup email/Twitch name) and add info to keybot message # TODO: check if incident stream owner has pledged (Twitch name) and add info to keybot message end - + def new @report = Report.new end def create @report = Report.new(report_params) - + if @report.save # Email notification to staff StaffMailer.notify_staff_new_report(@report).deliver_now - + flash[:notice] = "You've successfully submitted the report. Thank you." redirect_to root_path - else + else flash.now[:alert] ||= "" @report.errors.full_messages.each do |message| flash.now[:alert] << message + ". " - end + end render(action: :new) end end @@ -65,14 +64,14 @@ def dismiss end redirect_to reports_path end - + def undismiss @report.dismissed = false @report.reviewer = nil if @report.save flash[:notice] = "You undismissed the report about #{@report.reported_twitch_name}. It can now be reviewed again." redirect_to report_path(@report) - else + else redirect_to reports_path end end @@ -83,11 +82,11 @@ def find_report rescue ActiveRecord::RecordNotFound redirect_to staff_index_path end - + def find_reported_twitch_user # Check if reported_twitch_name exists on Twitch response = HTTParty.get(URI.escape("#{ENV['TWITCH_API_BASE_URL']}/users?login=#{@report.reported_twitch_name}"), headers: {"Client-ID": ENV['TWITCH_CLIENT_ID'], "Authorization": "Bearer #{TwitchToken.first.valid_token!}"}) - + if response["data"].blank? @reported_twitch_user = nil else @@ -95,20 +94,20 @@ def find_reported_twitch_user end end - private + private def ensure_staff unless current_user.is_moderator? || current_user.is_admin? redirect_to root_url end end - + def display_timezone timezone = Time.find_zone( cookies[:browser_timezone] ) Time.use_zone(timezone) { yield } end - + def report_params params.require(:report).permit(:reporter_email, :reporter_twitch_name, :reported_twitch_name, :incident_stream, :incident_occurred, :incident_description, :recommended_response, :image) end - + end diff --git a/app/views/reports/index.html.erb b/app/views/reports/index.html.erb index ea456c4e..e2eaac5a 100644 --- a/app/views/reports/index.html.erb +++ b/app/views/reports/index.html.erb @@ -6,7 +6,7 @@
<% Report::AVAILABLE_SCOPES.each do |filter| %> <% if @filter_category.to_s == filter[0].to_s %> - <%= link_to("#{filter[1]} [#{@reports.count}]", reports_path(f: filter[0]), class: "filter selected") %> + <%= link_to("#{filter[1]} [#{@reports.total_entries}]", reports_path(f: filter[0]), class: "filter selected") %> <% else %> <%= link_to(filter[1], reports_path(f: filter[0]), class: "filter") %> <% end %> @@ -16,6 +16,10 @@
<% if @reports.present? %> <%= render(partial: "reports/row", collection: @reports, as: :report) %> +
+ <%= will_paginate @reports, class: "pagination full flexbox horizontal", previous_label: "« PREV", next_label: "NEXT »", inner_window: 3, outer_window: 0 %> + <%= will_paginate @reports, class: "pagination mini flexbox horizontal justify-space-between", previous_label: "« PREV", next_label: "NEXT »", page_links: false %> +
<% else %>
No reports found.