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: add default values for update form #631

Merged
merged 1 commit into from
Sep 8, 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 @@ -980,6 +980,11 @@ export default function MyPostForm(props) {
const queryData = async () => {
const record = id ? await DataStore.query(Post, id) : post;
setPostRecord(record);
setTextAreaFieldbbd63464(record.TextAreaFieldbbd63464);
setCaption(record.caption);
setUsername(record.username);
setProfile_url(record.profile_url);
setPost_url(record.post_url);
};
queryData();
}, [id, post]);
Expand Down Expand Up @@ -1102,6 +1107,7 @@ export default function MyPostForm(props) {
}}
errorMessage={errors.TextAreaFieldbbd63464?.errorMessage}
hasError={errors.TextAreaFieldbbd63464?.hasError}
defaultValue={TextAreaFieldbbd63464}
{...getOverrideProps(overrides, \\"TextAreaFieldbbd63464\\")}
></TextAreaField>
</Grid>
Expand All @@ -1122,6 +1128,7 @@ export default function MyPostForm(props) {
}}
errorMessage={errors.caption?.errorMessage}
hasError={errors.caption?.hasError}
defaultValue={caption}
{...getOverrideProps(overrides, \\"caption\\")}
></TextField>
</Grid>
Expand All @@ -1142,6 +1149,7 @@ export default function MyPostForm(props) {
}}
errorMessage={errors.username?.errorMessage}
hasError={errors.username?.hasError}
defaultValue={username}
{...getOverrideProps(overrides, \\"username\\")}
></TextField>
</Grid>
Expand All @@ -1162,6 +1170,7 @@ export default function MyPostForm(props) {
}}
errorMessage={errors.profile_url?.errorMessage}
hasError={errors.profile_url?.hasError}
defaultValue={profile_url}
{...getOverrideProps(overrides, \\"profile_url\\")}
></TextField>
</Grid>
Expand All @@ -1182,6 +1191,7 @@ export default function MyPostForm(props) {
}}
errorMessage={errors.post_url?.errorMessage}
hasError={errors.post_url?.hasError}
defaultValue={post_url}
{...getOverrideProps(overrides, \\"post_url\\")}
></TextField>
</Grid>
Expand Down
29 changes: 28 additions & 1 deletion packages/codegen-ui-react/lib/forms/form-renderer-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,14 @@ export const addFormAttributes = (component: StudioComponent | StudioComponentCh
),
),
);
if (formMetadata.formActionType === 'update') {
attributes.push(
factory.createJsxAttribute(
factory.createIdentifier('defaultValue'),
factory.createJsxExpression(undefined, factory.createIdentifier(component.name)),
),
);
}
if (fieldConfig.isArray) {
attributes.push(
factory.createJsxAttribute(
Expand Down Expand Up @@ -1081,7 +1089,10 @@ export const onSubmitValidationRun = [
),
];

export const buildUpdateDatastoreQuery = (dataTypeName: string) => {
export const buildUpdateDatastoreQuery = (
dataTypeName: string,
fieldConfigs: Record<string, FieldConfigMetadata> | undefined,
) => {
return [
factory.createVariableStatement(
undefined,
Expand Down Expand Up @@ -1133,6 +1144,22 @@ export const buildUpdateDatastoreQuery = (dataTypeName: string) => {
factory.createIdentifier('record'),
]),
),
...(fieldConfigs
? Object.keys(fieldConfigs).map((field) =>
factory.createExpressionStatement(
factory.createCallExpression(
factory.createIdentifier(`set${capitalizeFirstLetter(field)}`),
undefined,
[
factory.createPropertyAccessExpression(
factory.createIdentifier('record'),
factory.createIdentifier(field),
),
],
),
),
)
: []),
],
true,
),
Expand Down
7 changes: 6 additions & 1 deletion packages/codegen-ui-react/lib/forms/react-form-renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,12 @@ export abstract class ReactFormTemplateRenderer extends StudioTemplateRenderer<
statements.push(
buildUseStateExpression(`${lowerCaseDataTypeName}Record`, factory.createIdentifier(lowerCaseDataTypeName)),
);
statements.push(addUseEffectWrapper(buildUpdateDatastoreQuery(dataTypeName), ['id', lowerCaseDataTypeName]));
statements.push(
addUseEffectWrapper(
buildUpdateDatastoreQuery(dataTypeName, this.componentMetadata.formMetadata?.fieldConfigs),
['id', lowerCaseDataTypeName],
),
);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -782,7 +782,6 @@ export const generateArrayFieldComponent = () => {
};
/*
<ArrayField
label="Breeds"
onChange = { async(items) => {
setModelFields({ ...modelFields, breeds: items });
setCurrentBreedsValue('');
Expand All @@ -802,7 +801,6 @@ export const renderArrayFieldComponent = (fieldName: string, label: string, inpu
factory.createIdentifier('ArrayField'),
undefined,
factory.createJsxAttributes([
factory.createJsxAttribute(factory.createIdentifier('label'), factory.createStringLiteral(label)),
factory.createJsxAttribute(
factory.createIdentifier('onChange'),
factory.createJsxExpression(
Expand Down
6 changes: 6 additions & 0 deletions packages/codegen-ui/lib/types/form/form-metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@
import { DataFieldDataType } from '../data';
import { FieldValidationConfiguration } from './form-validation';

/**
* Form Action type definition
*/
export type StudioFormActionType = 'create' | 'update';
Jshhhh marked this conversation as resolved.
Show resolved Hide resolved

export type FieldConfigMetadata = {
// ex. name field has a string validation type where the rule is char length > 5
validationRules: FieldValidationConfiguration[];
Expand All @@ -27,6 +32,7 @@ export type FieldConfigMetadata = {

export type FormMetadata = {
id?: string;
formActionType: StudioFormActionType;
name: string;
fieldConfigs: Record<string, FieldConfigMetadata>;
};
6 changes: 1 addition & 5 deletions packages/codegen-ui/lib/types/form/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { FormDefinition, ModelFieldsConfigs, FieldTypeMapKeys, ButtonConfig } fr
import { StudioFieldInputConfig, StudioFormValueMappings } from './input-config';
import { StudioFieldPosition } from './position';
import { StudioFormCTA } from './form-cta';
import { FormMetadata, FieldConfigMetadata } from './form-metadata';
import { FormMetadata, FieldConfigMetadata, StudioFormActionType } from './form-metadata';

export type StudioDataSourceType = 'DataStore' | 'Custom';

Expand All @@ -33,10 +33,6 @@ export type StudioFormDataType = {

dataTypeName: string;
};
/**
* Form Action type definition
*/
type StudioFormActionType = 'create' | 'update';

/**
* This is the base type for all StudioForms
Expand Down
1 change: 1 addition & 0 deletions packages/codegen-ui/lib/utils/form-component-metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export const mapFormMetadata = (form: StudioForm, formDefinition: FormDefinition
return {
id: form.id,
name: form.name,
formActionType: form.formActionType,
fieldConfigs: inputElementEntries.reduce<Record<string, FieldConfigMetadata>>((configs, [name, config]) => {
const updatedConfigs = configs;
const metadata: FieldConfigMetadata = {
Expand Down