implement datatable sorting on non-analytics tables. (website/board/link/pixel/team/admin)
This commit is contained in:
1 parent
b5c4dbfa56
commit
5ccfb6d0a5
39 files changed
+322
-100
No files matched your search
@@ -82,6 +82,16 @@ export const pagingParams = {
|
||||
|
||||
export const sortingParams = {
|
||||
orderBy: z.string().optional(),
|
||||
sortDescending: z
|
||||
.enum(['true', 'false'])
|
||||
.optional()
|
||||
.transform(value => {
|
||||
if (value === undefined) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
return value === 'true';
|
||||
}),
|
||||
};
|
||||
|
||||
export const userRoleParam = z.enum(['admin', 'user', 'view-only']);
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
import type { QueryFilters } from './types';
|
||||
|
||||
export function sanitizeSortFilters<const T extends readonly string[]>(
|
||||
filters: QueryFilters = {},
|
||||
allowedFields: T,
|
||||
defaults: Partial<Pick<QueryFilters, 'orderBy' | 'sortDescending'>> = {},
|
||||
): QueryFilters {
|
||||
const { orderBy, sortDescending, ...rest } = filters;
|
||||
const fallbackOrderBy = defaults.orderBy;
|
||||
const fallbackSortDescending = defaults.sortDescending;
|
||||
const isAllowed = orderBy ? allowedFields.includes(orderBy as T[number]) : false;
|
||||
|
||||
return {
|
||||
...rest,
|
||||
...(isAllowed
|
||||
? {
|
||||
orderBy,
|
||||
sortDescending,
|
||||
}
|
||||
: {
|
||||
...(fallbackOrderBy && { orderBy: fallbackOrderBy }),
|
||||
...(fallbackSortDescending !== undefined && {
|
||||
sortDescending: fallbackSortDescending,
|
||||
}),
|
||||
}),
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user