Browse Source

Update resolveCollectionVariable (#6670)

IhorKaleniuk666-fix/id-conflict
Artur Arseniev 2 months ago
committed by GitHub
parent
commit
8cf995e4d5
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 40
      packages/core/src/data_sources/model/DataVariable.ts

40
packages/core/src/data_sources/model/DataVariable.ts

@ -150,41 +150,13 @@ export default class DataVariable extends Model<DataVariableProps> {
ctx: DataVariableOptions,
) {
const { collectionId = '', variableType, path, defaultValue = '' } = params;
const { em, collectionsStateMap } = ctx;
const { collectionsStateMap, em } = ctx;
const collectionItemState = collectionsStateMap?.[collectionId] as DataCollectionState | undefined;
if (!collectionsStateMap) return defaultValue;
if (!collectionItemState || !variableType) return defaultValue;
const collectionItem = collectionsStateMap[collectionId];
if (!collectionItem) return defaultValue;
if (!variableType) {
em.logError(`Missing collection variable type for collection: ${collectionId}`);
return defaultValue;
}
if (variableType === 'currentItem') {
return DataVariable.resolveCurrentItem(collectionItem as DataCollectionState, path) ?? defaultValue;
}
const state = collectionItem as DataCollectionState;
return state[variableType] ?? defaultValue;
}
private static resolveCurrentItem(collectionItem: DataCollectionState, path: string | undefined) {
const currentItem = collectionItem.currentItem;
if (!currentItem) {
return;
}
if (currentItem.type === DataVariableType) {
const resolvedPath = currentItem.path ? `${currentItem.path}.${path}` : path;
return { type: DataVariableType, path: resolvedPath };
}
if (path && !(currentItem as any)[path]) {
return;
}
return path ? (currentItem as any)[path] : currentItem;
return em.DataSources.getValue(`${variableType}${path ? `.${path}` : ''}`, defaultValue, {
context: collectionItemState,
});
}
}

Loading…
Cancel
Save