@teselagen/ui 0.8.4 → 0.8.6-beta.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,275 @@
1
+ import { camelCase, set } from "lodash-es";
2
+
3
+ export function tableQueryParamsToHasuraClauses({
4
+ page,
5
+ pageSize,
6
+ searchTerm,
7
+ filters,
8
+ order,
9
+ schema, // Add schema as a parameter
10
+ additionalFilter
11
+ }) {
12
+ const ccFields = getFieldsMappedByCCDisplayName(schema);
13
+ let where = {};
14
+ const order_by = [];
15
+ const limit = pageSize || 25;
16
+ const offset = page && pageSize ? (page - 1) * pageSize : 0;
17
+
18
+ if (searchTerm) {
19
+ const searchTermFilters = [];
20
+ // Create a map to deduplicate fields by path
21
+ const uniqueFieldsByPath = {};
22
+
23
+ schema.fields.forEach(field => {
24
+ const { type, path, searchDisabled } = field;
25
+ if (uniqueFieldsByPath[path]) return; // Skip if already added
26
+ uniqueFieldsByPath[path] = true;
27
+ if (searchDisabled || field.filterDisabled || type === "color") return;
28
+ const filterValue = searchTerm; // No cleaning needed here, we're using _ilike
29
+
30
+ if (type === "string" || type === "lookup") {
31
+ const o = set({}, path, { _ilike: `%${filterValue}%` });
32
+ searchTermFilters.push(o);
33
+ } else if (type === "boolean") {
34
+ let regex;
35
+ try {
36
+ regex = new RegExp("^" + searchTerm, "ig");
37
+ } catch (error) {
38
+ //ignore
39
+ }
40
+ if (regex) {
41
+ if ("true".replace(regex, "") !== "true") {
42
+ const o = set({}, path, { _eq: true });
43
+ searchTermFilters.push(o);
44
+ } else if ("false".replace(regex, "") !== "false") {
45
+ const o = set({}, path, { _eq: false });
46
+ searchTermFilters.push(o);
47
+ }
48
+ }
49
+ } else if (
50
+ (type === "number" || type === "integer") &&
51
+ !isNaN(filterValue)
52
+ ) {
53
+ const o = set({}, path, { _eq: parseFloat(filterValue) });
54
+ searchTermFilters.push(o);
55
+ }
56
+ });
57
+
58
+ if (searchTermFilters.length > 0) {
59
+ if (Object.keys(where).length > 0) {
60
+ where = { _and: [where, { _or: searchTermFilters }] };
61
+ } else {
62
+ where = { _or: searchTermFilters };
63
+ }
64
+ }
65
+ }
66
+
67
+ if (filters && filters.length > 0) {
68
+ const filterClauses = filters
69
+ .map(filter => {
70
+ let { selectedFilter, filterOn, filterValue } = filter;
71
+ console.log('filterOn:', filterOn)
72
+ const fieldSchema = ccFields[filterOn] || {};
73
+
74
+ const { path, reference, type, customColumnFilter } = fieldSchema;
75
+ console.log('customColumnFilter:', customColumnFilter)
76
+ if (customColumnFilter) {
77
+ return customColumnFilter(filterValue);
78
+ }
79
+ let stringFilterValue =
80
+ filterValue && filterValue.toString
81
+ ? filterValue.toString()
82
+ : filterValue;
83
+ if (stringFilterValue === false) {
84
+ // we still want to be able to search for the string "false" which will get parsed to false
85
+ stringFilterValue = "false";
86
+ } else {
87
+ stringFilterValue = stringFilterValue || "";
88
+ }
89
+ const arrayFilterValue = Array.isArray(filterValue)
90
+ ? filterValue
91
+ : stringFilterValue.split(";");
92
+
93
+ if (type === "number" || type === "integer") {
94
+ filterValue = Array.isArray(filterValue)
95
+ ? filterValue.map(val => Number(val))
96
+ : Number(filterValue);
97
+ }
98
+
99
+ if (fieldSchema.normalizeFilter) {
100
+ filterValue = fieldSchema.normalizeFilter(
101
+ filterValue,
102
+ selectedFilter,
103
+ filterOn
104
+ );
105
+ }
106
+
107
+ if (reference) {
108
+ filterOn = reference.sourceField;
109
+ } else {
110
+ filterOn = path || filterOn;
111
+ }
112
+ switch (selectedFilter) {
113
+ case "none":
114
+ return {};
115
+ case "startsWith":
116
+ return { [filterOn]: { _ilike: `${filterValue}%` } };
117
+ case "endsWith":
118
+ return { [filterOn]: { _ilike: `%${filterValue}` } };
119
+ case "contains":
120
+ return { [filterOn]: { _ilike: `%${filterValue}%` } };
121
+ case "notContains":
122
+ return { [filterOn]: { _nilike: `%${filterValue}%` } };
123
+ case "isExactly":
124
+ return { [filterOn]: { _eq: filterValue } };
125
+ case "isEmpty":
126
+ if (filterOn.includes(".")) {
127
+ // if we're filtering on a nested field, like a sequence table with parts.name
128
+ // we really want to just query on the top level field's existence
129
+ return {
130
+ _not: {
131
+ [filterOn.split(".")[0]]: {}
132
+ }
133
+ };
134
+ }
135
+ return {
136
+ _or: [
137
+ { [filterOn]: { _eq: "" } },
138
+ { [filterOn]: { _is_null: true } }
139
+ ]
140
+ };
141
+ case "notEmpty":
142
+ return {
143
+ _and: [
144
+ { [filterOn]: { _neq: "" } },
145
+ { [filterOn]: { _is_null: false } }
146
+ ]
147
+ };
148
+ case "inList":
149
+ return { [filterOn]: { _in: filterValue } };
150
+ case "notInList":
151
+ return { [filterOn]: { _nin: filterValue } };
152
+ case "true":
153
+ return { [filterOn]: { _eq: true } };
154
+ case "false":
155
+ return { [filterOn]: { _eq: false } };
156
+ case "dateIs":
157
+ return { [filterOn]: { _eq: filterValue } };
158
+ case "notBetween":
159
+ return {
160
+ _or: [
161
+ {
162
+ [filterOn]: {
163
+ _lt: new Date(arrayFilterValue[0])
164
+ }
165
+ },
166
+ {
167
+ [filterOn]: {
168
+ _gt: new Date(
169
+ new Date(arrayFilterValue[1]).setHours(23, 59)
170
+ )
171
+ }
172
+ }
173
+ ]
174
+ };
175
+ case "isBetween":
176
+ return {
177
+ [filterOn]: {
178
+ _gte: new Date(arrayFilterValue[0]),
179
+ _lte: new Date(new Date(arrayFilterValue[1]).setHours(23, 59))
180
+ }
181
+ };
182
+ case "isBefore":
183
+ return { [filterOn]: { _lt: new Date(filterValue) } };
184
+ case "isAfter":
185
+ return { [filterOn]: { _gt: new Date(filterValue) } };
186
+ case "greaterThan":
187
+ return { [filterOn]: { _gt: parseFloat(filterValue) } };
188
+ case "lessThan":
189
+ return { [filterOn]: { _lt: parseFloat(filterValue) } };
190
+ case "inRange":
191
+ return {
192
+ [filterOn]: {
193
+ _gte: parseFloat(arrayFilterValue[0]),
194
+ _lte: parseFloat(arrayFilterValue[1])
195
+ }
196
+ };
197
+ case "outsideRange":
198
+ return {
199
+ _or: [
200
+ {
201
+ [filterOn]: {
202
+ _lt: parseFloat(arrayFilterValue[0])
203
+ }
204
+ },
205
+ {
206
+ [filterOn]: {
207
+ _gt: parseFloat(arrayFilterValue[1])
208
+ }
209
+ }
210
+ ]
211
+ };
212
+ case "equalTo":
213
+ return {
214
+ [filterOn]: {
215
+ _eq:
216
+ type === "number" || type === "integer"
217
+ ? parseFloat(filterValue)
218
+ : filterValue
219
+ }
220
+ };
221
+ case "regex":
222
+ return { [filterOn]: { _regex: filterValue } };
223
+ default:
224
+ console.warn(`Unsupported filter type: ${selectedFilter}`);
225
+ return {};
226
+ }
227
+ })
228
+
229
+ if (filterClauses.length > 0) {
230
+ if (Object.keys(where).length > 0) {
231
+ where = { _and: [where, ...filterClauses] };
232
+ } else {
233
+ where = { _and: filterClauses };
234
+ }
235
+ }
236
+ }
237
+
238
+ if (order && order.length > 0) {
239
+ order.forEach(item => {
240
+ const field = item.startsWith("-") ? item.substring(1) : item;
241
+ const direction = item.startsWith("-") ? "desc" : "asc";
242
+ order_by.push({ [field]: direction });
243
+ });
244
+ }
245
+
246
+ if (additionalFilter) {
247
+ where = { _and: [where, additionalFilter] };
248
+ }
249
+ return { where, order_by, limit, offset };
250
+ }
251
+
252
+ /**
253
+ * Takes a schema and returns an object with the fields mapped by their camelCased display name.
254
+ * If the displayName is not set or is a jsx element, the path is used instead.
255
+ * The same conversion must be done when using the result of this method
256
+ */
257
+ export function getFieldsMappedByCCDisplayName(schema) {
258
+ if (!schema || !schema.fields) return {};
259
+ return schema.fields.reduce((acc, field) => {
260
+ const ccDisplayName = getCCDisplayName(field);
261
+ acc[ccDisplayName] = field;
262
+ return acc;
263
+ }, {});
264
+ }
265
+
266
+ /**
267
+ *
268
+ * @param {object} field
269
+ * @returns the camelCase display name of the field, to be used for filters, sorting, etc
270
+ */
271
+ export function getCCDisplayName(field) {
272
+ return camelCase(
273
+ typeof field.displayName === "string" ? field.displayName : field.path
274
+ );
275
+ }
@@ -0,0 +1,226 @@
1
+ import { tableQueryParamsToHasuraClauses } from "./tableQueryParamsToHasuraClauses";
2
+
3
+ describe("tableQueryParamsToHasuraClauses", () => {
4
+ const schema = {
5
+ fields: [
6
+ { path: "name", type: "string" },
7
+ { path: "age", type: "number" },
8
+ { path: "isActive", type: "boolean" },
9
+ { path: "email", type: "string" }
10
+ ]
11
+ };
12
+
13
+ it("should handle empty query params", () => {
14
+ const result = tableQueryParamsToHasuraClauses({});
15
+ expect(result).toEqual({
16
+ where: {},
17
+ order_by: [],
18
+ limit: 25,
19
+ offset: 0
20
+ });
21
+ });
22
+
23
+ it("should handle page and pageSize", () => {
24
+ const result = tableQueryParamsToHasuraClauses({ page: 2, pageSize: 10 });
25
+ expect(result).toEqual({
26
+ where: {},
27
+ order_by: [],
28
+ limit: 10,
29
+ offset: 10
30
+ });
31
+ });
32
+
33
+ it("should handle searchTerm with string fields", () => {
34
+ const result = tableQueryParamsToHasuraClauses({
35
+ searchTerm: "test",
36
+ schema
37
+ });
38
+ expect(result).toEqual({
39
+ where: {
40
+ _or: [{ name: { _ilike: "%test%" } }, { email: { _ilike: "%test%" } }]
41
+ },
42
+ order_by: [],
43
+ limit: 25,
44
+ offset: 0
45
+ });
46
+ });
47
+ it("should flatten queries with dup paths", () => {
48
+ const result = tableQueryParamsToHasuraClauses({
49
+ searchTerm: "test",
50
+ schema: {
51
+ fields: [
52
+ ...schema.fields,
53
+ { path: "name", type: "string" },
54
+ { path: "name", type: "string" }
55
+ ]
56
+ }
57
+ });
58
+ expect(result).toEqual({
59
+ where: {
60
+ _or: [{ name: { _ilike: "%test%" } }, { email: { _ilike: "%test%" } }]
61
+ },
62
+ order_by: [],
63
+ limit: 25,
64
+ offset: 0
65
+ });
66
+ });
67
+
68
+ it("should handle searchTerm with number fields", () => {
69
+ const result = tableQueryParamsToHasuraClauses({
70
+ searchTerm: "30",
71
+ schema
72
+ });
73
+ expect(result).toEqual({
74
+ where: {
75
+ _or: [
76
+ { name: { _ilike: "%30%" } },
77
+ { age: { _eq: 30 } },
78
+ { email: { _ilike: "%30%" } }
79
+ ]
80
+ },
81
+ order_by: [],
82
+ limit: 25,
83
+ offset: 0
84
+ });
85
+ });
86
+
87
+ it("should handle searchTerm with boolean fields", () => {
88
+ const result = tableQueryParamsToHasuraClauses({
89
+ searchTerm: "true",
90
+ schema
91
+ });
92
+ expect(result).toEqual({
93
+ where: {
94
+ _or: [
95
+ { name: { _ilike: "%true%" } },
96
+ { isActive: { _eq: true } },
97
+ { email: { _ilike: "%true%" } }
98
+ ]
99
+ },
100
+ order_by: [],
101
+ limit: 25,
102
+ offset: 0
103
+ });
104
+ });
105
+
106
+ it("should handle searchTerm with multiple field types", () => {
107
+ const result = tableQueryParamsToHasuraClauses({
108
+ searchTerm: "test",
109
+ schema
110
+ });
111
+ expect(result).toEqual({
112
+ where: {
113
+ _or: [{ name: { _ilike: "%test%" } }, { email: { _ilike: "%test%" } }]
114
+ },
115
+ order_by: [],
116
+ limit: 25,
117
+ offset: 0
118
+ });
119
+ });
120
+
121
+ it("should handle contains filter", () => {
122
+ const result = tableQueryParamsToHasuraClauses({
123
+ filters: [
124
+ {
125
+ selectedFilter: "contains",
126
+ filterOn: "name",
127
+ filterValue: "test"
128
+ }
129
+ ]
130
+ });
131
+ expect(result).toEqual({
132
+ where: { _and: [{ name: { _ilike: "%test%" } }] },
133
+ order_by: [],
134
+ limit: 25,
135
+ offset: 0
136
+ });
137
+ });
138
+
139
+ it("should handle equalTo filter for number", () => {
140
+ const result = tableQueryParamsToHasuraClauses({
141
+ filters: [
142
+ { selectedFilter: "equalTo", filterOn: "age", filterValue: "30" }
143
+ ],
144
+ schema
145
+ });
146
+ expect(result).toEqual({
147
+ where: { _and: [{ age: { _eq: 30 } }] },
148
+ order_by: [],
149
+ limit: 25,
150
+ offset: 0
151
+ });
152
+ });
153
+
154
+ it("should handle order", () => {
155
+ const result = tableQueryParamsToHasuraClauses({ order: ["name", "-age"] });
156
+ expect(result).toEqual({
157
+ where: {},
158
+ order_by: [{ name: "asc" }, { age: "desc" }],
159
+ limit: 25,
160
+ offset: 0
161
+ });
162
+ });
163
+
164
+ it("should combine all params", () => {
165
+ const result = tableQueryParamsToHasuraClauses({
166
+ page: 2,
167
+ pageSize: 10,
168
+ searchTerm: "test",
169
+ filters: [
170
+ {
171
+ selectedFilter: "greaterThan",
172
+ filterOn: "age",
173
+ filterValue: "30"
174
+ }
175
+ ],
176
+ order: ["name"],
177
+ schema
178
+ });
179
+ expect(result).toEqual({
180
+ where: {
181
+ _and: [
182
+ {
183
+ _or: [
184
+ { name: { _ilike: "%test%" } },
185
+ { email: { _ilike: "%test%" } }
186
+ ]
187
+ },
188
+ { age: { _gt: 30 } }
189
+ ]
190
+ },
191
+ order_by: [{ name: "asc" }],
192
+ limit: 10,
193
+ offset: 10
194
+ });
195
+ });
196
+
197
+ it("should combine searchTerm and filters", () => {
198
+ const result = tableQueryParamsToHasuraClauses({
199
+ searchTerm: "test",
200
+ filters: [
201
+ {
202
+ selectedFilter: "greaterThan",
203
+ filterOn: "age",
204
+ filterValue: "30"
205
+ }
206
+ ],
207
+ schema
208
+ });
209
+ expect(result).toEqual({
210
+ where: {
211
+ _and: [
212
+ {
213
+ _or: [
214
+ { name: { _ilike: "%test%" } },
215
+ { email: { _ilike: "%test%" } }
216
+ ]
217
+ },
218
+ { age: { _gt: 30 } }
219
+ ]
220
+ },
221
+ order_by: [],
222
+ limit: 25,
223
+ offset: 0
224
+ });
225
+ });
226
+ });
@@ -6,13 +6,13 @@ import {
6
6
  makeDataTableHandlers,
7
7
  getQueryParams,
8
8
  setCurrentParamsOnUrl,
9
- getCurrentParamsFromUrl,
10
- getCCDisplayName
9
+ getCurrentParamsFromUrl
11
10
  } from "./queryParams";
12
11
  import { withRouter } from "react-router-dom";
13
12
  import getTableConfigFromStorage from "./getTableConfigFromStorage";
14
13
  import { useDeepEqualMemo } from "../../utils/hooks/useDeepEqualMemo";
15
14
  import { branch, compose } from "recompose";
15
+ import { getCCDisplayName } from "./tableQueryParamsToHasuraClauses";
16
16
 
17
17
  /**
18
18
  * Note all these options can be passed at Design Time or at Runtime (like reduxForm())
@@ -32,7 +32,6 @@ import { branch, compose } from "recompose";
32
32
  export const useTableParams = props => {
33
33
  const {
34
34
  additionalFilter,
35
- additionalOrFilter,
36
35
  controlled_pageSize,
37
36
  defaults: _defaults,
38
37
  doNotCoercePageSize,
@@ -166,16 +165,6 @@ export const useTableParams = props => {
166
165
  );
167
166
 
168
167
  const queryParams = useMemo(() => {
169
- const additionalFilterToUse =
170
- typeof additionalFilter === "function"
171
- ? additionalFilter
172
- : () => additionalFilter;
173
-
174
- const additionalOrFilterToUse =
175
- typeof additionalOrFilter === "function"
176
- ? additionalOrFilter
177
- : () => additionalOrFilter;
178
-
179
168
  return getQueryParams({
180
169
  doNotCoercePageSize,
181
170
  currentParams,
@@ -185,8 +174,7 @@ export const useTableParams = props => {
185
174
  schema: convertedSchema,
186
175
  isInfinite: isInfinite || (isSimple && !withPaging),
187
176
  isLocalCall,
188
- additionalFilter: additionalFilterToUse,
189
- additionalOrFilter: additionalOrFilterToUse,
177
+ additionalFilter,
190
178
  noOrderError,
191
179
  isCodeModel,
192
180
  ownProps: passingProps
@@ -194,7 +182,6 @@ export const useTableParams = props => {
194
182
  }, [
195
183
  additionalFilter,
196
184
  passingProps,
197
- additionalOrFilter,
198
185
  doNotCoercePageSize,
199
186
  currentParams,
200
187
  entities,
@@ -788,7 +788,11 @@ const Uploader = ({
788
788
  .join(", ")
789
789
  : undefined
790
790
  }
791
- onDrop={async (_acceptedFiles, rejectedFiles) => {
791
+ onDrop={async (_acceptedFiles, rejectedFiles, e) => {
792
+ const parentDropzone = e.target.closest(".tg-dropzone");
793
+ if (parentDropzone) {
794
+ parentDropzone.blur();
795
+ }
792
796
  let acceptedFiles = [];
793
797
  for (const file of _acceptedFiles) {
794
798
  if ((validateAgainstSchema || autoUnzip) && isZipFile(file)) {
@@ -114,14 +114,14 @@ function removeUnwantedProps(props) {
114
114
 
115
115
  const LabelWithTooltipInfo = ({ label, tooltipInfo, labelStyle }) =>
116
116
  tooltipInfo ? (
117
- <div style={{ display: "flex", alignItems: "center", ...labelStyle }}>
117
+ <span style={{ display: "flex", alignItems: "center", ...labelStyle }}>
118
118
  {label}{" "}
119
119
  <InfoHelper
120
120
  style={{ marginLeft: "5px", marginTop: "-6px" }}
121
121
  size={12}
122
122
  content={tooltipInfo}
123
123
  />
124
- </div>
124
+ </span>
125
125
  ) : (
126
126
  label || null
127
127
  );