Skip to content

Commit

Permalink
Support claims index page filters
Browse files Browse the repository at this point in the history
We need to allow our support users to filter the claims.

Two filters, School and Provider filters have 2 search fields where
you can search by school name or provider to get the filter you want.
This is done by javascript in a stimulus controller.

The other 2 filters, Submitted after and Submitted before filters are
date filters and use the govuk_date_field form helper. You cannot set
the value of this helper as it's sending 3 params not just 1. So to
persist the value of these filters we need a form object.

Claims::Support::Claims::FilterForm is a form object that is meant to
just house the params and all the logic that these filter need.

All the params of the filters are in the url.
  • Loading branch information
CatalinVoineag committed Apr 15, 2024
1 parent 77e63a4 commit 16346a3
Show file tree
Hide file tree
Showing 16 changed files with 843 additions and 27 deletions.
3 changes: 3 additions & 0 deletions app/assets/images/icon-magnifying-glass.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 23 additions & 1 deletion app/assets/stylesheets/filter-form.scss
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,6 @@
&:hover:after {
background-image: url("icon-tag-remove-cross-white.svg");
}

}

.app-filter__options {
Expand All @@ -258,3 +257,26 @@
}
}
}

.app-filter__option .govuk-checkboxes {
position: relative;
max-height: 200px;
overflow-x: hidden;
overflow-y: auto;
}

.app-filter__option input[type=search] {
background: url("icon-magnifying-glass.svg") no-repeat;
background-color: govuk-colour("white");
padding-left: govuk-spacing(6);
}

.app-filter__option:not(:last-of-type) {
border-bottom: 1px solid $govuk-border-colour;
margin-bottom: govuk-spacing(4);

div:last-of-type {
margin-bottom: govuk-spacing(1);
}
}

2 changes: 1 addition & 1 deletion app/components/claim/card_component.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
</div>

<div class="claim-card__body__right">
<div class="govuk-body-s"><%= l(claim.created_at.to_date, format: :short) %></div>
<div class="govuk-body-s"><%= l(claim.submitted_at.to_date, format: :short) %></div>
<div class="govuk-body-s"><%= humanized_money_with_symbol(claim.amount) %></div>
</div>
</div>
Expand Down
27 changes: 26 additions & 1 deletion app/controllers/claims/support/claims_controller.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
class Claims::Support::ClaimsController < Claims::Support::ApplicationController
before_action :set_claim, only: %i[show]
before_action :authorize_claim
helper_method :filter_form

def index
@pagy, @claims = pagy(Claims::ClaimsQuery.call(params:))
@pagy, @claims = pagy(Claims::ClaimsQuery.call(params: filter_form.query_params))
@schools = Claims::School.all
@providers = Claims::Provider.private_beta_providers
end

def show; end
Expand All @@ -16,6 +19,28 @@ def download_csv

private

def filter_form
Claims::Support::Claims::FilterForm.new(filter_params)
end

def filter_params
params.fetch(:claims_support_claims_filter_form, {}).permit(
:search,
:search_school,
:search_provider,
"submitted_after(1i)",
"submitted_after(2i)",
"submitted_after(3i)",
"submitted_before(1i)",
"submitted_before(2i)",
"submitted_before(3i)",
:submitted_after,
:submitted_before,
provider_ids: [],
school_ids: [],
)
end

def set_claim
@claim = Claims::Claim.find(params.require(:id))
end
Expand Down
111 changes: 111 additions & 0 deletions app/forms/claims/support/claims/filter_form.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
class Claims::Support::Claims::FilterForm < ApplicationForm
include ActiveModel::Attributes

attribute :search
attribute :search_school
attribute :search_provider
attribute "submitted_after(1i)"
attribute "submitted_after(2i)"
attribute "submitted_after(3i)"
attribute "submitted_before(1i)"
attribute "submitted_before(2i)"
attribute "submitted_before(3i)"
attribute :school_ids, default: []
attribute :provider_ids, default: []

def initialize(params = {})
params[:school_ids].compact_blank! if params[:school_ids].present?
params[:provider_ids].compact_blank! if params[:provider_ids].present?

super(params)
end

def filters_selected?
school_ids.present? ||
provider_ids.present? ||
submitted_after.present? ||
submitted_before.present?
end

def index_path_without_filter(filter:, value: nil)
without_filter = compacted_attributes.merge(
filter => compacted_attributes[filter].reject { |filter_value| filter_value == value },
)

claims_support_claims_path(
params: { claims_support_claims_filter_form: without_filter },
)
end

def index_path_without_submitted_dates(filter)
without_filter = compacted_attributes.except(
"#{filter}(1i)",
"#{filter}(2i)",
"#{filter}(3i)",
)

claims_support_claims_path(
params: { claims_support_claims_filter_form: without_filter },
)
end

def clear_filters_path
filter_params = search.present? ? { claims_support_claims_filter_form: { search: } } : {}

claims_support_claims_path(
params: filter_params,
)
end

def clear_search_path
claims_support_claims_path(
params: { claims_support_claims_filter_form: compacted_attributes.except("search") },
)
end

def schools
@schools ||= Claims::School.find(school_ids)
end

def providers
@providers ||= Claims::Provider.find(provider_ids)
end

def query_params
{
search:,
search_school:,
search_provider:,
school_ids:,
provider_ids:,
submitted_after:,
submitted_before:,
}
end

def submitted_after
Date.new(
attributes["submitted_after(1i)"].to_i, # year
attributes["submitted_after(2i)"].to_i, # month
attributes["submitted_after(3i)"].to_i, # day
)
rescue Date::Error
nil
end

def submitted_before
Date.new(
attributes["submitted_before(1i)"].to_i, # year
attributes["submitted_before(2i)"].to_i, # month
attributes["submitted_before(3i)"].to_i, # day
)
rescue Date::Error
nil
end

private

def compacted_attributes
@compacted_attributes ||= attributes.compact_blank
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { Controller } from "@hotwired/stimulus"

// Connects to data-controller="claims-support-filter-search"
export default class extends Controller {
static targets = [ "schoolInput", "schoolList", "providerInput", "providerList" ]

connect() {
if (this.schoolInputTarget.value !== "") {
this.searchSchool()
}

if (this.providerInputTarget.value !== "") {
this.searchProvider()
}
}

searchSchool() {
const schoolItems = this.schoolListTarget.children
const searchValue = this.schoolInputTarget.value.toLowerCase()

Array.from(schoolItems).forEach(function (item) {
const inputField = item.querySelector("input")

if (item.textContent.toLowerCase().indexOf(searchValue) > -1 ) {
item.style.display = ""
} else {
item.style.display = "none"
inputField.checked = false
}
})
}

searchProvider() {
const providerItems = this.providerListTarget.children
const searchValue = this.providerInputTarget.value.toLowerCase()

Array.from(providerItems).forEach(function (item) {
const inputField = item.querySelector("input")

if (item.textContent.toLowerCase().indexOf(searchValue) > -1 ) {
item.style.display = ""
} else {
item.style.display = "none"
inputField.checked = false
}
})
}
}
3 changes: 3 additions & 0 deletions app/javascript/controllers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

import { application } from "./application"

import ClaimsSupportFilterSearchController from "./claims_support_filter_search_controller"
application.register("claims-support-filter-search", ClaimsSupportFilterSearchController )

import AutocompleteController from "./autocomplete_controller"
application.register("autocomplete", AutocompleteController)

Expand Down
15 changes: 15 additions & 0 deletions app/queries/claims/claims_query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ def call
scope = search_condition(scope)
scope = school_condition(scope)
scope = provider_condition(scope)
scope = submitted_after(scope)
scope = submitted_before(scope)

scope.order_created_at_desc
end

Expand All @@ -26,4 +29,16 @@ def provider_condition(scope)

scope.where(provider_id: params[:provider_ids])
end

def submitted_after(scope)
return scope if params[:submitted_after].nil?

scope.where(submitted_at: params[:submitted_after]..)
end

def submitted_before(scope)
return scope if params[:submitted_before].nil?

scope.where(submitted_at: ..params[:submitted_before])
end
end
Loading

0 comments on commit 16346a3

Please sign in to comment.