Skip to content

Commit

Permalink
Revert "Attempting to improve disallowing irrelevant args, not workin…
Browse files Browse the repository at this point in the history
…g out"

This reverts commit 9b0f43e.
  • Loading branch information
mturley committed Oct 12, 2023
1 parent 9b0f43e commit 1fb049c
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 56 deletions.
40 changes: 14 additions & 26 deletions client/src/app/hooks/table-controls/filtering/getFilterProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,19 @@ import {
} from "@app/components/FilterToolbar";
import { IFilterState } from "./useFilterState";

export type IFilterPropsEnabledArgs<
TItem,
TFilterCategoryKey extends string,
> = {
filterCategories: FilterCategory<TItem, TFilterCategoryKey>[];
};

export type IFilterPropsArgs<TItem, TFilterCategoryKey extends string> = {
export interface IFilterPropsArgs<TItem, TFilterCategoryKey extends string> {
filterState: IFilterState<TFilterCategoryKey>;
} & (
| ({ isFilterEnabled: true } & IFilterPropsEnabledArgs<
TItem,
TFilterCategoryKey
>)
| { isFilterEnabled?: false }
);
filterCategories?: FilterCategory<TItem, TFilterCategoryKey>[];
}

export const getFilterProps = <TItem, TFilterCategoryKey extends string>(
args: IFilterPropsArgs<TItem, TFilterCategoryKey>
): IFilterToolbarProps<TItem, TFilterCategoryKey> => {
const { isFilterEnabled } = args;
return {
filterCategories: (isFilterEnabled && args.filterCategories) || [],
filterValues: (isFilterEnabled && args.filterState.filterValues) || {},
setFilterValues:
(isFilterEnabled && args.filterState.setFilterValues) || (() => {}),
};
};
export const getFilterProps = <TItem, TFilterCategoryKey extends string>({
filterState: { filterValues, setFilterValues },
filterCategories = [],
}: IFilterPropsArgs<TItem, TFilterCategoryKey>): IFilterToolbarProps<
TItem,
TFilterCategoryKey
> => ({
filterCategories,
filterValues,
setFilterValues,
});
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,24 @@ import {
import { objectKeys } from "@app/utils/utils";
import { IFilterState } from "./useFilterState";

export type ILocalFilterDerivedStateEnabledArgs<
export interface ILocalFilterDerivedStateArgs<
TItem,
TFilterCategoryKey extends string,
> = {
> {
items: TItem[];
filterCategories: FilterCategory<TItem, TFilterCategoryKey>[];
};

export type ILocalFilterDerivedStateArgs<
TItem,
TFilterCategoryKey extends string,
> =
| ({ isFilterEnabled: true } & ILocalFilterDerivedStateEnabledArgs<
TItem,
TFilterCategoryKey
>)
| { isFilterEnabled?: false };
filterCategories?: FilterCategory<TItem, TFilterCategoryKey>[];
}

export const getLocalFilterDerivedState = <
TItem,
TFilterCategoryKey extends string,
>(
args: ILocalFilterDerivedStateArgs<TItem, TFilterCategoryKey> & {
filterState: IFilterState<TFilterCategoryKey>;
}
) => {
const {
isFilterEnabled,
filterState: { filterValues },
} = args;
const items = (isFilterEnabled && args.items) || [];
const filterCategories = (isFilterEnabled && args.filterCategories) || [];
>({
items,
filterCategories = [],
filterState: { filterValues },
}: ILocalFilterDerivedStateArgs<TItem, TFilterCategoryKey> & {
filterState: IFilterState<TFilterCategoryKey>;
}) => {
const filteredItems = items.filter((item) =>
objectKeys(filterValues).every((categoryKey) => {
const values = filterValues[categoryKey];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ export const getLocalTableControlDerivedState = <
TFilterCategoryKey
>
): ITableControlDerivedState<TItem> => {
const { items, isPaginationEnabled, isFilterEnabled } = args;
const { items, isPaginationEnabled = true } = args;
const { filteredItems } = getLocalFilterDerivedState({
...args,
...(isFilterEnabled ? { items } : {}),
items,
});
const { sortedItems } = getLocalSortDerivedState({
...args,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,6 @@ export const ApplicationsTableAnalyze: React.FC = () => {
analysis: "Analysis",
tags: "Tags",
},
isSortEnabled: true,
isSelectionEnabled: true,
sortableColumns: ["name", "description", "businessService", "tags"],
initialSort: { columnKey: "name", direction: "asc" },
getSortValues: (app) => ({
Expand All @@ -206,7 +204,6 @@ export const ApplicationsTableAnalyze: React.FC = () => {
businessService: app.businessService?.name || "",
tags: app.tags?.length || 0,
}),
// TODO why is there no error for including filterCategories when isFilterEnabled isn't true?
filterCategories: [
{
key: "name",
Expand Down Expand Up @@ -327,6 +324,7 @@ export const ApplicationsTableAnalyze: React.FC = () => {
],
initialItemsPerPage: 10,
hasActionsColumn: true,
isSelectionEnabled: true,
});

const {
Expand Down

0 comments on commit 1fb049c

Please sign in to comment.