@evoke-platform/ui-components 1.4.0-testing.16 → 1.4.0-testing.17

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.
@@ -2,7 +2,7 @@ import { AddRounded, UnfoldMore } from '@mui/icons-material';
2
2
  import { Typography } from '@mui/material';
3
3
  import { QueryBuilderMaterial } from '@react-querybuilder/material';
4
4
  import { isArray, isEmpty, startCase } from 'lodash';
5
- import React, { useCallback, useEffect, useMemo, useState } from 'react';
5
+ import React, { useEffect, useMemo, useState } from 'react';
6
6
  import { QueryBuilder, RuleGroupBodyComponents, RuleGroupHeaderComponents, TestID, add, defaultRuleProcessorMongoDB, formatQuery, useRuleGroup, } from 'react-querybuilder';
7
7
  import 'react-querybuilder/dist/query-builder.css';
8
8
  import escape from 'string-escape-regex';
@@ -266,9 +266,8 @@ export const valueEditor = (props) => {
266
266
  };
267
267
  const CriteriaBuilder = (props) => {
268
268
  const { properties, criteria, setCriteria, originalCriteria, enablePresetValues, presetValues, operators, disabled, disabledCriteria, hideBorder, presetGroupLabel, customValueEditor, treeViewOpts, disableRegexEscapeChars, } = props;
269
- const [query, setQuery] = useState(undefined);
270
269
  const [propertyTreeMap, setPropertyTreeMap] = useState();
271
- const processRules = useCallback((rules, isSavedValue) => {
270
+ const processRules = (rules, isSavedValue) => {
272
271
  return rules.map((rule) => {
273
272
  if ('rules' in rule) {
274
273
  return {
@@ -295,46 +294,68 @@ const CriteriaBuilder = (props) => {
295
294
  };
296
295
  }
297
296
  });
298
- }, [properties]);
299
- // this retrieves the properties from a treeview for each property in the query
300
- // they are then used in the custom query builder components to determine the input type etc
301
- const updatePropertyTreeMap = useCallback((q) => {
302
- const ids = [];
303
- const traverseRulesForIds = (rules) => {
304
- rules.forEach((rule) => {
305
- if ('rules' in rule) {
306
- traverseRulesForIds(rule.rules);
307
- }
308
- else {
309
- ids.push(rule.field);
310
- }
311
- });
312
- };
313
- traverseRulesForIds(q.rules);
314
- const tempPropertyMap = { ...propertyTreeMap };
315
- ids.forEach(async (id) => {
316
- if (!propertyTreeMap?.[id] && treeViewOpts?.object && treeViewOpts?.fetchObject) {
317
- const prop = await traversePropertyPath(id, treeViewOpts?.object, treeViewOpts?.fetchObject);
318
- if (prop)
319
- tempPropertyMap[id] = prop;
320
- }
321
- setPropertyTreeMap(tempPropertyMap);
322
- });
323
- }, [treeViewOpts, propertyTreeMap]);
297
+ };
324
298
  useEffect(() => {
325
- if (criteria || originalCriteria) {
326
- const criteriaToParse = criteria || originalCriteria || {};
327
- const updatedQuery = parseMongoDB(criteriaToParse);
328
- !isEmpty(treeViewOpts) && updatePropertyTreeMap(updatedQuery);
329
- setQuery({
299
+ if ((criteria || originalCriteria) &&
300
+ !isEmpty(treeViewOpts) &&
301
+ treeViewOpts.object &&
302
+ treeViewOpts.fetchObject) {
303
+ const { object, fetchObject } = treeViewOpts;
304
+ // this retrieves the properties from a treeview for each property in the query
305
+ // they are then used in the custom query builder components to determine the input type etc
306
+ const updatePropertyTreeMap = async () => {
307
+ const newQuery = parseMongoDB(criteria || originalCriteria || {});
308
+ const ids = [];
309
+ const traverseRulesForIds = (rules) => {
310
+ rules.forEach((rule) => {
311
+ if ('rules' in rule) {
312
+ traverseRulesForIds(rule.rules);
313
+ }
314
+ else {
315
+ ids.push(rule.field);
316
+ }
317
+ });
318
+ };
319
+ traverseRulesForIds(newQuery.rules);
320
+ let newPropertyTreeMap = {};
321
+ const newPropertyTreeMapPromises = [];
322
+ for (const id of ids) {
323
+ if (!propertyTreeMap?.[id]) {
324
+ newPropertyTreeMapPromises.push(traversePropertyPath(id, object, fetchObject)
325
+ .then((property) => {
326
+ if (property) {
327
+ return {
328
+ [id]: property,
329
+ };
330
+ }
331
+ return {};
332
+ })
333
+ .catch((err) => {
334
+ console.error(err);
335
+ return {};
336
+ }));
337
+ }
338
+ }
339
+ newPropertyTreeMap = (await Promise.all(newPropertyTreeMapPromises)).reduce((acc, currentProperty) => ({ ...acc, ...currentProperty }), {});
340
+ setPropertyTreeMap((prevPropertyTreeMap) => ({
341
+ ...prevPropertyTreeMap,
342
+ ...newPropertyTreeMap,
343
+ }));
344
+ };
345
+ updatePropertyTreeMap().catch((err) => console.error(err));
346
+ }
347
+ }, [criteria, originalCriteria, treeViewOpts]);
348
+ const initializeQuery = () => {
349
+ const criteriaToParse = criteria || originalCriteria;
350
+ const updatedQuery = criteriaToParse ? parseMongoDB(criteriaToParse || {}) : undefined;
351
+ return updatedQuery
352
+ ? {
330
353
  ...updatedQuery,
331
354
  rules: processRules(updatedQuery.rules, true),
332
- });
333
- }
334
- else {
335
- setQuery({ combinator: 'and', rules: [] });
336
- }
337
- }, [criteria, originalCriteria, treeViewOpts, processRules, updatePropertyTreeMap]);
355
+ }
356
+ : { combinator: 'and', rules: [] };
357
+ };
358
+ const [query, setQuery] = useState(initializeQuery);
338
359
  const handleClearAll = () => {
339
360
  handleQueryChange({ combinator: 'and', rules: [] });
340
361
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@evoke-platform/ui-components",
3
- "version": "1.4.0-testing.16",
3
+ "version": "1.4.0-testing.17",
4
4
  "description": "",
5
5
  "main": "dist/published/index.js",
6
6
  "module": "dist/published/index.js",