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

Cherry pick requiredDataModels fixes #818

Merged
merged 2 commits into from
Dec 6, 2022
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 @@ -70,6 +70,24 @@ export const generateWithAmplifyFormRenderer = (
return renderer.renderComponent();
};

export const generateComponentOnlyWithAmplifyFormRenderer = (
formJsonFile: string,
dataSchemaJsonFile: string | undefined,
renderConfig: ReactRenderConfig = defaultCLIRenderConfig,
) => {
let dataSchema: GenericDataSchema | undefined;
if (dataSchemaJsonFile) {
const dataStoreSchema = loadSchemaFromJSONFile<Schema>(dataSchemaJsonFile);
dataSchema = getGenericFromDataStore(dataStoreSchema);
}
const rendererFactory = new StudioTemplateRendererFactory(
(component: StudioForm) => new AmplifyFormRenderer(component, dataSchema, renderConfig),
);

const renderer = rendererFactory.buildRenderer(loadSchemaFromJSONFile<StudioForm>(formJsonFile));
return renderer.renderComponentOnly();
};

export const renderWithAmplifyViewRenderer = (
viewJsonFile: string,
dataSchemaJsonFile: string | undefined,
Expand Down
27 changes: 19 additions & 8 deletions packages/codegen-ui-react/lib/forms/react-form-renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,15 @@ import {
validationResponseType,
} from './type-helper';

type RenderComponentOnlyResponse = {
compText: string;
/**
* @deprecated Use {@link RenderComponentOnlyResponse.importCollection} instead.
*/
importsText: string;
requiredDataModels: string[];
importCollection: ImportCollection;
};
export abstract class ReactFormTemplateRenderer extends StudioTemplateRenderer<
string,
StudioForm,
Expand All @@ -110,6 +119,8 @@ export abstract class ReactFormTemplateRenderer extends StudioTemplateRenderer<

public fileName: string;

protected requiredDataModels: string[] = [];

constructor(component: StudioForm, dataSchema: GenericDataSchema | undefined, renderConfig: ReactRenderConfig) {
super(component, new ReactOutputManager(), renderConfig);
this.renderConfig = {
Expand All @@ -130,10 +141,9 @@ export abstract class ReactFormTemplateRenderer extends StudioTemplateRenderer<
}

@handleCodegenErrors
renderComponentOnly() {
renderComponentOnly(): RenderComponentOnlyResponse {
const variableStatements = this.buildVariableStatements();
const jsx = this.renderJsx(this.formComponent);
const requiredDataModels = [];

const { printer, file } = buildPrinter(this.fileName, this.renderConfig);

Expand All @@ -158,12 +168,12 @@ export abstract class ReactFormTemplateRenderer extends StudioTemplateRenderer<
// do not produce declaration becuase it is not used
const { componentText: compText } = transpile(result, { ...this.renderConfig, renderTypeDeclarations: false });

if (this.component.dataType.dataSourceType === 'DataStore') {
requiredDataModels.push(this.component.dataType.dataTypeName);
// TODO: require other models if form is handling querying relational models
}

return { compText, importsText, requiredDataModels };
return {
compText,
importsText,
requiredDataModels: this.requiredDataModels,
importCollection: this.importCollection,
};
}

renderComponentInternal() {
Expand Down Expand Up @@ -356,6 +366,7 @@ export abstract class ReactFormTemplateRenderer extends StudioTemplateRenderer<

// add model import for datastore type
if (dataSourceType === 'DataStore') {
this.requiredDataModels.push(dataTypeName);
modelName = this.importCollection.addImport(ImportSource.LOCAL_MODELS, dataTypeName);
}

Expand Down