Skip to content

Commit

Permalink
Merge pull request #215 from axonivy/do-not-import-twice
Browse files Browse the repository at this point in the history
XIVY-15691 do not import variable multiple times
  • Loading branch information
ivy-lgi authored Dec 18, 2024
2 parents 7557a2a + 59fdc7f commit 923fd95
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { useAppContext } from '../../../context/AppContext';
import { useMeta } from '../../../context/useMeta';
import { getNode, getNodesOnPath, updateNode, hasChildren as variableHasChildren } from '../../../utils/tree/tree-data';
import { type VariableUpdates } from '../data/variable';
import { findKnownVariable } from '../dialog/known-variables';
import { findVariable } from '../dialog/known-variables';
import './DetailContent.css';
import { Metadata } from './Metadata';
import { Value } from './Value';
Expand All @@ -17,7 +17,7 @@ export const useOverwrites = () => {
return false;
}
const key = getNodesOnPath(variables, selectedVariable).map(node => (node ? node.name : ''));
return findKnownVariable(knownVariables, ...key) !== undefined;
return findVariable(knownVariables, ...key) !== undefined;
};

export const VariablesDetailContent = () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import type { AddNodeReturnType } from '../../../utils/tree/types';
import { validateName, validateNamespace } from '../data/validation-utils';
import { createVariable, type Variable } from '../data/variable';
import './AddDialog.css';
import { addKnownVariable, findKnownVariable } from './known-variables';
import { addKnownVariable, findVariable } from './known-variables';

type AddVariableDialogProps = {
table: Table<Variable>;
Expand Down Expand Up @@ -80,9 +80,9 @@ export const AddVariableDialog = ({ table }: AddVariableDialogProps) => {
return;
}
const namespaceKey = namespace ? namespace.split('.') : [];
const foundKnownVariable = findKnownVariable(knownVariables, ...namespaceKey, name);
const foundKnownVariable = findVariable(knownVariables, ...namespaceKey, name);
if (foundKnownVariable) {
setKnownVariable(foundKnownVariable);
setKnownVariable(foundKnownVariable.node);
event.preventDefault();
return;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { EMPTY_KNOWN_VARIABLES, type KnownVariables, type MetaData } from '@axonivy/variable-editor-protocol';
import type { Variable } from '../data/variable';
import { addKnownVariable, findKnownVariable, toNodes } from './known-variables';
import { addKnownVariable, findVariable, toNodes } from './known-variables';

const knownVariables: KnownVariables = {
namespace: '',
Expand Down Expand Up @@ -79,24 +79,25 @@ test('toNodes', () => {
expect(node.children[3]).toMatchObject({ value: 'File', icon: 'note', info: '' });
});

describe('findKnownVariable', () => {
describe('findVariable', () => {
test('known variables is empty', () => {
const knownVariables = { children: [] as Array<KnownVariables> } as KnownVariables;
expect(findKnownVariable(knownVariables, 'some', 'key')).toBeUndefined();
expect(findVariable(knownVariables, 'some', 'key')).toBeUndefined();
});

test('find folder', () => {
expect(findKnownVariable(knownVariables, 'Amazon', 'Comprehend')).toEqual(knownVariables.children[0].children[0]);
expect(findVariable(knownVariables, 'Amazon', 'Comprehend')).toEqual({ node: knownVariables.children[0].children[0], path: [0, 0] });
});

test('find leaf', () => {
expect(findKnownVariable(knownVariables, 'Amazon', 'Comprehend', 'AccessKey')).toEqual(
knownVariables.children[0].children[0].children[1]
);
expect(findVariable(knownVariables, 'Amazon', 'Comprehend', 'AccessKey')).toEqual({
node: knownVariables.children[0].children[0].children[1],
path: [0, 0, 1]
});
});

test('variable does not exist', () => {
expect(findKnownVariable(knownVariables, 'notFound')).toBeUndefined();
expect(findVariable(knownVariables, 'notFound')).toBeUndefined();
});
});

Expand Down Expand Up @@ -229,4 +230,58 @@ describe('addKnownVariable', () => {
}
]);
});

test('do not add existing variable', () => {
const variables = [{ name: 'Amazon', children: [] as Array<Variable> }] as Array<Variable>;
const originalVariables = structuredClone(variables);
const addNodeReturnValue = addKnownVariable(variables, knownVariables.children[0]);
const newData = addNodeReturnValue.newData;
const newNodePath = addNodeReturnValue.newNodePath;
expect(variables).toEqual(originalVariables);
expect(newData).not.toBe(variables);
expect(newNodePath).toEqual([0]);
expect(newData).toEqual([
{
name: 'Amazon',
children: [
{
name: 'Comprehend',
value: '',
description: 'Amazon comprehend connector settings',
metadata: { type: '' },
children: [
{
name: 'SecretKey',
value: '<YOUR_SECRET_KEY>',
description: 'Secret key to access amazon comprehend',
metadata: { type: 'password' },
children: []
},
{
name: 'AccessKey',
value: '<YOUR_ACCESS_KEY>',
description: 'Access key to access amazon comprehend',
metadata: { type: '' },
children: []
},
{
name: 'Enum',
value: 'two',
description: '',
metadata: { type: 'enum', values: ['one', 'two', 'three'] },
children: []
},
{
name: 'File',
value: '',
description: '',
metadata: { type: 'file', extension: 'json' },
children: []
}
]
}
]
}
]);
});
});
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { BrowserNode } from '@axonivy/ui-components';
import type { KnownVariables } from '@axonivy/variable-editor-protocol';
import { addNode } from '../../../utils/tree/tree-data';
import type { AddNodeReturnType, TreeNode } from '../../../utils/tree/types';
import { isMetadata, type Metadata } from '../data/metadata';
import { createVariable, type Variable } from '../data/variable';
import { nodeIcon } from '../data/variable-utils';
Expand All @@ -25,24 +26,44 @@ const toNode = (node: KnownVariables): BrowserNode => {
};
};

export const findKnownVariable = (node: KnownVariables, ...key: Array<string>) => {
let currentNode: KnownVariables | undefined = node;
export const findVariable = <TNode extends TreeNode<TNode>>(node: TNode, ...key: Array<string>) => {
const path = [];
let currentNode: TNode | undefined = node;
for (const part of key) {
currentNode = currentNode.children.find(child => child.name === part);
if (!currentNode) {
return;
const index: number = currentNode.children.findIndex(child => child.name === part);
if (index === -1) {
return undefined;
}
path.push(index);
currentNode = currentNode.children[index];
}
return currentNode;
return { node: currentNode, path };
};

export const addKnownVariable = (variables: Array<Variable>, node: KnownVariables) => {
export const addKnownVariable = (variables: Array<Variable>, node: KnownVariables): AddNodeReturnType<Variable> => {
const namespaceKey = node.namespace ? node.namespace.split('.') : [];
const foundVariable = findVariable({ name: '', value: '', children: variables }, ...namespaceKey, node.name);
let returnValue;
if (!foundVariable) {
returnValue = addKnown(variables, node);
} else {
returnValue = { newData: variables, newNodePath: foundVariable.path };
}

const newNodePath = returnValue.newNodePath;
for (const child of node.children) {
returnValue = addKnownVariable(returnValue.newData, child);
}
return { newData: returnValue.newData, newNodePath };
};

const addKnown = (variables: Array<Variable>, node: KnownVariables) => {
let metadata: Metadata = { type: '' };
const nodeMetaData = node.metaData;
if (isMetadata(nodeMetaData)) {
metadata = nodeMetaData;
}
let returnValue = addNode(node.name, node.namespace, variables, name => {
return addNode(node.name, node.namespace, variables, name => {
if (name === node.name) {
return {
name,
Expand All @@ -54,9 +75,4 @@ export const addKnownVariable = (variables: Array<Variable>, node: KnownVariable
}
return createVariable(name);
});
const newNodePath = returnValue.newNodePath;
for (const child of node.children) {
returnValue = addKnownVariable(returnValue.newData, child);
}
return { newData: returnValue.newData, newNodePath };
};

0 comments on commit 923fd95

Please sign in to comment.