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: update form useEffect to conditionally change state if record is defined #641

Merged
merged 1 commit into from
Sep 13, 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 @@ -851,12 +851,14 @@ export default function MyPostForm(props) {
React.useEffect(() => {
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);
if (record) {
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
60 changes: 33 additions & 27 deletions packages/codegen-ui-react/lib/forms/form-renderer-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1136,10 +1136,36 @@ export const onSubmitValidationRun = [
),
];

export const buildUpdateDatastoreQuery = (
dataTypeName: string,
fieldConfigs: Record<string, FieldConfigMetadata> | undefined,
) => {
export const ifRecordDefinedExpression = (dataTypeName: string, fieldConfigs: Record<string, FieldConfigMetadata>) => {
return factory.createIfStatement(
factory.createIdentifier('record'),
factory.createBlock(
[
factory.createExpressionStatement(
factory.createCallExpression(factory.createIdentifier(`set${dataTypeName}Record`), undefined, [
factory.createIdentifier('record'),
]),
),
...Object.keys(fieldConfigs).map((field) =>
factory.createExpressionStatement(
factory.createCallExpression(factory.createIdentifier(`set${capitalizeFirstLetter(field)}`), undefined, [
factory.createPropertyAccessExpression(
factory.createIdentifier('record'),
factory.createIdentifier(field),
),
]),
),
),
],
true,
),
undefined,
);
};

export const buildUpdateDatastoreQuery = (dataTypeName: string, fieldConfigs: Record<string, FieldConfigMetadata>) => {
// TODO: update this once cpk is supported in datastore
const pkQueryIdentifier = factory.createIdentifier('id');
return [
factory.createVariableStatement(
undefined,
Expand All @@ -1166,7 +1192,7 @@ export const buildUpdateDatastoreQuery = (
undefined,
undefined,
factory.createConditionalExpression(
factory.createIdentifier('id'),
pkQueryIdentifier,
factory.createToken(SyntaxKind.QuestionToken),
factory.createAwaitExpression(
factory.createCallExpression(
Expand All @@ -1175,7 +1201,7 @@ export const buildUpdateDatastoreQuery = (
factory.createIdentifier('query'),
),
undefined,
[factory.createIdentifier(dataTypeName), factory.createIdentifier('id')],
[factory.createIdentifier(dataTypeName), pkQueryIdentifier],
),
),
factory.createToken(SyntaxKind.ColonToken),
Expand All @@ -1186,27 +1212,7 @@ export const buildUpdateDatastoreQuery = (
NodeFlags.Const,
),
),
factory.createExpressionStatement(
factory.createCallExpression(factory.createIdentifier(`set${dataTypeName}Record`), undefined, [
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),
),
],
),
),
)
: []),
ifRecordDefinedExpression(dataTypeName, fieldConfigs),
],
true,
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,8 @@ export abstract class ReactFormTemplateRenderer extends StudioTemplateRenderer<
);
statements.push(
addUseEffectWrapper(
buildUpdateDatastoreQuery(dataTypeName, this.componentMetadata.formMetadata?.fieldConfigs),
buildUpdateDatastoreQuery(dataTypeName, formMetadata.fieldConfigs),
// TODO: change once cpk is supported in datastore
['id', lowerCaseDataTypeName],
),
);
Expand Down