Skip to content

Commit

Permalink
Merge branch 'tagged-release/cms-support' into cshin/has-many-integ-t…
Browse files Browse the repository at this point in the history
…ests
  • Loading branch information
bombguy authored Nov 29, 2022
2 parents 44ca4fb + c25b427 commit bf7db43
Show file tree
Hide file tree
Showing 14 changed files with 1,291 additions and 494 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,10 @@ describe('amplify form renderer tests', () => {
});

it('should generate an update form with manyToMany relationship', () => {
const { componentText } = generateWithAmplifyFormRenderer('forms/tag-datastore-update', 'datastore/tag-post');
const { componentText, declaration } = generateWithAmplifyFormRenderer(
'forms/tag-datastore-update',
'datastore/tag-post',
);
// check nested model is imported
expect(componentText).toContain('import { Tag, Post, TagPost } from "../models";');

Expand All @@ -139,6 +142,9 @@ describe('amplify form renderer tests', () => {

// check resetStateValues has correct dependencies
expect(componentText).toContain('React.useEffect(resetStateValues, [tagRecord, linkedPosts]);');

expect(componentText).toMatchSnapshot();
expect(declaration).toMatchSnapshot();
});

it('should generate a create form with array of Enums', () => {
Expand Down
240 changes: 3 additions & 237 deletions packages/codegen-ui-react/lib/forms/form-renderer-helper/cta-props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,11 @@
See the License for the specific language governing permissions and
limitations under the License.
*/
import { FieldConfigMetadata, HasManyRelationshipType } from '@aws-amplify/codegen-ui/lib/types';
import {
factory,
NodeFlags,
SyntaxKind,
Expression,
VariableStatement,
ExpressionStatement,
Statement,
} from 'typescript';
import { FieldConfigMetadata } from '@aws-amplify/codegen-ui/lib/types';
import { factory, NodeFlags, SyntaxKind, Expression, Statement } from 'typescript';
import { lowerCaseFirst } from '../../helpers';
import { getDisplayValueObjectName } from './display-value';
import { getSetNameIdentifier, getLinkedDataName } from './form-state';
import { getSetNameIdentifier } from './form-state';
import {
buildHasManyRelationshipDataStoreStatements,
buildManyToManyRelationshipDataStoreStatements,
Expand Down Expand Up @@ -469,229 +461,3 @@ export const buildUpdateDatastoreQuery = (
),
];
};

/**
* const queryData = async () => {
* const record = id ? await DataStore.query(Tag, id) : tag;
* const linkedPosts = record
* ? await Promise.all(
* (
* await record.Posts.toArray()
* ).map((r) => {
* return r.post;
* }),
* )
* : [];
* setLinkedPosts(linkedPosts);
* setTagRecord(record);
* };
* queryData();
*/
export const buildUpdateDatastoreQueryForHasMany = (
dataTypeName: string,
recordName: string,
hasManyFieldConfigs: [string, FieldConfigMetadata][],
) => {
const lazyLoadLinkedDataStatements: VariableStatement[] = [];
const setLinkedDataStateStatements: ExpressionStatement[] = [];

hasManyFieldConfigs.forEach(([fieldName, fieldConfig]) => {
const linkedDataName = getLinkedDataName(fieldName);
const { relatedJoinFieldName } = fieldConfig.relationship as HasManyRelationshipType;
let lazyLoadLinkedDataStatement;
if (isManyToManyRelationship(fieldConfig)) {
lazyLoadLinkedDataStatement = factory.createVariableStatement(
undefined,
factory.createVariableDeclarationList(
[
factory.createVariableDeclaration(
factory.createIdentifier(linkedDataName),
undefined,
undefined,
factory.createConditionalExpression(
factory.createIdentifier('record'),
factory.createToken(SyntaxKind.QuestionToken),
factory.createAwaitExpression(
factory.createCallExpression(
factory.createPropertyAccessExpression(
factory.createIdentifier('Promise'),
factory.createIdentifier('all'),
),
undefined,
[
factory.createCallExpression(
factory.createPropertyAccessExpression(
factory.createParenthesizedExpression(
factory.createAwaitExpression(
factory.createCallExpression(
factory.createPropertyAccessExpression(
factory.createPropertyAccessExpression(
factory.createIdentifier('record'),
factory.createIdentifier(fieldName),
),
factory.createIdentifier('toArray'),
),
undefined,
[],
),
),
),
factory.createIdentifier('map'),
),
undefined,
[
factory.createArrowFunction(
undefined,
undefined,
[
factory.createParameterDeclaration(
undefined,
undefined,
undefined,
factory.createIdentifier('r'),
undefined,
undefined,
undefined,
),
],
undefined,
factory.createToken(SyntaxKind.EqualsGreaterThanToken),
factory.createBlock(
[
factory.createReturnStatement(
factory.createPropertyAccessExpression(
factory.createIdentifier('r'),
factory.createIdentifier(relatedJoinFieldName as string),
),
),
],
true,
),
),
],
),
],
),
),
factory.createToken(SyntaxKind.ColonToken),
factory.createArrayLiteralExpression([], false),
),
),
],
// eslint-disable-next-line no-bitwise
NodeFlags.Const | NodeFlags.AwaitContext | NodeFlags.ContextFlags | NodeFlags.TypeExcludesFlags,
),
);
} else {
lazyLoadLinkedDataStatement = factory.createVariableStatement(
undefined,
factory.createVariableDeclarationList(
[
factory.createVariableDeclaration(
factory.createIdentifier(linkedDataName),
undefined,
undefined,
factory.createConditionalExpression(
factory.createIdentifier('record'),
factory.createToken(SyntaxKind.QuestionToken),
factory.createAwaitExpression(
factory.createCallExpression(
factory.createPropertyAccessExpression(
factory.createPropertyAccessExpression(
factory.createIdentifier('record'),
factory.createIdentifier(fieldName),
),
factory.createIdentifier('toArray'),
),
undefined,
[],
),
),
factory.createToken(SyntaxKind.ColonToken),
factory.createArrayLiteralExpression([], false),
),
),
],
NodeFlags.Const,
),
);
}

const setLinkedDataStateStatement = factory.createExpressionStatement(
factory.createCallExpression(getSetNameIdentifier(linkedDataName), undefined, [
factory.createIdentifier(linkedDataName),
]),
);

lazyLoadLinkedDataStatements.push(lazyLoadLinkedDataStatement);
setLinkedDataStateStatements.push(setLinkedDataStateStatement);
});

return [
factory.createVariableStatement(
undefined,
factory.createVariableDeclarationList(
[
factory.createVariableDeclaration(
factory.createIdentifier('queryData'),
undefined,
undefined,
factory.createArrowFunction(
[factory.createModifier(SyntaxKind.AsyncKeyword)],
undefined,
[],
undefined,
factory.createToken(SyntaxKind.EqualsGreaterThanToken),
factory.createBlock(
[
factory.createVariableStatement(
undefined,
factory.createVariableDeclarationList(
[
factory.createVariableDeclaration(
factory.createIdentifier('record'),
undefined,
undefined,
factory.createConditionalExpression(
factory.createIdentifier('id'),
factory.createToken(SyntaxKind.QuestionToken),
factory.createAwaitExpression(
factory.createCallExpression(
factory.createPropertyAccessExpression(
factory.createIdentifier('DataStore'),
factory.createIdentifier('query'),
),
undefined,
[factory.createIdentifier(dataTypeName), factory.createIdentifier('id')],
),
),
factory.createToken(SyntaxKind.ColonToken),
factory.createIdentifier(lowerCaseFirst(dataTypeName)),
),
),
],
// eslint-disable-next-line no-bitwise
NodeFlags.Const | NodeFlags.AwaitContext | NodeFlags.ContextFlags | NodeFlags.TypeExcludesFlags,
),
),
...lazyLoadLinkedDataStatements,
...setLinkedDataStateStatements,
factory.createExpressionStatement(
factory.createCallExpression(getSetNameIdentifier(recordName), undefined, [
factory.createIdentifier('record'),
]),
),
],
true,
),
),
),
],
NodeFlags.Const,
),
),
factory.createExpressionStatement(
factory.createCallExpression(factory.createIdentifier('queryData'), undefined, []),
),
];
};
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,7 @@
See the License for the specific language governing permissions and
limitations under the License.
*/
export {
onSubmitValidationRun,
buildUpdateDatastoreQuery,
buildUpdateDatastoreQueryForHasMany,
buildDataStoreExpression,
} from './cta-props';
export { onSubmitValidationRun, buildUpdateDatastoreQuery, buildDataStoreExpression } from './cta-props';

export { buildModelFieldObject } from './model-fields';

Expand Down
Loading

0 comments on commit bf7db43

Please sign in to comment.