Skip to content

Commit

Permalink
[UIPQB-85] Use non-id columns for queries
Browse files Browse the repository at this point in the history
  • Loading branch information
ncovercash committed Feb 22, 2024
1 parent 4330ef4 commit a80f3da
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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,
Expand All @@ -48,6 +49,11 @@ export const QueryBuilderModal = ({

const { cancelQuery } = useCancelQuery({ cancelQueryDataSource });

const initialValues = useMemo(
() => upgradeInitialValues(originalInitialValues, entityType),
[originalInitialValues, entityType],
);

const {
source,
setSource,
Expand Down
2 changes: 1 addition & 1 deletion src/QueryBuilder/QueryBuilder/helpers/selectOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
24 changes: 24 additions & 0 deletions src/QueryBuilder/QueryBuilder/helpers/upgradeInitialValues.js
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit a80f3da

Please sign in to comment.