Skip to content

Commit

Permalink
Merge pull request #777 from rszwajko/dynamicFilters
Browse files Browse the repository at this point in the history
Create the filter dynamically based on available data
  • Loading branch information
yaacov authored Nov 28, 2023
2 parents e703f2f + 746e7f0 commit 34ee0d0
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export const AttributeValueWithMultipleSelectionTypes: Story = {
type: ['india', 'japan', 'france'],
archived: ['true'],
},
fieldFilters: fieldsMetadata.filter((field) => field.filter?.primary).map(toFieldFilter),
fieldFilters: fieldsMetadata.filter((field) => field.filter?.primary).map(toFieldFilter()),
supportedFilterTypes: defaultSupportedFilters,
},
};
Expand All @@ -107,7 +107,7 @@ export const AttributeValueWithMultipleSelectionTypes: Story = {
export const AttributeValueWithNoSelectedValues: Story = {
args: {
selectedFilters: {},
fieldFilters: fieldsMetadata.filter((field) => field.filter?.primary).map(toFieldFilter),
fieldFilters: fieldsMetadata.filter((field) => field.filter?.primary).map(toFieldFilter()),
supportedFilterTypes: defaultSupportedFilters,
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export const FilterGroupWithMultipleSelectionTypes: Story = {
type: ['india', 'japan', 'france'],
archived: ['true'],
},
fieldFilters: fieldsMetadata.filter((field) => field.filter?.primary).map(toFieldFilter),
fieldFilters: fieldsMetadata.filter((field) => field.filter?.primary).map(toFieldFilter()),
supportedFilterTypes: defaultSupportedFilters,
},
};
Expand All @@ -106,7 +106,7 @@ export const FilterGroupWithMultipleSelectionTypes: Story = {
export const FilterGroupWithNoSelectedValues: Story = {
args: {
selectedFilters: {},
fieldFilters: fieldsMetadata.filter((field) => field.filter?.primary).map(toFieldFilter),
fieldFilters: fieldsMetadata.filter((field) => field.filter?.primary).map(toFieldFilter()),
supportedFilterTypes: defaultSupportedFilters,
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export const GroupedEnumFilter = ({
selectedFilters: selectedEnumIds = [],
onFilterUpdate: onSelectedEnumIdsChange,
supportedValues: supportedEnumValues = [],
supportedGroups,
supportedGroups = [],
placeholderLabel,
showFilter = true,
}: FilterTypeProps) => {
Expand Down
12 changes: 7 additions & 5 deletions packages/common/src/components/FilterGroup/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ import { ResourceField } from '../../utils';

import { FieldFilter } from './types';

export const toFieldFilter = ({
resourceFieldId,
label,
filter: filterDef,
}: ResourceField): FieldFilter => ({ resourceFieldId, label, filterDef });
export const toFieldFilter =
(data?: unknown[]): ((field: ResourceField) => FieldFilter) =>
({ resourceFieldId, label, filter }: ResourceField): FieldFilter => ({
resourceFieldId,
label,
filterDef: { ...filter, ...filter?.dynamicFilter?.(data ?? []) },
});

export const EnumToTuple = (i18nEnum: { [k: string]: string }) =>
Object.entries(i18nEnum).map(([type, label]) => ({ id: type, label }));
1 change: 1 addition & 0 deletions packages/common/src/utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export interface FilterDef {
// by default missing/empty filters result in positive match (vacuous truth)
defaultValues?: string[];
helperText?: string | React.ReactNode;
dynamicFilter?: (items: unknown[]) => Partial<FilterDef>;
}

type OpenApiJsonPath = string | ((resourceData: unknown) => unknown);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,9 @@ export function StandardPage<T>({
const noResults = loaded && !error && flatData.length == 0;
const noMatchingResults = loaded && !error && filteredData.length === 0 && flatData.length > 0;

const primaryFilters = fields.filter((field) => field.filter?.primary).map(toFieldFilter);
const primaryFilters = fields
.filter((field) => field.filter?.primary)
.map(toFieldFilter(flatData));

return (
<>
Expand Down Expand Up @@ -246,7 +248,7 @@ export function StandardPage<T>({
<AttributeValueFilter
fieldFilters={fields
.filter(({ filter }) => filter && !filter.primary && !filter.standalone)
.map(toFieldFilter)}
.map(toFieldFilter(flatData))}
onFilterUpdate={setSelectedFilters}
selectedFilters={selectedFilters}
supportedFilterTypes={supportedFilters}
Expand All @@ -256,7 +258,7 @@ export function StandardPage<T>({
<FilterGroup
fieldFilters={fields
.filter((field) => field.filter?.standalone)
.map(toFieldFilter)}
.map(toFieldFilter(flatData))}
onFilterUpdate={setSelectedFilters}
selectedFilters={selectedFilters}
supportedFilterTypes={supportedFilters}
Expand Down

0 comments on commit 34ee0d0

Please sign in to comment.