Skip to content

Commit

Permalink
feat: add single record binding generation (#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
frimfram authored Sep 10, 2021
1 parent b739775 commit 454d754
Showing 1 changed file with 75 additions and 53 deletions.
128 changes: 75 additions & 53 deletions packages/studio-ui-codegen-react/lib/react-studio-template-renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,65 +326,87 @@ export abstract class ReactStudioTemplateRenderer extends StudioTemplateRenderer
const propType = collectionProp[1].type;
const propBindingProp = collectionProp[1].bindingProperties;
const bindingModel = propBindingProp.model;

// create collection items variable declaration
if (propType === 'Data') {
// create filterCriteria declaration
statements.push(
factory.createVariableStatement(
const propStatements = this.buildUseDataStoreBindingCall('collection', propName, bindingModel);
propStatements.forEach((value) => {
statements.push(value);
});
}
});
});

// generate for single record binding
if (component.bindingProperties !== undefined) {
Object.entries(component.bindingProperties).map((compBindingProp) => {
const [compPropName, compBinding] = compBindingProp;
if (isDataPropertyBinding(compBinding) && 'bindingProperties' in compBinding) {
if ('model' in compBinding.bindingProperties && 'predicates' in compBinding.bindingProperties) {
const { model, predicate } = compBinding.bindingProperties;
const moreStatements = this.buildUseDataStoreBindingCall('record', compPropName, model);
moreStatements.forEach((value) => {
statements.push(value);
});
}
}
});
}
return statements;
}

private buildUseDataStoreBindingCall(callType: string, propName: string, bindingModel: string): VariableStatement[] {
const statements: VariableStatement[] = [];
statements.push(
factory.createVariableStatement(
undefined,
factory.createVariableDeclarationList(
[
factory.createVariableDeclaration(
factory.createIdentifier(`filterCriteria${propName}`),
undefined,
factory.createVariableDeclarationList(
[
factory.createVariableDeclaration(
factory.createIdentifier(`filterCriteria${propName}`),
undefined,
undefined,
factory.createArrayLiteralExpression([], false),
),
],
ts.NodeFlags.Const,
),
undefined,
factory.createArrayLiteralExpression([], false),
),
);
const statement = factory.createVariableStatement(
undefined,
factory.createVariableDeclarationList(
[
factory.createVariableDeclaration(
factory.createObjectBindingPattern([
factory.createBindingElement(undefined, undefined, factory.createIdentifier(propName), undefined),
]),
undefined,
undefined,
factory.createCallExpression(factory.createIdentifier('useDataStoreBinding'), undefined, [
factory.createObjectLiteralExpression(
[
factory.createPropertyAssignment(
factory.createIdentifier('type'),
factory.createStringLiteral('collection'),
),
factory.createPropertyAssignment(
factory.createIdentifier('model'),
factory.createIdentifier(bindingModel),
),
factory.createPropertyAssignment(
factory.createIdentifier('criteria'),
factory.createIdentifier(`filterCriteria${propName}`),
),
],
true,
],
ts.NodeFlags.Const,
),
),
);
statements.push(
factory.createVariableStatement(
undefined,
factory.createVariableDeclarationList(
[
factory.createVariableDeclaration(
factory.createObjectBindingPattern([
factory.createBindingElement(undefined, undefined, factory.createIdentifier(propName), undefined),
]),
undefined,
undefined,
factory.createCallExpression(factory.createIdentifier('useDataStoreBinding'), undefined, [
factory.createObjectLiteralExpression(
[
factory.createPropertyAssignment(
factory.createIdentifier('type'),
factory.createStringLiteral(callType),
),
factory.createPropertyAssignment(
factory.createIdentifier('model'),
factory.createIdentifier(bindingModel),
),
factory.createPropertyAssignment(
factory.createIdentifier('criteria'),
factory.createIdentifier(`filterCriteria${propName}`),
),
]),
],
true,
),
],
ts.NodeFlags.Const,
]),
),
);
statements.push(statement);
}
});
});

],
ts.NodeFlags.Const,
),
),
);
return statements;
}

Expand Down

0 comments on commit 454d754

Please sign in to comment.