Skip to content

Commit

Permalink
add changes
Browse files Browse the repository at this point in the history
  • Loading branch information
vashjs committed Nov 7, 2023
1 parent 9879fa4 commit 5482526
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { QueryBuilderTitle } from '../../QueryBuilderTitle';
import css from '../QueryBuilderModal.css';
import { COLUMN_KEYS } from '../../../../constants/columnKeys';
import { booleanOptions, getFieldOptions, getOperatorOptions, sourceTemplate } from '../../helpers/selectOptions';
import { OPERATORS } from '../../../../constants/operators';
import { BOOLEAN_OPERATORS } from '../../../../constants/operators';
import { DataTypeInput } from '../DataTypeInput';

export const RepeatableFields = ({ source, setSource, getParamsSource, columns }) => {
Expand All @@ -27,7 +27,7 @@ export const RepeatableFields = ({ source, setSource, getParamsSource, columns }
...res,
{
...sourceTemplate(fieldOptions),
[COLUMN_KEYS.BOOLEAN]: { options: booleanOptions, current: OPERATORS.AND },
[COLUMN_KEYS.BOOLEAN]: { options: booleanOptions, current: BOOLEAN_OPERATORS.AND },
},
]));
};
Expand Down
103 changes: 57 additions & 46 deletions src/QueryBuilder/QueryBuilder/helpers/query.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { COLUMN_KEYS } from '../../../constants/columnKeys';
import { valueBuilder } from './valueBuilder';
import { OPERATORS } from '../../../constants/operators';
import { BOOLEAN_OPERATORS, OPERATORS } from '../../../constants/operators';
import { getOperatorOptions } from './selectOptions';

export const DEFAULT_PREVIEW_INTERVAL = 5000;
Expand Down Expand Up @@ -130,6 +130,47 @@ const getSourceFields = (field) => ({
},
}[field]);

const getFormattedSourceField = async ({ item, intl, booleanOptions, fieldOptions, getParamsSource }) => {

Check failure on line 133 in src/QueryBuilder/QueryBuilder/helpers/query.js

View workflow job for this annotation

GitHub Actions / github-actions-ci

Expected to return a value at the end of async arrow function
const [field, query] = Object.entries(item)[0];
const mongoOperator = Object.keys(query)[0];
const mongoValue = query[mongoOperator];

const { operator, value } = getSourceFields(mongoOperator)(mongoValue);

if (operator && value) {
const boolean = BOOLEAN_OPERATORS.AND;
const fieldItem = fieldOptions.find(f => f.value === field);
const { dataType, values, source } = fieldItem;
const hasSourceOrValues = values || source;
let formattedValue;

if (Array.isArray(value) && source) {
const params = await getParamsSource?.({
entityTypeId: source?.entityTypeId,
columnName: source?.columnName,
searchValue: '',
});

formattedValue = value.map(val => params?.content?.find(param => param.value === val));
}

return {
boolean: { options: booleanOptions, current: boolean },
field: { options: fieldOptions, current: field, dataType },
operator: {
dataType,
options: getOperatorOptions({
dataType,
hasSourceOrValues,
intl,
}),
current: operator,
},
value: { current: formattedValue || value, source, options: values },
};
}
};

export const mongoQueryToSource = async ({
initialValues,
booleanOptions = [],
Expand All @@ -139,51 +180,21 @@ export const mongoQueryToSource = async ({
}) => {
if (!fieldOptions?.length) return [];

const target = [];
const andQuery = initialValues.$and || [];

for (const queryObj of andQuery) {
const [field, query] = Object.entries(queryObj)[0];
const mongoOperator = Object.keys(query)[0];
const mongoValue = query[mongoOperator];

const { operator, value } = getSourceFields(mongoOperator)(mongoValue);

if (operator && value) {
const boolean = OPERATORS.AND;
const fieldItem = fieldOptions.find(f => f.value === field);
const { dataType, values, source } = fieldItem;
const hasSourceOrValues = values || source;
let formattedValue;

if (Array.isArray(value) && source) {
const params = await getParamsSource?.({
entityTypeId: source?.entityTypeId,
columnName: source?.columnName,
searchValue: '',
});

formattedValue = value.map(item => params?.content?.find(param => param.value === item));
}

const item = {
boolean: { options: booleanOptions, current: boolean },
field: { options: fieldOptions, current: field, dataType },
operator: {
dataType,
options: getOperatorOptions({
dataType,
hasSourceOrValues,
intl,
}),
current: operator,
},
value: { current: formattedValue || value, source, options: values },
};

target.push(item);
}
const key = Object.keys(initialValues)[0];
const sharedArgs = { intl, booleanOptions, getParamsSource, fieldOptions };

// handle case when query contains boolean operators (AND, OR, etc.)
if (Object.values(BOOLEAN_OPERATORS).includes(key)) {
return initialValues[key].map((item) => getFormattedSourceField({
item,
...sharedArgs,
}));
}

return target;
return [
getFormattedSourceField({
item: initialValues,
...sharedArgs,
}),
];
};
4 changes: 2 additions & 2 deletions src/QueryBuilder/QueryBuilder/helpers/selectOptions.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { DATA_TYPES } from '../../../constants/dataTypes';
import { OPERATORS } from '../../../constants/operators';
import { BOOLEAN_OPERATORS, OPERATORS } from '../../../constants/operators';
import { COLUMN_KEYS } from '../../../constants/columnKeys';

const getOperatorsWithPlaceholder = (options, intl) => {
Expand Down Expand Up @@ -106,7 +106,7 @@ export const getFieldOptions = (options) => {
};

export const booleanOptions = [
{ label: 'AND', value: OPERATORS.AND },
{ label: 'AND', value: BOOLEAN_OPERATORS.AND },
];

export const sourceTemplate = (fieldOptions = []) => ({
Expand Down
3 changes: 3 additions & 0 deletions src/constants/operators.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,8 @@ export const OPERATORS = {
NOT_IN: 'not in',
CONTAINS: 'contains',
STARTS_WITH: 'starts with',
};

export const BOOLEAN_OPERATORS = {
AND: '$and',
};

0 comments on commit 5482526

Please sign in to comment.