Skip to content

Commit

Permalink
chore: take GenericDataSchema into form renderer and definition mapper (
Browse files Browse the repository at this point in the history
#528)

Co-authored-by: Hein Jeong <heinje@amazon.com>
  • Loading branch information
hein-j and Hein Jeong authored Jul 21, 2022
1 parent 85d7be1 commit 5978feb
Show file tree
Hide file tree
Showing 12 changed files with 506 additions and 358 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ export default function myPostForm(props) {
<TextField
label=\\"caption\\"
isRequired={false}
isReadOnly={false}
{...getOverrideProps(overrides, \\"TextField0\\")}
></TextField>
</Grid>
Expand All @@ -212,6 +213,7 @@ export default function myPostForm(props) {
<TextField
label=\\"username\\"
isRequired={false}
isReadOnly={false}
{...getOverrideProps(overrides, \\"TextField0\\")}
></TextField>
</Grid>
Expand All @@ -224,6 +226,7 @@ export default function myPostForm(props) {
<TextField
label=\\"post_url\\"
isRequired={false}
isReadOnly={false}
{...getOverrideProps(overrides, \\"TextField0\\")}
></TextField>
</Grid>
Expand All @@ -236,6 +239,7 @@ export default function myPostForm(props) {
<TextField
label=\\"profile_url\\"
isRequired={false}
isReadOnly={false}
{...getOverrideProps(overrides, \\"TextField0\\")}
></TextField>
</Grid>
Expand Down Expand Up @@ -358,6 +362,7 @@ export default function myPostForm(props) {
<TextField
label=\\"caption\\"
isRequired={false}
isReadOnly={false}
{...getOverrideProps(overrides, \\"TextField0\\")}
></TextField>
</Grid>
Expand All @@ -370,6 +375,7 @@ export default function myPostForm(props) {
<TextField
label=\\"username\\"
isRequired={false}
isReadOnly={false}
{...getOverrideProps(overrides, \\"TextField0\\")}
></TextField>
</Grid>
Expand All @@ -382,6 +388,7 @@ export default function myPostForm(props) {
<TextField
label=\\"post_url\\"
isRequired={false}
isReadOnly={false}
{...getOverrideProps(overrides, \\"TextField0\\")}
></TextField>
</Grid>
Expand All @@ -394,6 +401,7 @@ export default function myPostForm(props) {
<TextField
label=\\"profile_url\\"
isRequired={false}
isReadOnly={false}
{...getOverrideProps(overrides, \\"TextField0\\")}
></TextField>
</Grid>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

import { StudioTemplateRendererFactory } from '@aws-amplify/codegen-ui/lib/template-renderer-factory';
import { DataStoreModelInfo, StudioForm, SchemaModel } from '@aws-amplify/codegen-ui';
import { StudioForm, Schema, GenericDataSchema, getGenericFromDataStore } from '@aws-amplify/codegen-ui';
import { ReactRenderConfig, AmplifyFormRenderer, ModuleKind, ScriptKind, ScriptTarget } from '..';
import { loadSchemaFromJSONFile } from './__utils__';

Expand All @@ -31,13 +31,13 @@ export const generateWithAmplifyFormRenderer = (
dataSchemaJsonFile: string | undefined,
renderConfig: ReactRenderConfig = defaultCLIRenderConfig,
): { componentText: string; declaration?: string } => {
let dataStoreInfo: DataStoreModelInfo | undefined;
let dataSchema: GenericDataSchema | undefined;
if (dataSchemaJsonFile) {
const { fields } = loadSchemaFromJSONFile<SchemaModel>(dataSchemaJsonFile);
dataStoreInfo = { fields: Object.values(fields).map((value) => value) };
const dataStoreSchema = loadSchemaFromJSONFile<Schema>(dataSchemaJsonFile);
dataSchema = getGenericFromDataStore(dataStoreSchema);
}
const rendererFactory = new StudioTemplateRendererFactory(
(component: StudioForm) => new AmplifyFormRenderer(component, dataStoreInfo, renderConfig),
(component: StudioForm) => new AmplifyFormRenderer(component, dataSchema, renderConfig),
);

const renderer = rendererFactory.buildRenderer(loadSchemaFromJSONFile<StudioForm>(formJsonFile));
Expand Down
9 changes: 3 additions & 6 deletions packages/codegen-ui-react/lib/forms/react-form-renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
import {
ComponentMetadata,
computeComponentMetadata,
DataStoreModelInfo,
FormDefinition,
generateFormDefinition,
GenericDataSchema,
handleCodegenErrors,
mapFormDefinitionToComponent,
mapFormMetadata,
Expand Down Expand Up @@ -86,10 +86,7 @@ export abstract class ReactFormTemplateRenderer extends StudioTemplateRenderer<

public fileName: string;

/*
TODO: Change to use generic dataschema
*/
constructor(component: StudioForm, modelInfo: DataStoreModelInfo | undefined, renderConfig: ReactRenderConfig) {
constructor(component: StudioForm, dataSchema: GenericDataSchema | undefined, renderConfig: ReactRenderConfig) {
super(component, new ReactOutputManager(), renderConfig);
this.renderConfig = {
...defaultRenderConfig,
Expand All @@ -98,7 +95,7 @@ export abstract class ReactFormTemplateRenderer extends StudioTemplateRenderer<
// the super class creates a component aka form which is what we pass in this extended implmentation
this.fileName = `${this.component.name}.${scriptKindToFileExtension(this.renderConfig.script)}`;

this.formDefinition = generateFormDefinition({ form: component, modelInfo });
this.formDefinition = generateFormDefinition({ form: component, dataSchema });

// create a studio component which will represent the structure of the form
this.formComponent = mapFormDefinitionToComponent(this.component.name, this.formDefinition);
Expand Down
160 changes: 84 additions & 76 deletions packages/codegen-ui/example-schemas/datastore/post.json
Original file line number Diff line number Diff line change
@@ -1,81 +1,89 @@
{
"name": "Post",
"fields": {
"id": {
"name": "id",
"isArray": false,
"type": "ID",
"isRequired": true,
"attributes": []
},
"caption": {
"name": "caption",
"isArray": false,
"type": "String",
"isRequired": false,
"attributes": []
},
"username": {
"name": "username",
"isArray": false,
"type": "String",
"isRequired": false,
"attributes": []
},
"post_url": {
"name": "post_url",
"isArray": false,
"type": "AWSURL",
"isRequired": false,
"attributes": []
},
"profile_url": {
"name": "profile_url",
"isArray": false,
"type": "AWSURL",
"isRequired": false,
"attributes": []
},
"createdAt": {
"name": "createdAt",
"isArray": false,
"type": "AWSDateTime",
"isRequired": false,
"attributes": [],
"isReadOnly": true
},
"updatedAt": {
"name": "updatedAt",
"isArray": false,
"type": "AWSDateTime",
"isRequired": false,
"attributes": [],
"isReadOnly": true
}
},
"syncable": true,
"pluralName": "Posts",
"attributes": [
{
"type": "model",
"properties": {}
},
{
"type": "auth",
"properties": {
"rules": [
{
"allow": "private",
"provider": "iam",
"operations": [
"create",
"update",
"delete",
"read"
"models": {
"Post": {
"name": "Post",
"fields": {
"id": {
"name": "id",
"isArray": false,
"type": "ID",
"isRequired": true,
"attributes": []
},
"caption": {
"name": "caption",
"isArray": false,
"type": "String",
"isRequired": false,
"attributes": []
},
"username": {
"name": "username",
"isArray": false,
"type": "String",
"isRequired": false,
"attributes": []
},
"post_url": {
"name": "post_url",
"isArray": false,
"type": "AWSURL",
"isRequired": false,
"attributes": []
},
"profile_url": {
"name": "profile_url",
"isArray": false,
"type": "AWSURL",
"isRequired": false,
"attributes": []
},
"createdAt": {
"name": "createdAt",
"isArray": false,
"type": "AWSDateTime",
"isRequired": false,
"attributes": [],
"isReadOnly": true
},
"updatedAt": {
"name": "updatedAt",
"isArray": false,
"type": "AWSDateTime",
"isRequired": false,
"attributes": [],
"isReadOnly": true
}
},
"syncable": true,
"pluralName": "Posts",
"attributes": [
{
"type": "model",
"properties": {}
},
{
"type": "auth",
"properties": {
"rules": [
{
"allow": "private",
"provider": "iam",
"operations": [
"create",
"update",
"delete",
"read"
]
}
]
}
]
}
}
]
}
]
},
"enums": {
},
"nonModels": {},
"version": "000000"
}
Loading

0 comments on commit 5978feb

Please sign in to comment.