Skip to content

Commit

Permalink
Render form utils and add forms to index file (#536)
Browse files Browse the repository at this point in the history
* fix: remove locale dependency on date formatting

* feat: add utils file renderer

Co-authored-by: Kevin Pranoto <kpranoto@amazon.com>
Co-authored-by: Justin Shih <jushih@amazon.com>
  • Loading branch information
3 people authored Jul 21, 2022
1 parent 5978feb commit 9ceb6bc
Show file tree
Hide file tree
Showing 16 changed files with 1,647 additions and 129 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,7 @@ import {
getOverrideProps,
useStateMutationAction,
} from \\"@aws-amplify/ui-react/internal\\";
import {
Button,
Flex,
Grid,
TextField,
useTypeCastFields,
} from \\"@aws-amplify/ui-react\\";
import { Button, Flex, Grid, TextField } from \\"@aws-amplify/ui-react\\";
import { DataStore } from \\"aws-amplify\\";
export default function customDataForm(props) {
const {
Expand All @@ -34,15 +28,10 @@ export default function customDataForm(props) {
);
}
try {
await DataStore.save(
new Post(
useTypeCastFields({
fields: customDataFormFields,
modelName: Post.name,
schema,
})
)
);
await DataStore.save(new Post(customDataFormFields));
if (onSubmitComplete) {
onSubmitComplete({ saveSuccessful: true });
}
} catch (err) {
if (onSubmitComplete) {
onSubmitComplete({
Expand Down Expand Up @@ -140,13 +129,7 @@ import {
} from \\"@aws-amplify/ui-react/internal\\";
import { Post } from \\"../models\\";
import { schema } from \\"../models/schema\\";
import {
Button,
Flex,
Grid,
TextField,
useTypeCastFields,
} from \\"@aws-amplify/ui-react\\";
import { Button, Flex, Grid, TextField } from \\"@aws-amplify/ui-react\\";
import { DataStore } from \\"aws-amplify\\";
export default function myPostForm(props) {
const { onSubmitBefore, onSubmitComplete, onCancel, overrides, ...rest } =
Expand All @@ -165,15 +148,10 @@ export default function myPostForm(props) {
setMyPostFormFields(onSubmitBefore({ fields: myPostFormFields }));
}
try {
await DataStore.save(
new Post(
useTypeCastFields({
fields: myPostFormFields,
modelName: Post.name,
schema,
})
)
);
await DataStore.save(new Post(myPostFormFields));
if (onSubmitComplete) {
onSubmitComplete({ saveSuccessful: true });
}
} catch (err) {
if (onSubmitComplete) {
onSubmitComplete({
Expand Down Expand Up @@ -301,13 +279,7 @@ import {
} from \\"@aws-amplify/ui-react/internal\\";
import { Post } from \\"../models\\";
import { schema } from \\"../models/schema\\";
import {
Button,
Flex,
Grid,
TextField,
useTypeCastFields,
} from \\"@aws-amplify/ui-react\\";
import { Button, Flex, Grid, TextField } from \\"@aws-amplify/ui-react\\";
import { DataStore } from \\"aws-amplify\\";
export default function myPostForm(props) {
const { id, onSubmitBefore, onSubmitComplete, onCancel, overrides, ...rest } =
Expand All @@ -327,15 +299,10 @@ export default function myPostForm(props) {
setMyPostFormFields(onSubmitBefore({ fields: myPostFormFields }));
}
try {
await DataStore.save(
new Post(
useTypeCastFields({
fields: myPostFormFields,
modelName: Post.name,
schema,
})
)
);
await DataStore.save(new Post(myPostFormFields));
if (onSubmitComplete) {
onSubmitComplete({ saveSuccessful: true });
}
} catch (err) {
if (onSubmitComplete) {
onSubmitComplete({
Expand Down
46 changes: 21 additions & 25 deletions packages/codegen-ui-react/lib/amplify-ui-renderers/form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ export default class FormRenderer extends ReactComponentRenderer<BaseComponentPr
);

this.importCollection.addImport('@aws-amplify/ui-react', this.component.componentType);
this.importCollection.addImport('@aws-amplify/ui-react', 'useTypeCastFields');
this.importCollection.addImport('aws-amplify', 'DataStore');

return element;
Expand Down Expand Up @@ -149,36 +148,33 @@ export default class FormRenderer extends ReactComponentRenderer<BaseComponentPr
undefined,
[
factory.createNewExpression(factory.createIdentifier(dataTypeName), undefined, [
factory.createCallExpression(
factory.createIdentifier('useTypeCastFields'),
[factory.createTypeReferenceNode(factory.createIdentifier(dataTypeName), undefined)],
factory.createIdentifier(getStateName(FieldStateVariable(name))),
]),
],
),
),
),
factory.createIfStatement(
factory.createIdentifier('onSubmitComplete'),
factory.createBlock(
[
factory.createExpressionStatement(
factory.createCallExpression(factory.createIdentifier('onSubmitComplete'), undefined, [
factory.createObjectLiteralExpression(
[
factory.createObjectLiteralExpression(
[
factory.createPropertyAssignment(
factory.createIdentifier('fields'),
factory.createIdentifier(getStateName(FieldStateVariable(name))),
),
factory.createPropertyAssignment(
factory.createIdentifier('modelName'),
factory.createPropertyAccessExpression(
factory.createIdentifier(dataTypeName),
factory.createIdentifier('name'),
),
),
factory.createShorthandPropertyAssignment(
factory.createIdentifier('schema'),
undefined,
),
],
true,
factory.createPropertyAssignment(
factory.createIdentifier('saveSuccessful'),
factory.createTrue(),
),
],
false,
),
]),
],
),
),
],
true,
),
undefined,
),
],
true,
Expand Down
1 change: 1 addition & 0 deletions packages/codegen-ui-react/lib/forms/react-form-renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ export abstract class ReactFormTemplateRenderer extends StudioTemplateRenderer<
return {
componentText: transpiledComponentText,
declaration,
formMetadata: this.componentMetadata.formMetadata,
renderComponentToFilesystem: async (outputPath: string) => {
await this.renderComponentToFilesystem(transpiledComponentText)(this.fileName)(outputPath);
if (declaration) {
Expand Down
1 change: 1 addition & 0 deletions packages/codegen-ui-react/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ export * from './amplify-ui-renderers/amplify-renderer';
export * from './amplify-ui-renderers/amplify-form-renderer';
export * from './primitive';
export * from './react-index-studio-template-renderer';
export * from './react-utils-studio-template-renderer';
export * from './react-required-dependency-provider';
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@
*/
import { EOL } from 'os';
import { EmitHint, ExportDeclaration, factory } from 'typescript';
import { StudioTemplateRenderer, StudioTheme, StudioComponent } from '@aws-amplify/codegen-ui';
import { StudioTemplateRenderer, StudioTheme, StudioComponent, StudioForm } from '@aws-amplify/codegen-ui';
import { ReactRenderConfig, scriptKindToFileExtensionNonReact } from './react-render-config';
import { ImportCollection } from './imports';
import { ReactOutputManager } from './react-output-manager';
import { transpile, buildPrinter, defaultRenderConfig } from './react-studio-template-renderer-helper';
import { RequiredKeys } from './utils/type-utils';

type StudioSchema = StudioComponent | StudioTheme;
type StudioSchema = StudioComponent | StudioForm | StudioTheme;

export class ReactIndexStudioTemplateRenderer extends StudioTemplateRenderer<
string,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License").
You may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import { EOL } from 'os';
import ts, { EmitHint } from 'typescript';
import { StudioTemplateRenderer } from '@aws-amplify/codegen-ui';
import { ReactRenderConfig, scriptKindToFileExtensionNonReact } from './react-render-config';
import { ImportCollection } from './imports';
import { ReactOutputManager } from './react-output-manager';
import { RequiredKeys } from './utils/type-utils';
import { transpile, buildPrinter, defaultRenderConfig } from './react-studio-template-renderer-helper';
import { generateValidationFunction } from './utils/forms/validation';

export class ReactUtilsStudioTemplateRenderer extends StudioTemplateRenderer<
string,
string[],
ReactOutputManager,
{
componentText: string;
renderComponentToFilesystem: (outputPath: string) => Promise<void>;
}
> {
protected importCollection = new ImportCollection();

protected renderConfig: RequiredKeys<ReactRenderConfig, keyof typeof defaultRenderConfig>;

fileName: string;

/*
* list of util functions to generate
*/
utils: string[];

constructor(utils: string[], renderConfig: ReactRenderConfig) {
super(utils, new ReactOutputManager(), renderConfig);
this.utils = utils;
this.renderConfig = {
...defaultRenderConfig,
...renderConfig,
renderTypeDeclarations: false, // Never render type declarations for index.js|ts file.
};
this.fileName = `utils.${scriptKindToFileExtensionNonReact(this.renderConfig.script)}`;
}

renderComponentInternal() {
const { printer, file } = buildPrinter(this.fileName, this.renderConfig);
const utilsStatements: (ts.TypeAliasDeclaration | ts.VariableStatement)[] = [];

this.utils.forEach((util) => {
if (util === 'validation') {
utilsStatements.push(...generateValidationFunction());
}
});

const { componentText } = transpile(
utilsStatements.map((util) => printer.printNode(EmitHint.Unspecified, util, file)).join(EOL),
this.renderConfig,
);

return {
componentText,
renderComponentToFilesystem: async (outputPath: string) => {
await this.renderComponentToFilesystem(componentText)(this.fileName)(outputPath);
},
};
}

// no-op
validateSchema() {}
}
Loading

0 comments on commit 9ceb6bc

Please sign in to comment.