mirror of
https://github.com/stefanpejcic/openpanel
synced 2025-06-26 18:28:26 +00:00
packages
This commit is contained in:
12
packages/airtable/src/utils/generateFilter.ts
Normal file
12
packages/airtable/src/utils/generateFilter.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import type { CrudFilters } from "@refinedev/core";
|
||||
import { compile } from "@qualifyze/airtable-formulator";
|
||||
import { generateFilterFormula } from "./generateFilterFormula";
|
||||
|
||||
export const generateFilter = (filters?: CrudFilters): string | undefined => {
|
||||
if (filters) {
|
||||
// Top-level array has an implicit AND as per CRUDFilter design - https://refine.dev/docs/guides-and-concepts/data-provider/handling-filters/#logicalfilters
|
||||
return compile(["AND", ...generateFilterFormula(filters)]);
|
||||
}
|
||||
|
||||
return undefined;
|
||||
};
|
||||
17
packages/airtable/src/utils/generateFilterFormula.ts
Normal file
17
packages/airtable/src/utils/generateFilterFormula.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
import type { CrudFilters, LogicalFilter } from "@refinedev/core";
|
||||
import type { Formula } from "@qualifyze/airtable-formulator";
|
||||
import { generateLogicalFilterFormula } from "./generateLogicalFilterFormula";
|
||||
|
||||
export const generateFilterFormula = (filters: CrudFilters): Formula[] => {
|
||||
const compound = filters.map((filter): Formula => {
|
||||
const { operator, value } = filter;
|
||||
|
||||
if (operator === "or") {
|
||||
return ["OR", ...generateFilterFormula(value)];
|
||||
}
|
||||
|
||||
return generateLogicalFilterFormula(filter as LogicalFilter);
|
||||
});
|
||||
|
||||
return compound;
|
||||
};
|
||||
46
packages/airtable/src/utils/generateLogicalFilterFormula.ts
Normal file
46
packages/airtable/src/utils/generateLogicalFilterFormula.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
import type { LogicalFilter } from "@refinedev/core";
|
||||
import { isContainsOperator, isContainssOperator } from "./isContainsOperator";
|
||||
import { isSimpleOperator, simpleOperatorMapping } from "./isSimpleOperator";
|
||||
import type { Formula } from "@qualifyze/airtable-formulator";
|
||||
|
||||
export const generateLogicalFilterFormula = (
|
||||
filter: LogicalFilter,
|
||||
): Formula => {
|
||||
const { field, operator, value } = filter;
|
||||
|
||||
if (isSimpleOperator(operator)) {
|
||||
return [simpleOperatorMapping[operator], { field }, value];
|
||||
}
|
||||
|
||||
if (isContainssOperator(operator)) {
|
||||
const mappedOperator = {
|
||||
containss: "!=",
|
||||
ncontainss: "=",
|
||||
} as const;
|
||||
|
||||
return [mappedOperator[operator], ["FIND", value, { field }], 0];
|
||||
}
|
||||
|
||||
if (isContainsOperator(operator)) {
|
||||
const mappedOperator = {
|
||||
contains: "!=",
|
||||
ncontains: "=",
|
||||
} as const;
|
||||
|
||||
const find = ["FIND", ["LOWER", value], ["LOWER", { field }]] as Formula;
|
||||
|
||||
return [mappedOperator[operator], find, 0];
|
||||
}
|
||||
|
||||
if (operator === "null") {
|
||||
return ["=", { field }, ["BLANK"]];
|
||||
}
|
||||
|
||||
if (operator === "nnull") {
|
||||
return ["!=", { field }, ["BLANK"]];
|
||||
}
|
||||
|
||||
throw Error(
|
||||
`Operator ${operator} is not supported for the Airtable data provider`,
|
||||
);
|
||||
};
|
||||
8
packages/airtable/src/utils/generateSort.ts
Normal file
8
packages/airtable/src/utils/generateSort.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
import type { CrudSorting } from "@refinedev/core";
|
||||
|
||||
export const generateSort = (sorters?: CrudSorting) => {
|
||||
return sorters?.map((item) => ({
|
||||
field: item.field,
|
||||
direction: item.order,
|
||||
}));
|
||||
};
|
||||
6
packages/airtable/src/utils/index.ts
Normal file
6
packages/airtable/src/utils/index.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
export * from "./isSimpleOperator";
|
||||
export * from "./isContainsOperator";
|
||||
export * from "./generateLogicalFilterFormula";
|
||||
export * from "./generateFilterFormula";
|
||||
export * from "./generateFilter";
|
||||
export * from "./generateSort";
|
||||
9
packages/airtable/src/utils/isContainsOperator.ts
Normal file
9
packages/airtable/src/utils/isContainsOperator.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
export const isContainssOperator = (
|
||||
operator: any,
|
||||
): operator is "containss" | "ncontainss" =>
|
||||
["containss", "ncontainss"].includes(operator);
|
||||
|
||||
export const isContainsOperator = (
|
||||
operator: any,
|
||||
): operator is "contains" | "ncontains" =>
|
||||
["contains", "ncontains"].includes(operator);
|
||||
14
packages/airtable/src/utils/isSimpleOperator.ts
Normal file
14
packages/airtable/src/utils/isSimpleOperator.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
export type SimpleOperators = "eq" | "ne" | "lt" | "lte" | "gt" | "gte";
|
||||
import type { OperatorSymbol } from "@qualifyze/airtable-formulator";
|
||||
|
||||
export const simpleOperatorMapping: Record<SimpleOperators, OperatorSymbol> = {
|
||||
eq: "=",
|
||||
ne: "!=",
|
||||
lt: "<",
|
||||
lte: "<=",
|
||||
gt: ">",
|
||||
gte: ">=",
|
||||
} as const;
|
||||
|
||||
export const isSimpleOperator = (operator: any): operator is SimpleOperators =>
|
||||
Object.keys(simpleOperatorMapping).includes(operator);
|
||||
Reference in New Issue
Block a user