Client: Stricter typing
This commit is contained in:
112
client/src/utils/filtering.ts
Normal file
112
client/src/utils/filtering.ts
Normal file
@@ -0,0 +1,112 @@
|
||||
import { CrudFilter, CrudOperators } from "@refinedev/core";
|
||||
import { ColumnFilterItem } from "antd/es/table/interface";
|
||||
|
||||
interface TypedCrudFilter<Obj> {
|
||||
field: keyof Obj;
|
||||
operator: Exclude<CrudOperators, "or" | "and">;
|
||||
value: string[];
|
||||
}
|
||||
|
||||
export function typeFilters<Obj>(filters: CrudFilter[]): TypedCrudFilter<Obj>[] {
|
||||
return filters as TypedCrudFilter<Obj>[]; // <-- Unsafe cast
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a filtering function that can be used to filter an array of objects based on the provided filters.
|
||||
* @param filters An array of `CrudFilter` objects that define the filtering criteria.
|
||||
* @returns A function that returns a boolean value based on whether the provided object matches the filtering criteria.
|
||||
*/
|
||||
export function genericFilterer<Obj>(filters: TypedCrudFilter<Obj>[]) {
|
||||
return (record: Obj) => {
|
||||
for (const filter of filters) {
|
||||
if (!("field" in filter)) {
|
||||
console.error("Filter must be of type LogicalFilter");
|
||||
return false;
|
||||
}
|
||||
if (!Array.isArray(filter.value)) {
|
||||
console.error("Filter value must be an array of strings.");
|
||||
return false;
|
||||
}
|
||||
if (!filter.value.length) {
|
||||
continue;
|
||||
}
|
||||
const value = record[filter.field];
|
||||
let strValue: string;
|
||||
if (value === undefined || value === null) {
|
||||
strValue = "";
|
||||
} else {
|
||||
if (typeof value !== "string") {
|
||||
console.error("Only string fields can be filtered, field is of type " + typeof value);
|
||||
return false;
|
||||
} else {
|
||||
strValue = value;
|
||||
}
|
||||
}
|
||||
|
||||
switch (filter.operator) {
|
||||
case "in":
|
||||
if (!filter.value.includes(strValue)) {
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
console.error(`Not implemented operator: ${filter.operator}`);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Populates an array of `ColumnFilterItem` objects based on the unique values of a given field in an array of objects.
|
||||
* @param dataSource An array of objects to filter.
|
||||
* @param field The name of the field to filter on.
|
||||
* @returns An array of `ColumnFilterItem` objects representing the unique values of the specified field in the input array.
|
||||
*/
|
||||
export function useListFiltersForField<Obj, Field extends keyof Obj>(
|
||||
dataSource: Obj[],
|
||||
field: Field): ColumnFilterItem[] {
|
||||
const filters: ColumnFilterItem[] = [];
|
||||
dataSource.forEach((element) => {
|
||||
const value = element[field];
|
||||
if (typeof value === "string" && value !== "") {
|
||||
// Make sure it's not already in the filters
|
||||
if (filters.find((f) => f.value === value)) {
|
||||
return;
|
||||
}
|
||||
filters.push({
|
||||
text: value,
|
||||
value: value,
|
||||
});
|
||||
}
|
||||
});
|
||||
// Sort the filters
|
||||
filters.sort((a, b) => {
|
||||
if (typeof a.text !== "string" || typeof b.text !== "string") {
|
||||
return 0;
|
||||
}
|
||||
return a.text.localeCompare(b.text);
|
||||
});
|
||||
filters.push({
|
||||
text: "<empty>",
|
||||
value: "",
|
||||
});
|
||||
return filters;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array of filter values for a given field based on the provided filters.
|
||||
* @param filters An array of `CrudFilter` objects that define the filtering criteria.
|
||||
* @param field The field to get the filter values for.
|
||||
* @returns An array of filter values for the given field.
|
||||
*/
|
||||
export function getFiltersForField<Obj, Field extends keyof Obj>(filters: TypedCrudFilter<Obj>[], field: Field): string[] {
|
||||
const filterValues: string[] = [];
|
||||
filters.forEach((filter) => {
|
||||
if (filter.field === field) {
|
||||
filterValues.push(...filter.value as string[]);
|
||||
}
|
||||
});
|
||||
return filterValues;
|
||||
}
|
||||
@@ -1,8 +1,10 @@
|
||||
import { CrudFilter, CrudSort } from "@refinedev/core";
|
||||
import { ColumnFilterItem, SortOrder } from "antd/es/table/interface";
|
||||
import { CrudSort } from "@refinedev/core";
|
||||
import { SortOrder } from "antd/es/table/interface";
|
||||
|
||||
interface IObj {
|
||||
[key: string]: unknown;
|
||||
|
||||
interface TypedCrudSort<Obj> {
|
||||
field: keyof Obj;
|
||||
order: "asc" | "desc";
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -10,8 +12,8 @@ interface IObj {
|
||||
* @param sorters An array of `CrudSort` objects that define the sorting criteria.
|
||||
* @returns A sorting function that can be used to sort an array of objects based on the provided sorters.
|
||||
*/
|
||||
export function genericSorter(sorters: CrudSort[]) {
|
||||
return (a: IObj, b: IObj) => {
|
||||
export function genericSorter<Obj>(sorters: TypedCrudSort<Obj>[]) {
|
||||
return (a: Obj, b: Obj) => {
|
||||
for (const sorter of sorters) {
|
||||
const aValue = a[sorter.field];
|
||||
const bValue = b[sorter.field];
|
||||
@@ -67,7 +69,7 @@ export function genericSorter(sorters: CrudSort[]) {
|
||||
* @param field The field to get the sort order for.
|
||||
* @returns The sort order for the given field, or undefined if the field is not being sorted.
|
||||
*/
|
||||
export function getSortOrderForField(sorters: CrudSort[], field: string): SortOrder | undefined {
|
||||
export function getSortOrderForField<Obj, Field extends keyof Obj>(sorters: TypedCrudSort<Obj>[], field: Field): SortOrder | undefined {
|
||||
const sorter = sorters.find((s) => s.field === field);
|
||||
if (sorter) {
|
||||
return sorter.order === "asc" ? "ascend" : "descend";
|
||||
@@ -75,95 +77,6 @@ export function getSortOrderForField(sorters: CrudSort[], field: string): SortOr
|
||||
return undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a filtering function that can be used to filter an array of objects based on the provided filters.
|
||||
* @param filters An array of `CrudFilter` objects that define the filtering criteria.
|
||||
* @returns A function that returns a boolean value based on whether the provided object matches the filtering criteria.
|
||||
*/
|
||||
export function genericFilterer(filters: CrudFilter[]) {
|
||||
return (record: IObj) => {
|
||||
for (const filter of filters) {
|
||||
if (!("field" in filter)) {
|
||||
console.error("Filter must be of type LogicalFilter");
|
||||
return false;
|
||||
}
|
||||
if (!Array.isArray(filter.value)) {
|
||||
console.error("Filter value must be an array of strings.");
|
||||
return false;
|
||||
}
|
||||
if (!filter.value.length) {
|
||||
continue;
|
||||
}
|
||||
let value = record[filter.field];
|
||||
if (value === undefined || value === null) {
|
||||
value = "";
|
||||
}
|
||||
if (typeof value !== "string") {
|
||||
console.error("Only string fields can be filtered, field is of type " + typeof value);
|
||||
return false;
|
||||
}
|
||||
switch (filter.operator) {
|
||||
case "in":
|
||||
if (!filter.value.includes(value)) {
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
console.error(`Not implemented operator: ${filter.operator}`);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Populates an array of `ColumnFilterItem` objects based on the unique values of a given field in an array of objects.
|
||||
* @param dataSource An array of objects to filter.
|
||||
* @param field The name of the field to filter on.
|
||||
* @returns An array of `ColumnFilterItem` objects representing the unique values of the specified field in the input array.
|
||||
*/
|
||||
export function filterPopulator(dataSource: IObj[], field: string): ColumnFilterItem[] {
|
||||
const filters: ColumnFilterItem[] = [];
|
||||
dataSource.forEach((element) => {
|
||||
const value = element[field];
|
||||
if (typeof value === "string" && value !== "") {
|
||||
// Make sure it's not already in the filters
|
||||
if (filters.find((f) => f.value === value)) {
|
||||
return;
|
||||
}
|
||||
filters.push({
|
||||
text: value,
|
||||
value: value,
|
||||
});
|
||||
}
|
||||
});
|
||||
// Sort the filters
|
||||
filters.sort((a, b) => {
|
||||
if (typeof a.text !== "string" || typeof b.text !== "string") {
|
||||
return 0;
|
||||
}
|
||||
return a.text.localeCompare(b.text);
|
||||
});
|
||||
filters.push({
|
||||
text: "<empty>",
|
||||
value: "",
|
||||
});
|
||||
return filters;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array of filter values for a given field based on the provided filters.
|
||||
* @param filters An array of `CrudFilter` objects that define the filtering criteria.
|
||||
* @param field The field to get the filter values for.
|
||||
* @returns An array of filter values for the given field.
|
||||
*/
|
||||
export function getFiltersForField(filters: CrudFilter[], field: string): string[] {
|
||||
const filterValues: string[] = [];
|
||||
filters.forEach((filter) => {
|
||||
if ("field" in filter && filter.field === field) {
|
||||
filterValues.push(...filter.value as string[]);
|
||||
}
|
||||
});
|
||||
return filterValues;
|
||||
export function typeSorters<Obj>(sorters: CrudSort[]): TypedCrudSort<Obj>[] {
|
||||
return sorters as TypedCrudSort<Obj>[]; // <-- Unsafe cast
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user