-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
1 parent
30a9521
commit 6de4acb
Showing
16 changed files
with
843 additions
and
27 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
48 changes: 48 additions & 0 deletions
48
app/javascript/controllers/claims_support_filter_search_controller.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.