From a80f3da040de905016435898726a99c26ab59379 Mon Sep 17 00:00:00 2001 From: Noah Overcash Date: Thu, 22 Feb 2024 17:01:44 -0500 Subject: [PATCH] [UIPQB-85] Use non-id columns for queries --- .../QueryBuilderModal/QueryBuilderModal.js | 10 ++++++-- .../QueryBuilder/helpers/selectOptions.js | 2 +- .../helpers/upgradeInitialValues.js | 24 +++++++++++++++++++ 3 files changed, 33 insertions(+), 3 deletions(-) create mode 100644 src/QueryBuilder/QueryBuilder/helpers/upgradeInitialValues.js diff --git a/src/QueryBuilder/QueryBuilder/QueryBuilderModal/QueryBuilderModal.js b/src/QueryBuilder/QueryBuilder/QueryBuilderModal/QueryBuilderModal.js index 12c083b5..e1c71426 100644 --- a/src/QueryBuilder/QueryBuilder/QueryBuilderModal/QueryBuilderModal.js +++ b/src/QueryBuilder/QueryBuilder/QueryBuilderModal/QueryBuilderModal.js @@ -1,4 +1,4 @@ -import React, { useEffect, useState } from 'react'; +import React, { useEffect, useMemo, useState } from 'react'; import { FormattedMessage, useIntl } from 'react-intl'; import { Modal, @@ -21,12 +21,13 @@ import { useEntityType } from '../../../hooks/useEntityType'; import { useCancelQuery } from '../../../hooks/useCancelQuery'; import { useTestQuery } from '../../../hooks/useTestQuery'; import { getFieldOptions } from '../helpers/selectOptions'; +import upgradeInitialValues from '../helpers/upgradeInitialValues'; export const QueryBuilderModal = ({ isOpen, setIsModalShown, saveBtnLabel, - initialValues, + initialValues: originalInitialValues, entityTypeDataSource, runQueryDataSource, testQueryDataSource, @@ -48,6 +49,11 @@ export const QueryBuilderModal = ({ const { cancelQuery } = useCancelQuery({ cancelQueryDataSource }); + const initialValues = useMemo( + () => upgradeInitialValues(originalInitialValues, entityType), + [originalInitialValues, entityType], + ); + const { source, setSource, diff --git a/src/QueryBuilder/QueryBuilder/helpers/selectOptions.js b/src/QueryBuilder/QueryBuilder/helpers/selectOptions.js index 504fd5af..abeab78c 100644 --- a/src/QueryBuilder/QueryBuilder/helpers/selectOptions.js +++ b/src/QueryBuilder/QueryBuilder/helpers/selectOptions.js @@ -111,7 +111,7 @@ export const getFieldOptions = (options) => { return options?.filter(o => !ids.includes(o.name)).map(o => ({ label: o.labelAlias, - value: o.idColumnName || o.name, + value: o.name, dataType: o.dataType.dataType, source: o.source, values: getFilledValues(o.values), diff --git a/src/QueryBuilder/QueryBuilder/helpers/upgradeInitialValues.js b/src/QueryBuilder/QueryBuilder/helpers/upgradeInitialValues.js new file mode 100644 index 00000000..cfe732fb --- /dev/null +++ b/src/QueryBuilder/QueryBuilder/helpers/upgradeInitialValues.js @@ -0,0 +1,24 @@ +/** + * Upgrades initial values to indirectly reference id columns (e.g. vendor_code instead of vendor_id). + * FQM used to previously require vendor_id, but this was changed in MODFQMMGR-151 to allow for better expression + * and to allow for more flexibility in the future. + */ +export default function upgradeInitialValues(initialValues, entityType) { + const idColumnMapping = {}; + + entityType?.columns.forEach((column) => { + if (column.idColumnName) { + idColumnMapping[column.idColumnName] = column.name; + } + }); + + const upgradedInitialValues = {}; + + Object.keys(initialValues).forEach((key) => { + const newKey = idColumnMapping[key] || key; + + upgradedInitialValues[newKey] = initialValues[key]; + }); + + return upgradedInitialValues; +}