Skip to content

Commit

Permalink
xFilter util improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
abailly-akamai committed Oct 31, 2024
1 parent f96f40b commit 071f12f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 26 deletions.
13 changes: 5 additions & 8 deletions packages/manager/src/features/Volumes/VolumesLanding.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,11 @@ export const VolumesLanding = () => {
preferenceKey,
});

// const filter: Filter = {
// ['+order']: order,
// ['+order_by']: orderBy,
// ...(query && {
// label: { '+contains': query },
// }),
// };
const volumeXFilters = buildVolumeXFilters(query);
const volumeXFilters = buildVolumeXFilters({
order,
orderBy,
query,
});
const { data: volumes, error, isFetching, isLoading } = useVolumesQuery(
{
page: pagination.page,
Expand Down
17 changes: 5 additions & 12 deletions packages/manager/src/routes/utils/buildXFilters.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Filter, FilterConditionTypes } from '@linode/api-v4';

type OrderDirection = FilterConditionTypes['+order'];
export type OrderDirection = FilterConditionTypes['+order'];
type XFilterableField = string;

type FilterOperator = Exclude<
Expand All @@ -12,27 +12,20 @@ type FieldFilter = {
[K in FilterOperator]?: FilterConditionTypes[K];
};

interface XFilterDefaults {
order: OrderDirection;
orderBy: XFilterableField;
}

interface BuildXFilterParams<T extends XFilterableField> {
additionalFilters?: Partial<Record<T, FieldFilter>>;
defaults: XFilterDefaults;
order?: OrderDirection;
orderBy?: T;
order: OrderDirection;
orderBy: T;
}

export function buildXFilters<T extends XFilterableField>({
additionalFilters,
defaults,
order,
orderBy,
}: BuildXFilterParams<T>): Filter {
return {
'+order': order ?? defaults.order,
'+order_by': orderBy ?? defaults.orderBy,
'+order': order,
'+order_by': orderBy,
...additionalFilters,
};
}
25 changes: 19 additions & 6 deletions packages/manager/src/routes/volumes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { buildXFilters } from '../utils/buildXFilters';
import { VolumesRoot } from './VolumesRoot';

import type { TableSearchParams } from '../types';
import type { OrderDirection } from '../utils/buildXFilters';

const volumeAction = {
attach: 'attach',
Expand All @@ -25,17 +26,25 @@ export interface VolumesSearchParams extends TableSearchParams {
query?: string;
}

export const buildVolumeXFilters = (query?: string) =>
interface BuildVolumeXFiltersParams {
order: OrderDirection;
orderBy: string | undefined;
query?: string;
}

export const buildVolumeXFilters = ({
order = 'desc',
orderBy = 'label',
query,
}: BuildVolumeXFiltersParams) =>
buildXFilters({
additionalFilters: query
? {
label: { '+contains': query },
}
: undefined,
defaults: {
order: 'asc',
orderBy: 'label',
},
order,
orderBy,
});

const volumesRoute = createRoute({
Expand Down Expand Up @@ -70,7 +79,11 @@ const volumeActionRoute = createRoute({
});
}

const volumeXFilters = buildVolumeXFilters(search.query);
const volumeXFilters = buildVolumeXFilters({
order: search.order,
orderBy: search.orderBy,
query: search.query,
});
const volumes = await context.queryClient.fetchQuery(
volumeQueries.lists._ctx.paginated(
{
Expand Down

0 comments on commit 071f12f

Please sign in to comment.