Skip to content

Commit

Permalink
add applied filter string func
Browse files Browse the repository at this point in the history
  • Loading branch information
charmingduchess committed Nov 8, 2023
1 parent f0223bf commit 42bb608
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/models/itemFilterData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,20 @@ export class ItemFilterData {
displayOptions(): Option[] {
return this.options
}

labelForValue(value: string) {
return this.displayOptions().find((opt: Option) => {
return opt.value === value
})?.label
}

labelsForConcatenatedValues(values: string) {
return Array.from(
new Set(
values.split(",").map((val: string) => `'${this.labelForValue(val)}'`)
)
).join(", ")
}
}

export class LocationFilterData extends ItemFilterData {
Expand Down
26 changes: 26 additions & 0 deletions src/utils/itemFilterUtils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* eslint-disable @typescript-eslint/naming-convention */
import type { ItemFilterData } from "../models/itemFilterData"
import type { AppliedFilters } from "../types/filterTypes"

export const isRecapLocation = (loc: string) => {
Expand Down Expand Up @@ -48,3 +49,28 @@ export const buildItemFilterQueryParams = (
const query = encodeURI(`?${location_query}${format_query}${status_query}`)
return query.length > 3 ? query : ""
}

export const buildAppliedFiltersString = (
query: BibPageQueryParams,
numItems = 20,
itemAggs: ItemFilterData[]
) => {
const items = `Item${numItems === 1 ? "" : "s"}`
if (Object.keys(query).length === 0) return `${numItems} ${items}`
const num = numItems === 0 ? "No" : numItems
const numMatchingItems = `${num} ${items} Matching `
const filters = Object.keys(query)
.map((field: string) => {
const queryPerField = query[field]
if (queryPerField) {
const fieldAggregations = itemAggs.find(
(agg: ItemFilterData) => agg.field === field.substring(5)
)
const labels =
fieldAggregations.labelsForConcatenatedValues(queryPerField)
return field.substring(5) + ": " + labels
}
})
.filter((filter) => filter)
return numMatchingItems + "Filtered by " + filters.join(", ")
}
49 changes: 49 additions & 0 deletions src/utils/utilsTests/itemFilterUtils.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ import {
combineRecapLocations,
parseItemFilterQueryParams,
buildItemFilterQueryParams,
buildAppliedFiltersString,
} from "../itemFilterUtils"
import testAggregations from "../../../__test__/fixtures/testAggregations"
import { ItemFilterData } from "../../models/itemFilterData"

describe("Item Filter Utils", () => {
describe("isRecapLocation", () => {
Expand Down Expand Up @@ -86,4 +89,50 @@ describe("Item Filter Utils", () => {
)
})
})

describe("buildAppliedFiltersString", () => {
const query = {
item_location: "loc:rc2ma,loc:rcma2",
item_status: "status:a",
item_format: "Text",
}
const aggs = testAggregations.map((agg) => new ItemFilterData(agg))
it("can handle no filters", () => {
expect(buildAppliedFiltersString({}, 30, [])).toBe("30 Items")
})
it("no items with filters", () => {
const query = {
item_location: "loc:rc2ma,loc:rcma2",
item_status: "status:a",
item_format: "Text",
}
expect(buildAppliedFiltersString(query, 0, aggs)).toBe(
"No Items Matching Filtered by location: 'Offsite', status: 'Available', format: 'Text'"
)
})
it("some items no filters", () => {
expect(buildAppliedFiltersString({}, 5, aggs)).toBe("5 Items")
})
it("some items with filters", () => {
expect(buildAppliedFiltersString(query, 5, aggs)).toBe(
"5 Items Matching Filtered by location: 'Offsite', status: 'Available', format: 'Text'"
)
})
it("one item with filters", () => {
expect(buildAppliedFiltersString(query, 1, aggs)).toBe(
"1 Item Matching Filtered by location: 'Offsite', status: 'Available', format: 'Text'"
)
})
it("some items one filter", () => {
expect(
buildAppliedFiltersString(
{ item_status: "status:a,status:na" },
5,
aggs
)
).toBe(
"5 Items Matching Filtered by status: 'Available', 'Not available'"
)
})
})
})

0 comments on commit 42bb608

Please sign in to comment.