Skip to content

Commit 3d321bf

Browse files
Fran McDadeFran McDade
authored andcommitted
refactor: build next filter state for each category (#440)
1 parent b8c97db commit 3d321bf

3 files changed

Lines changed: 63 additions & 24 deletions

File tree

src/common/categories/models/range/utils.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { SelectedFilter } from "../../../entities";
1+
import { CategoryValueKey, SelectedFilter } from "../../../entities";
22
import { isSelectedRange } from "./typeGuards";
33
import { SelectedRange } from "./types";
44

@@ -13,6 +13,28 @@ export function assertIsRange(value: unknown): asserts value is SelectedRange {
1313
}
1414
}
1515

16+
/**
17+
* Build the next filter state for the given range category.
18+
* @param nextCategorySelectedFilter - Next filter state for the range category.
19+
* @param selectedValue - Selected value for the range category.
20+
* @param selected - Whether the category value is selected.
21+
*/
22+
export function buildNextRangeFilterState(
23+
nextCategorySelectedFilter: SelectedFilter,
24+
selectedValue: CategoryValueKey,
25+
selected: boolean
26+
): void {
27+
if (selected) {
28+
// Assert that the selected value is a range.
29+
assertIsRange(selectedValue);
30+
// Set the selected range.
31+
nextCategorySelectedFilter.value = selectedValue;
32+
} else {
33+
// Remove the selected range.
34+
nextCategorySelectedFilter.value = [];
35+
}
36+
}
37+
1638
/**
1739
* Get the selected values for the given category, if any.
1840
* Handles type checking for selected range.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { CategoryValueKey, SelectedFilter } from "../../../entities";
2+
3+
/**
4+
* Build the next filter state for the given select category.
5+
* @param nextCategorySelectedFilter - Next filter state for the select category.
6+
* @param selectedValue - Selected value for the select category.
7+
* @param selected - Whether the category value is selected.
8+
*/
9+
export function buildNextSelectFilterState(
10+
nextCategorySelectedFilter: SelectedFilter,
11+
selectedValue: CategoryValueKey,
12+
selected: boolean
13+
): void {
14+
if (selected) {
15+
// Set the selected value.
16+
nextCategorySelectedFilter.value.push(selectedValue);
17+
} else {
18+
// Remove the selected value from the selected set of values.
19+
nextCategorySelectedFilter.value = nextCategorySelectedFilter.value.filter(
20+
(value: CategoryValueKey) => value !== selectedValue
21+
);
22+
}
23+
}

src/hooks/useCategoryFilter.ts

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { CategoryConfig } from "../common/categories/config/types";
22
import { findSelectCategoryConfig } from "../common/categories/config/utils";
33
import { isRangeCategory } from "../common/categories/models/range/typeGuards";
4-
import { assertIsRange } from "../common/categories/models/range/utils";
4+
import { buildNextRangeFilterState } from "../common/categories/models/range/utils";
5+
import { buildNextSelectFilterState } from "../common/categories/models/select/utils";
56
import { Category } from "../common/categories/models/types";
67
import { buildRangeCategoryView } from "../common/categories/views/range/utils";
78
import { CategoryView, VIEW_KIND } from "../common/categories/views/types";
@@ -174,28 +175,21 @@ export function buildNextFilterState(
174175
value: categorySelectedFilter ? [...categorySelectedFilter.value] : [],
175176
};
176177

177-
// Handle case where category value is selected.
178-
if (selected) {
179-
if (viewKind === VIEW_KIND.RANGE) {
180-
// Assert that the selected value is a range.
181-
assertIsRange(selectedValue);
182-
// Set the selected range.
183-
nextCategorySelectedFilter.value = selectedValue;
184-
} else {
185-
// Set the selected value.
186-
nextCategorySelectedFilter.value.push(selectedValue);
187-
}
188-
}
189-
// Otherwise, category value has been de-selected; remove the selected value from the selected set of values.
190-
else {
191-
if (viewKind === VIEW_KIND.RANGE) {
192-
nextCategorySelectedFilter.value = [];
193-
} else {
194-
nextCategorySelectedFilter.value =
195-
nextCategorySelectedFilter.value.filter(
196-
(value: CategoryValueKey) => value !== selectedValue
197-
);
198-
}
178+
// Build next filter state for category.
179+
if (viewKind === VIEW_KIND.RANGE) {
180+
// Handle range category.
181+
buildNextRangeFilterState(
182+
nextCategorySelectedFilter,
183+
selectedValue,
184+
selected
185+
);
186+
} else {
187+
// Handle select category.
188+
buildNextSelectFilterState(
189+
nextCategorySelectedFilter,
190+
selectedValue,
191+
selected
192+
);
199193
}
200194

201195
// Add the new selected filter for this category to the set of selected filters, if there are selected values for it.

0 commit comments

Comments
 (0)