Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: only use useDataStoreBinding when predicate is set #86

Merged
merged 1 commit into from
Sep 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
});
Comment on lines -218 to -222
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this removed? We would need to be able to query a single record.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is removed because there is no predicate on the buttonUser binding properties. According to #84 we should not use useDataStoreBinding here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, makes sense.

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"}