Skip to content

Commit

Permalink
fix: only use useDataStoreBinding when predicate is set (#86)
Browse files Browse the repository at this point in the history
Resolves #84
  • Loading branch information
dpilch committed Sep 17, 2021
1 parent d0998bc commit ec079f1
Show file tree
Hide file tree
Showing 5 changed files with 177 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -215,11 +215,6 @@ export default function ComponentWithDataBinding(
props: ComponentWithDataBindingProps
): JSX.Element {
const { width, isDisabled, buttonUser, buttonColor } = props;
const { buttonUser } = useDataStoreBinding({
type: \\"record\\",
model: User,
criteria: buttonUserFilter,
});
return (
<Button
label={buttonUser.username || \\"hspain@gmail.com\\"}
Expand All @@ -233,6 +228,67 @@ export default function ComponentWithDataBinding(
"
`;

exports[`amplify render tests component with data binding should not have useDataStoreBinding when there is no predicate 1`] = `
"/* eslint-disable */
import React from \\"react\\";
import { UntitledModel } from \\"../models\\";
import {
EscapeHatchProps,
Flex,
Text,
getOverrideProps,
} from \\"@aws-amplify/ui-react\\";
export type SectionHeadingProps = {
newProp6fd1?: UntitledModel,
} & {
overrides?: EscapeHatchProps | undefined | null,
};
export default function SectionHeading(
props: SectionHeadingProps
): JSX.Element {
const { newProp6fd1 } = props;
return (
<Flex
padding=\\"0px 0px 10px 0px\\"
backgroundColor=\\"rgb(255,255,255)\\"
direction=\\"column\\"
{...props}
{...getOverrideProps(props.overrides, \\"Flex\\")}
>
<Flex
tes={newProp6fd1.untitledfield}
{...getOverrideProps(props.overrides, \\"Flex.Flex\\")}
>
<Text
fontFamily=\\"Inter\\"
color=\\"rgb(15.539060980081558,29.73937589675188,47.8125)\\"
textAlign=\\"left\\"
fontSize=\\"48px\\"
lineHeight=\\"48px\\"
fontWeight=\\"300\\"
{...getOverrideProps(props.overrides, \\"Flex.Flex.Text\\")}
>
Heading 2
</Text>
</Flex>
<Text
fontFamily=\\"Inter\\"
color=\\"rgb(0,0,0)\\"
textAlign=\\"left\\"
fontSize=\\"32px\\"
lineHeight=\\"48px\\"
fontWeight=\\"400\\"
{...getOverrideProps(props.overrides, \\"Flex.Text\\")}
>
subtitle
</Text>
</Flex>
);
}
"
`;

exports[`amplify render tests component with data binding should render collection data binding 1`] = `
"/* eslint-disable */
import React from \\"react\\";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ describe('amplify render tests', () => {
const generatedCode = generateWithAmplifyRenderer('collectionWithBinding');
expect(generatedCode).toMatchSnapshot();
});

it('should not have useDataStoreBinding when there is no predicate', () => {
const generatedCode = generateWithAmplifyRenderer('dataBindingWithoutPredicate');
expect(generatedCode).toMatchSnapshot();
});
});

describe('component with binding', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
{
"sourceId": "372:15567",
"componentType": "Flex",
"bindingProperties": {
"newProp6fd1": {
"type": "Data",
"bindingProperties": {
"model": "UntitledModel"
}
}
},
"children": [
{
"name": "Heading",
"componentType": "Flex",
"children": [
{
"name": "Heading 2",
"componentType": "Text",
"children": [],
"properties": {
"fontFamily": {
"value": "Inter"
},
"color": {
"value": "rgb(15.539060980081558,29.73937589675188,47.8125)"
},
"textAlign": {
"value": "left"
},
"fontSize": {
"value": "48px"
},
"lineHeight": {
"value": "48px"
},
"value": {
"value": "Heading 2"
},
"fontWeight": {
"value": "300"
}
}
}
],
"properties": {
"tes": {
"bindingProperties": {
"property": "newProp6fd1.untitledfield"
}
}
}
},
{
"name": "subtitle",
"componentType": "Text",
"children": [],
"properties": {
"fontFamily": {
"value": "Inter"
},
"color": {
"value": "rgb(0,0,0)"
},
"textAlign": {
"value": "left"
},
"fontSize": {
"value": "32px"
},
"lineHeight": {
"value": "48px"
},
"value": {
"value": "subtitle"
},
"fontWeight": {
"value": "400"
}
}
}
],
"name": "SectionHeading",
"id": "c-b1fYXAcpNYHNt0jbZ4",
"overrides": {},
"variants": [],
"properties": {
"padding": {
"value": "0px 0px 10px 0px"
},
"backgroundColor": {
"value": "rgb(255,255,255)"
},
"direction": {
"value": "column"
}
},
"appId": "d1rxgz43y8l9ij",
"environmentName": "staging"
}
Original file line number Diff line number Diff line change
Expand Up @@ -387,16 +387,14 @@ export abstract class ReactStudioTemplateRenderer extends StudioTemplateRenderer
const [propName, binding] = collectionProp;
if (isDataPropertyBinding(binding)) {
const { bindingProperties } = binding;
const { predicate } = bindingProperties;
if ('predicate' in bindingProperties && bindingProperties.predicate !== undefined) {
statements.push(this.buildPredicateDeclaration(propName, bindingProperties.predicate));
const { model } = bindingProperties;
this.importCollection.addImport('../models', model);
this.buildUseDataStoreBindingCall('collection', propName, model).forEach((value) => {
statements.push(value);
});
}

const { model } = bindingProperties;
this.importCollection.addImport('../models', model);
this.buildUseDataStoreBindingCall('collection', propName, model).forEach((value) => {
statements.push(value);
});
}
});
}
Expand All @@ -408,15 +406,14 @@ export abstract class ReactStudioTemplateRenderer extends StudioTemplateRenderer
const [propName, binding] = compBindingProp;
if (isDataPropertyBinding(binding)) {
const { bindingProperties } = binding;
const { predicate } = bindingProperties;
if ('predicate' in bindingProperties && bindingProperties.predicate !== undefined) {
statements.push(this.buildPredicateDeclaration(propName, bindingProperties.predicate));
const { model } = bindingProperties;
this.importCollection.addImport('../models', model);
this.buildUseDataStoreBindingCall('record', propName, model).forEach((value) => {
statements.push(value);
});
}

const { model } = bindingProperties;
this.buildUseDataStoreBindingCall('record', propName, model).forEach((value) => {
statements.push(value);
});
}
});
}
Expand Down
1 change: 1 addition & 0 deletions tmp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"sourceId":"372:15567","componentType":"Flex","bindingProperties":{"newProp6fd1":{"type":"Data","bindingProperties":{"model":"UntitledModel"}}},"children":[{"name":"Heading","componentType":"Flex","children":[{"name":"Heading 2","componentType":"Text","children":[],"properties":{"fontFamily":{"value":"Inter"},"color":{"value":"rgb(15.539060980081558,29.73937589675188,47.8125)"},"textAlign":{"value":"left"},"fontSize":{"value":"48px"},"lineHeight":{"value":"48px"},"value":{"value":"Heading 2"},"fontWeight":{"value":"300"}}}],"properties":{"tes":{"bindingProperties":{"property":"newProp6fd1.untitledfield"}}}},{"name":"subtitle","componentType":"Text","children":[],"properties":{"fontFamily":{"value":"Inter"},"color":{"value":"rgb(0,0,0)"},"textAlign":{"value":"left"},"fontSize":{"value":"32px"},"lineHeight":{"value":"48px"},"value":{"value":"subtitle"},"fontWeight":{"value":"400"}}}],"name":"SectionHeading","id":"c-b1fYXAcpNYHNt0jbZ4","overrides":{},"variants":[],"properties":{"padding":{"value":"0px 0px 10px 0px"},"backgroundColor":{"value":"rgb(255,255,255)"},"direction":{"value":"column"}},"appId":"d1rxgz43y8l9ij","environmentName":"staging"}

0 comments on commit ec079f1

Please sign in to comment.