Skip to content

Commit

Permalink
Fix reports group selection, routing, and rendering in print layout
Browse files Browse the repository at this point in the history
  • Loading branch information
tomca32 committed Jul 29, 2024
1 parent 4a4f6c0 commit 4fb1e05
Show file tree
Hide file tree
Showing 9 changed files with 226 additions and 149 deletions.
9 changes: 9 additions & 0 deletions app/controllers/group_reports_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class GroupReportsController < HtmlController
skip_after_action :verify_policy_scoped
layout 'print'
def show
@group = Group.includes(:chapter).find params[:id]
authorize @group

end
end
34 changes: 8 additions & 26 deletions app/controllers/reports_controller.rb
Original file line number Diff line number Diff line change
@@ -1,30 +1,12 @@
class ReportsController < HtmlController
skip_after_action :verify_policy_scoped
include Pagy::Backend
has_scope :exclude_deleted, type: :boolean, default: true
has_scope :table_order, type: :hash, default: { key: :created_at, order: :desc }
has_scope :search, only: [:show, :index]

before_action do
@available_organizations = policy_scope Organization.where(deleted_at: nil).order(:organization_name)
@available_chapters = policy_scope Chapter.where(deleted_at: nil).order(:chapter_name)
@available_groups = policy_scope Group.where(deleted_at: nil).order(:group_name)
def show
@available_organizations = policy_scope Organization.where(deleted_at: nil).order(:organization_name)
@available_chapters = policy_scope Chapter.where(deleted_at: nil).order(:chapter_name)
@available_groups = policy_scope Group.where(deleted_at: nil).order(:group_name)

@selected_organization_id = params[:organization_id] || @available_organizations.first.id
@selected_chapter_id = params[:chapter_id]
@selected_group_id = params[:group_id]
end


def index
authorize Group
@group = Group.new
@pagy, @groups = pagy policy_scope(apply_scopes(GroupSummary.includes(chapter: [:organization])), policy_scope_class: GroupPolicy::Scope)
end

def show
@group = Group.includes(:chapter).find params[:group_select]
authorize @group
@pagy, @student_rows = pagy apply_scopes(StudentTableRow.where(group_id: @group.id).includes(:tags, :group))
@student_table_component = TableComponents::Table.new(pagy: @pagy, rows: @student_rows, row_component: TableComponents::StudentRow)
end
end
authorize Group
end
end
63 changes: 63 additions & 0 deletions app/javascript/controllers/reports_filter_controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { Controller } from '@hotwired/stimulus'

function createOption(label, value) {
const opt = document.createElement('option');
opt.text = label;
if (value !== undefined) {
opt.value = value;
}

return opt;
}
export default class extends Controller {
static targets = ['select', 'anchor']
static values = {
path: String
}

connect() {
this.updateFilter()
}

toId(value) {
if (Number.isNaN(Number(value))) {
return ''
}
return value
}

updateFilter() {
this.updateDropdown(this.selectTargets[0], JSON.parse(this.selectTargets[0].dataset.resources))
const replaceValue = this.toId(this.selectTargets[this.selectTargets.length - 1].value)
this.anchorTarget.href = this.pathValue.replace("placeholder", replaceValue)
}

updateDropdown(dropdown, values, parentIds) {
const currentValue = Number(dropdown.value);
dropdown.innerHTML = '';
const dependents = JSON.parse(dropdown.dataset.dependents || null);
if (dependents) {
dropdown.append(createOption('All', null));
}

let valueExists = false;
const filteredIds = values.filter(v => !parentIds || parentIds.includes(v.depend_id)).map((v) => {
if (currentValue === v.id) {
valueExists = true;
}
dropdown.append(createOption(v.label, v.id));
return v.id;
});

if (valueExists) {
dropdown.value = currentValue;
}

if (dependents) {
dependents.forEach(dependentSelectName => {
const targetSelect = this.selectTargets.find(t => t.dataset.name === dependentSelectName);
this.updateDropdown(targetSelect, JSON.parse(targetSelect.dataset.resources), valueExists ? [currentValue] : filteredIds)
})
}
}
}
7 changes: 7 additions & 0 deletions app/views/group_reports/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<h1>Lorem ipsum</h1>

<script>
document.addEventListener('DOMContentLoaded', function () {
window.print();
});
</script>
24 changes: 24 additions & 0 deletions app/views/layouts/print.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<!DOCTYPE html>
<html class="h-full bg-gray-100 overflow-hidden">
<head>
<title>Tracker<% if content_for? :title %><%= " - #{content_for :title}" %><% end %></title>
<meta name="turbo-cache-control" content="no-preview">
<meta content="notranslate" name="google" />
<%= favicon_link_tag 'favicon.ico' %>
<%= stylesheet_link_tag 'tailwind.css', media: 'all' %>
<%= javascript_importmap_tags %>
<%= yield :head_content %>
<link href="https://fonts.googleapis.com/css?family=Roboto" rel="stylesheet" />
<% if staging_site? %>
<meta name="robots" content="noindex, nofollow">
<link rel="canonical" href="https://mindleapstracker.org<%= request.path %>" />
<% end %>
</head>
<body class="h-full bg-gray-200">
<div class="h-full flex">
<div class="flex flex-col w-full overflow-auto">
<%= yield %>
</div>
</div>
</body>
</html>
22 changes: 0 additions & 22 deletions app/views/reports/index.html.erb

This file was deleted.

29 changes: 22 additions & 7 deletions app/views/reports/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
<h1>Lorem ipsum</h1>

<script>
document.addEventListener('DOMContentLoaded', function () {
window.print();
});
</script>
<% content_for :title, t(:subject_analytics).capitalize %>
<div class="overflow-x-scroll">
<div class="p-4 filter-wrap flex items-center flex-1">
<div class="p-4 filter-wrap flex items-center flex-1" data-controller="reports-filter" data-reports-filter-path-value="<%= reports_group_path(:placeholder) %>">
<div class="pl-2">
<%= label_tag :organization_label, "Organization", class: 'block text-sm font-medium text-gray-700' %>
<%= select_tag :organization_select, options_from_collection_for_select(@available_organizations, :id, :organization_name), :class => 'mt-1 block w-full pl-3 pr-10 py-2 text-base border-gray-300 focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm rounded-md', :prompt => 'All', 'data-reports-filter-target' => 'select', 'data-action' => 'reports-filter#updateFilter', 'data-name' => 'organization_id', 'data-dependents' => ['chapter_id'].to_json, 'data-resources' => @available_organizations.map { |o| { id: o.id, label: o.organization_name} }.to_json %>
</div>
<div class="pl-2">
<%= label_tag :chapter_label, "Chapter", class: 'block text-sm font-medium text-gray-700' %>
<%= select_tag :chapter_select, options_from_collection_for_select(@available_chapters, :id, :chapter_name), :class => 'mt-1 block w-full pl-3 pr-10 py-2 text-base border-gray-300 focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm rounded-md', :prompt => 'All', 'data-reports-filter-target' => 'select', 'data-action' => 'reports-filter#updateFilter', 'data-name' => 'chapter_id', 'data-dependents' => ['group_id'].to_json, 'data-resources' => @available_chapters.map { |c| { id: c.id, label: c.chapter_name, depend_id: c.organization_id } }.to_json %>
</div>
<div class="pl-2">
<%= label_tag :group_label, "Group", class: 'block text-sm font-medium text-gray-700' %>
<%= select_tag :group_select, options_from_collection_for_select(@available_groups, :id, :group_name), :class => 'mt-1 block w-full pl-3 pr-10 py-2 text-base border-gray-300 focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm rounded-md', 'data-reports-filter-target' => 'select', 'data-action' => 'reports-filter#updateFilter', 'data-name' => 'group_id', 'data-resources' => @available_groups.map { |g| { id: g.id, label: g.group_name, depend_id: g.chapter_id } }.to_json %>
</div>
<div class="pl-2 pt-5">
<%= render CommonComponents::ButtonComponent.new(label: t(:filter), options: { 'data-reports-filter-target' => 'anchor', 'target' => '_blank' })%>
</div>
</div>
</div>
</div>
6 changes: 3 additions & 3 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@

resources :student_tags, only: %i[index new create show edit update]

resources :reports, only: %i[index show]
post 'reports/show', to: 'reports#show', as: :show_reports
get '/reports', controller: 'reports', action: :index
resource :reports, only: %i[show] do
resources :groups, controller: :group_reports, only: %i[show]
end
resources :lessons, only: %i[index new create show] do
resources :students, controller: :student_lessons, only: %i[show update]
end
Expand Down
Loading

0 comments on commit 4fb1e05

Please sign in to comment.