Skip to content

Commit 4f28d1e

Browse files
committed
3817 - fix ObjectProperty subproperties reordering
1 parent e343d99 commit 4f28d1e

File tree

1 file changed

+119
-52
lines changed

1 file changed

+119
-52
lines changed

client/src/pages/platform/workflow-editor/components/properties/ObjectProperty.tsx

Lines changed: 119 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,22 @@ import Property from './Property';
1616
import DeletePropertyButton from './components/DeletePropertyButton';
1717
import SubPropertyPopover from './components/SubPropertyPopover';
1818

19+
interface BuildOrderedPropertyKeysProps {
20+
dynamicPropertyTypes: Record<string, string> | undefined;
21+
parameterObject: {[key: string]: object[] | undefined};
22+
path: string;
23+
properties: Array<PropertyAllType> | undefined;
24+
}
25+
26+
interface BuildPropertyFromParameterKeyProps {
27+
baseProperty: PropertyAllType;
28+
dynamicPropertyTypes: Record<string, string> | undefined;
29+
parameterKey: string;
30+
parameterObject: {[key: string]: unknown};
31+
path: string;
32+
properties: Array<PropertyAllType>;
33+
}
34+
1935
interface ObjectPropertyProps {
2036
operationName?: string;
2137
arrayIndex?: number;
@@ -130,64 +146,69 @@ const ObjectProperty = ({arrayIndex, arrayName, onDeleteClick, operationName, pa
130146
[onDeleteClick, path]
131147
);
132148

133-
// render individual object items with data gathered from parameters
134-
useEffect(() => {
135-
if (
136-
!name ||
137-
!path ||
138-
!currentComponent?.parameters ||
139-
!properties ||
140-
!parameterObject ||
141-
!isObject(parameterObject)
142-
) {
143-
return;
144-
}
145-
146-
const subPropertyKeySet = new Set<string>();
147-
const dynamicPropertyTypes = currentComponent?.metadata?.ui?.dynamicPropertyTypes;
148-
149-
if (path && dynamicPropertyTypes) {
150-
const pathPrefix = `${path}.`;
149+
const buildOrderedPropertyKeys = useCallback(
150+
({dynamicPropertyTypes, parameterObject, path, properties}: BuildOrderedPropertyKeysProps): string[] => {
151+
const subPropertyKeySet = new Set<string>();
152+
const orderedKeys: string[] = [];
151153

152-
Object.keys(dynamicPropertyTypes).forEach((dynamicKey) => {
153-
if (dynamicKey.startsWith(pathPrefix)) {
154-
const subPropertyName = dynamicKey.substring(pathPrefix.length);
154+
if (properties?.length) {
155+
properties.forEach((property) => {
156+
if (property.name) {
157+
orderedKeys.push(property.name);
155158

156-
if (subPropertyName && !subPropertyName.includes('.')) {
157-
subPropertyKeySet.add(subPropertyName);
159+
subPropertyKeySet.add(property.name);
158160
}
159-
}
160-
});
161-
}
161+
});
162+
}
162163

163-
if (properties?.length) {
164-
properties.forEach((property) => {
165-
if (property.name) {
166-
subPropertyKeySet.add(property.name);
167-
}
168-
});
169-
}
164+
if (path && dynamicPropertyTypes) {
165+
const pathPrefix = `${path}.`;
170166

171-
if (parameterObject) {
172-
Object.keys(parameterObject).forEach((key) => {
173-
subPropertyKeySet.add(key);
174-
});
175-
}
167+
Object.keys(dynamicPropertyTypes).forEach((dynamicKey) => {
168+
if (dynamicKey.startsWith(pathPrefix)) {
169+
const subPropertyName = dynamicKey.substring(pathPrefix.length);
176170

177-
const objectParameterKeys = Array.from(subPropertyKeySet);
171+
if (subPropertyName && !subPropertyName.includes('.')) {
172+
if (!subPropertyKeySet.has(subPropertyName)) {
173+
orderedKeys.push(subPropertyName);
178174

179-
if (!objectParameterKeys.length) {
180-
return;
181-
}
175+
subPropertyKeySet.add(subPropertyName);
176+
}
177+
}
178+
}
179+
});
180+
}
182181

183-
const preexistingProperties = objectParameterKeys.map((parameterKey) => {
184-
const matchingProperty = (properties as Array<PropertyAllType>).find(
185-
(property) => property.name === parameterKey
186-
) as PropertyAllType | undefined;
182+
if (parameterObject) {
183+
Object.keys(parameterObject).forEach((key) => {
184+
if (!subPropertyKeySet.has(key)) {
185+
orderedKeys.push(key);
187186

188-
let parameterItemType = currentComponent.metadata?.ui?.dynamicPropertyTypes?.[`${path}.${parameterKey}`];
187+
subPropertyKeySet.add(key);
188+
}
189+
});
190+
}
189191

190-
const parameterKeyValue = parameterObject[parameterKey!];
192+
return orderedKeys;
193+
},
194+
[]
195+
);
196+
197+
const buildPropertyFromParameterKey = useCallback(
198+
({
199+
baseProperty,
200+
dynamicPropertyTypes,
201+
parameterKey,
202+
parameterObject,
203+
path,
204+
properties,
205+
}: BuildPropertyFromParameterKeyProps): PropertyAllType => {
206+
const matchingProperty = properties.find((property) => property.name === parameterKey) as
207+
| PropertyAllType
208+
| undefined;
209+
210+
let parameterItemType = dynamicPropertyTypes?.[`${path}.${parameterKey}`];
211+
const parameterKeyValue = parameterObject[parameterKey];
191212

192213
if (Array.isArray(parameterKeyValue) && !parameterItemType) {
193214
parameterItemType = 'ARRAY';
@@ -215,10 +236,10 @@ const ObjectProperty = ({arrayIndex, arrayName, onDeleteClick, operationName, pa
215236
controlType: matchingPropertyControlType,
216237
defaultValue: parameterKeyValue,
217238
type: matchingPropertyType,
218-
};
239+
} as PropertyAllType;
219240
} else {
220241
return {
221-
...property,
242+
...baseProperty,
222243
controlType: VALUE_PROPERTY_CONTROL_TYPES[
223244
parameterItemType as keyof typeof VALUE_PROPERTY_CONTROL_TYPES
224245
] as ControlType,
@@ -228,15 +249,61 @@ const ObjectProperty = ({arrayIndex, arrayName, onDeleteClick, operationName, pa
228249
label: parameterKey,
229250
name: parameterKey,
230251
type: parameterItemType as PropertyType,
231-
};
252+
} as PropertyAllType;
232253
}
254+
},
255+
[]
256+
);
257+
258+
// render individual object items with data gathered from parameters
259+
useEffect(() => {
260+
if (
261+
!name ||
262+
!path ||
263+
!currentComponent?.parameters ||
264+
!properties ||
265+
!parameterObject ||
266+
!isObject(parameterObject)
267+
) {
268+
return;
269+
}
270+
271+
const dynamicPropertyTypes = currentComponent?.metadata?.ui?.dynamicPropertyTypes;
272+
273+
const objectParameterKeys = buildOrderedPropertyKeys({
274+
dynamicPropertyTypes,
275+
parameterObject,
276+
path,
277+
properties: properties as Array<PropertyAllType>,
233278
});
234279

280+
if (!objectParameterKeys.length) {
281+
return;
282+
}
283+
284+
const preexistingProperties = objectParameterKeys.map((parameterKey) =>
285+
buildPropertyFromParameterKey({
286+
baseProperty: property,
287+
dynamicPropertyTypes,
288+
parameterKey,
289+
parameterObject,
290+
path,
291+
properties: properties as Array<PropertyAllType>,
292+
})
293+
);
294+
235295
if (preexistingProperties.length) {
236296
setSubProperties(preexistingProperties as Array<PropertyAllType>);
237297
}
238298
// eslint-disable-next-line react-hooks/exhaustive-deps
239-
}, [parameterObject, properties, path, currentComponent?.metadata?.ui?.dynamicPropertyTypes]);
299+
}, [
300+
parameterObject,
301+
properties,
302+
path,
303+
currentComponent?.metadata?.ui?.dynamicPropertyTypes,
304+
buildOrderedPropertyKeys,
305+
buildPropertyFromParameterKey,
306+
]);
240307

241308
// set default values for subProperties when they are created
242309
useEffect(() => {

0 commit comments

Comments
 (0)