Skip to content

Commit

Permalink
Fix dataKey to json conversion in surveys (#1174)
Browse files Browse the repository at this point in the history
  • Loading branch information
dexamundsen authored Feb 18, 2025
1 parent 9765b23 commit 1279cfa
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 28 deletions.
24 changes: 7 additions & 17 deletions ui/src/criteria/classification.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ import {
} from "data/source";
import {
convertAttributeStringToDataValue,
dataKeyToKey,
keyFromDataKey,
DataEntry,
DataKey,
} from "data/types";
Expand Down Expand Up @@ -195,19 +197,6 @@ class _ implements CriteriaPlugin<string> {
}
}

function dataKey(key: DataKey, entityGroup: string): string {
return JSON.stringify({
entityGroup,
stringVal: typeof key === "string" ? key : undefined,
bigIntVal: typeof key === "bigint" ? String(key) : undefined,
});
}

function keyFromDataKey(key: string): DataKey {
const parsed = JSON.parse(key);
return parsed.bigIntVal ? BigInt(parsed.bigIntVal) : parsed.stringVal;
}

type SearchState = {
// The query entered in the search box.
query?: string;
Expand Down Expand Up @@ -335,11 +324,11 @@ function ClassificationEdit(props: ClassificationEditProps) {
const rowData: TreeGridRowData = { ...node.data };
if (node.ancestors) {
rowData.view_hierarchy = node.ancestors.map((a) =>
dataKey(a, entityGroup)
dataKeyToKey(a, entityGroup)
);
}

const key = dataKey(node.data.key, entityGroup);
const key = dataKeyToKey(node.data.key, entityGroup);
children.push(key);

const group = lookupEntityGroupData(
Expand Down Expand Up @@ -990,8 +979,9 @@ function HierarchySearchList(props: HierarchySearchListProps) {
const entityGroup = props.hierarchyEntityConfig?.id;
if (entityGroup) {
props.onClick(
n.ancestors?.map((a) => dataKey(a, entityGroup)) ?? [],
dataKey(n.data.key, entityGroup)
n.ancestors?.map((a) => dataKeyToKey(a, entityGroup)) ??
[],
dataKeyToKey(n.data.key, entityGroup)
);
}
}}
Expand Down
20 changes: 9 additions & 11 deletions ui/src/criteria/survey.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,12 @@ import {
protoFromDataKey,
UnderlaySource,
} from "data/source";
import { compareDataValues, DataEntry, DataKey } from "data/types";
import {
compareDataValues,
dataKeyToKey,
DataEntry,
DataKey,
} from "data/types";
import { useUnderlaySource } from "data/underlaySourceContext";
import { useUpdateCriteria } from "hooks";
import emptyImage from "images/empty.svg";
Expand Down Expand Up @@ -195,13 +200,6 @@ class _ implements CriteriaPlugin<string> {
}
}

function dataKey(key: DataKey, entityGroup: string): string {
return JSON.stringify({
entityGroup,
key,
});
}

type SearchState = {
// The query entered in the search box.
query?: string;
Expand Down Expand Up @@ -283,11 +281,11 @@ function SurveyEdit(props: SurveyEditProps) {
allEntityGroups.forEach(([entityGroup, nodes]) => {
nodes.forEach((node) => {
const rowData: TreeGridRowData = { ...node.data };
const key = dataKey(node.data.key, entityGroup);
const key = dataKeyToKey(node.data.key, entityGroup);

let parentKey = "root";
if (node.ancestors?.length) {
parentKey = dataKey(node.ancestors[0], entityGroup);
parentKey = dataKeyToKey(node.ancestors[0], entityGroup);
}

const cItem: EntityNodeItem = {
Expand Down Expand Up @@ -421,7 +419,7 @@ function SurveyEdit(props: SurveyEditProps) {
if (re.test(String(node.data[k]))) {
matched.add(key);
node.node.ancestors?.forEach((a) =>
ancestors.add(dataKey(a, node.entityGroup))
ancestors.add(dataKeyToKey(a, node.entityGroup))
);
break;
}
Expand Down
13 changes: 13 additions & 0 deletions ui/src/data/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,16 @@ export function convertAttributeStringToDataValue(
`Unknown data type "${attribute.dataType}" in ${JSON.stringify(attribute)}.`
);
}

export function dataKeyToKey(key: DataKey, entityGroup: string): string {
return JSON.stringify({
entityGroup,
stringVal: typeof key === "string" ? key : undefined,
bigIntVal: typeof key === "bigint" ? String(key) : undefined,
});
}

export function keyFromDataKey(key: string): DataKey {
const parsed = JSON.parse(key);
return parsed.bigIntVal ? BigInt(parsed.bigIntVal) : parsed.stringVal;
}

0 comments on commit 1279cfa

Please sign in to comment.