Skip to content

Commit

Permalink
fix: add optional chaining
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacob Logan committed Feb 21, 2024
1 parent 575cbcc commit 3b6a311
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32139,7 +32139,7 @@ export default function UpdateOrgForm(props) {
const queryData = async () => {
const record = idProp ? await DataStore.query(Org, idProp) : orgModelProp;
setOrgRecord(record);
const linkedComments = record ? await record.comments.toArray() : [];
const linkedComments = record ? await record?.comments?.toArray() : [];
setLinkedComments(linkedComments);
};
queryData();
Expand Down Expand Up @@ -32759,13 +32759,13 @@ export default function UpdateCompositeDogForm(props) {
: undefined;
setCompositeOwner(CompositeOwnerRecord);
const linkedCompositeToys = record
? await record.CompositeToys.toArray()
? await record?.CompositeToys?.toArray()
: [];
setLinkedCompositeToys(linkedCompositeToys);
const linkedCompositeVets = record
? await Promise.all(
(
await record.CompositeVets.toArray()
await record?.CompositeVets?.toArray()
).map((r) => {
return r.compositeVet;
})
Expand Down Expand Up @@ -33888,15 +33888,15 @@ export default function UpdateCPKTeacherForm(props) {
const linkedCPKClasses = record
? await Promise.all(
(
await record.CPKClasses.toArray()
await record?.CPKClasses?.toArray()
).map((r) => {
return r.cpkClass;
})
)
: [];
setLinkedCPKClasses(linkedCPKClasses);
const linkedCPKProjects = record
? await record.CPKProjects.toArray()
? await record?.CPKProjects?.toArray()
: [];
setLinkedCPKProjects(linkedCPKProjects);
};
Expand Down Expand Up @@ -37257,13 +37257,13 @@ export default function UpdateCompositeDogForm(props) {
: undefined;
setCompositeOwner(CompositeOwnerRecord);
const linkedCompositeToys = record
? await record.CompositeToys.toArray()
? await record?.CompositeToys?.toArray()
: [];
setLinkedCompositeToys(linkedCompositeToys);
const linkedCompositeVets = record
? await Promise.all(
(
await record.CompositeVets.toArray()
await record?.CompositeVets?.toArray()
).map((r) => {
return r.compositeVet;
})
Expand Down Expand Up @@ -46067,7 +46067,7 @@ export default function SchoolUpdateForm(props) {
? await DataStore.query(School, idProp)
: schoolModelProp;
setSchoolRecord(record);
const linkedStudents = record ? await record.Students.toArray() : [];
const linkedStudents = record ? await record?.Students?.toArray() : [];
setLinkedStudents(linkedStudents);
};
queryData();
Expand Down Expand Up @@ -46643,7 +46643,7 @@ export default function SchoolUpdateForm(props) {
? await DataStore.query(School, idProp)
: schoolModelProp;
setSchoolRecord(record);
const linkedStudent = record ? await record.Student.toArray() : [];
const linkedStudent = record ? await record?.Student?.toArray() : [];
setLinkedStudent(linkedStudent);
};
queryData();
Expand Down Expand Up @@ -47891,7 +47891,7 @@ export default function TagUpdateForm(props) {
const linkedPosts = record
? await Promise.all(
(
await record.Posts.toArray()
await record?.Posts?.toArray()
).map((r) => {
return r.post;
})
Expand Down Expand Up @@ -55329,7 +55329,7 @@ export default function UpdateDealershipForm(props) {
? await DataStore.query(Dealership, idProp)
: dealershipModelProp;
setDealershipRecord(record);
const linkedCars = record ? await record.cars.toArray() : [];
const linkedCars = record ? await record?.cars?.toArray() : [];
setLinkedCars(linkedCars);
};
queryData();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ describe('amplify form renderer tests', () => {
expect(componentText).toContain('const postRecords = useDataStoreBinding({');

// check lazy load linked data
expect(componentText).toContain('await record.Posts.toArray()');
expect(componentText).toContain('await record?.Posts?.toArray()');

// check custom display value is set
expect(componentText).toContain('Posts: (r) => r?.title');
Expand Down Expand Up @@ -228,7 +228,7 @@ describe('amplify form renderer tests', () => {
expect(componentText).toContain('const studentRecords = useDataStoreBinding({');

// check lazy load linked data
expect(componentText).toContain('const linkedStudents = record ? await record.Students.toArray() : [];');
expect(componentText).toContain('const linkedStudents = record ? await record?.Students?.toArray() : [];');

// check custom display value is set
expect(componentText).toContain('Students: (r) => r?.name');
Expand Down Expand Up @@ -257,7 +257,7 @@ describe('amplify form renderer tests', () => {
expect(componentText).toContain('const studentRecords = useDataStoreBinding({');

// check lazy load linked data
expect(componentText).toContain('const linkedStudent = record ? await record.Student.toArray() : [];');
expect(componentText).toContain('const linkedStudent = record ? await record?.Student?.toArray() : [];');

// check custom display value is set
expect(componentText).toContain('Student: (r) => `${r?.name ? r?.name + " - " : ""}${r?.id}`,');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1233,11 +1233,13 @@ export const buildGetRelationshipModels = (
factory.createParenthesizedExpression(
factory.createAwaitExpression(
factory.createCallExpression(
factory.createPropertyAccessExpression(
factory.createPropertyAccessExpression(
factory.createPropertyAccessChain(
factory.createPropertyAccessChain(
factory.createIdentifier('record'),
factory.createToken(SyntaxKind.QuestionDotToken),
factory.createIdentifier(fieldName),
),
factory.createToken(SyntaxKind.QuestionDotToken),
factory.createIdentifier('toArray'),
),
undefined,
Expand Down Expand Up @@ -1309,8 +1311,13 @@ export const buildGetRelationshipModels = (
factory.createToken(SyntaxKind.QuestionToken),
factory.createAwaitExpression(
factory.createCallExpression(
factory.createPropertyAccessExpression(
factory.createPropertyAccessExpression(recordIdentifier, factory.createIdentifier(fieldName)),
factory.createPropertyAccessChain(
factory.createPropertyAccessChain(
recordIdentifier,
factory.createToken(SyntaxKind.QuestionDotToken),
factory.createIdentifier(fieldName),
),
factory.createToken(SyntaxKind.QuestionDotToken),
factory.createIdentifier('toArray'),
),
undefined,
Expand Down

0 comments on commit 3b6a311

Please sign in to comment.