Skip to content

Commit

Permalink
fix: conditionally set selected record state
Browse files Browse the repository at this point in the history
  • Loading branch information
rtpascual authored and awinberg-aws committed Aug 18, 2023
1 parent 0a83efb commit 604e0e9
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1706,7 +1706,10 @@ export default function MyMemberForm(props) {
: \\"\\"
);
setCurrentTeamIDValue(value);
setSelectedTeamIDRecords([teamIDRecords.find((r) => r.id === value)]);
const selectedRecord = teamIDRecords.find((r) => r.id === value);
if (selectedRecord) {
setSelectedTeamIDRecords([selectedRecord]);
}
}}
inputFieldRef={teamIDRef}
defaultFieldValue={\\"\\"}
Expand Down Expand Up @@ -4018,7 +4021,10 @@ export default function CommentCreateForm(props) {
: \\"\\"
);
setCurrentPostIDValue(value);
setSelectedPostIDRecords([postIDRecords.find((r) => r.id === value)]);
const selectedRecord = postIDRecords.find((r) => r.id === value);
if (selectedRecord) {
setSelectedPostIDRecords([selectedRecord]);
}
}}
inputFieldRef={postIDRef}
defaultFieldValue={\\"\\"}
Expand Down Expand Up @@ -9614,7 +9620,10 @@ export default function CommentUpdateForm(props) {
: \\"\\"
);
setCurrentPostIDValue(value);
setSelectedPostIDRecords([postIDRecords.find((r) => r.id === value)]);
const selectedRecord = postIDRecords.find((r) => r.id === value);
if (selectedRecord) {
setSelectedPostIDRecords([selectedRecord]);
}
}}
inputFieldRef={postIDRef}
defaultFieldValue={\\"\\"}
Expand Down Expand Up @@ -10344,7 +10353,10 @@ export default function CommentUpdateForm(props) {
: \\"\\"
);
setCurrentPostIDValue(value);
setSelectedPostIDRecords([postIDRecords.find((r) => r.id === value)]);
const selectedRecord = postIDRecords.find((r) => r.id === value);
if (selectedRecord) {
setSelectedPostIDRecords([selectedRecord]);
}
}}
inputFieldRef={postIDRef}
defaultFieldValue={\\"\\"}
Expand Down Expand Up @@ -10997,7 +11009,10 @@ export default function CommentUpdateForm(props) {
: \\"\\"
);
setCurrentPostIDValue(value);
setSelectedPostIDRecords([postIDRecords.find((r) => r.id === value)]);
const selectedRecord = postIDRecords.find((r) => r.id === value);
if (selectedRecord) {
setSelectedPostIDRecords([selectedRecord]);
}
}}
inputFieldRef={postIDRef}
defaultFieldValue={\\"\\"}
Expand Down Expand Up @@ -13869,9 +13884,12 @@ export default function CreateCompositeToyForm(props) {
: \\"\\"
);
setCurrentCompositeDogCompositeToysNameValue(value);
setSelectedCompositeDogCompositeToysNameRecords([
compositeDogCompositeToysNameRecords.find((r) => r.name === value),
]);
const selectedRecord = compositeDogCompositeToysNameRecords.find(
(r) => r.name === value
);
if (selectedRecord) {
setSelectedCompositeDogCompositeToysNameRecords([selectedRecord]);
}
}}
inputFieldRef={compositeDogCompositeToysNameRef}
defaultFieldValue={\\"\\"}
Expand Down Expand Up @@ -13982,11 +14000,15 @@ export default function CreateCompositeToyForm(props) {
: \\"\\"
);
setCurrentCompositeDogCompositeToysDescriptionValue(value);
setSelectedCompositeDogCompositeToysDescriptionRecords([
const selectedRecord =
compositeDogCompositeToysDescriptionRecords.find(
(r) => r.description === value
),
]);
);
if (selectedRecord) {
setSelectedCompositeDogCompositeToysDescriptionRecords([
selectedRecord,
]);
}
}}
inputFieldRef={compositeDogCompositeToysDescriptionRef}
defaultFieldValue={\\"\\"}
Expand Down Expand Up @@ -14929,9 +14951,12 @@ export default function CreateCommentForm(props) {
: \\"\\"
);
setCurrentPostCommentsIdValue(value);
setSelectedPostCommentsIdRecords([
postCommentsIdRecords.find((r) => r.id === value),
]);
const selectedRecord = postCommentsIdRecords.find(
(r) => r.id === value
);
if (selectedRecord) {
setSelectedPostCommentsIdRecords([selectedRecord]);
}
}}
inputFieldRef={postCommentsIdRef}
defaultFieldValue={\\"\\"}
Expand Down Expand Up @@ -17788,11 +17813,14 @@ export default function ChildItemUpdateForm(props) {
: \\"\\"
);
setCurrentCustomKeyModelChildrenMycustomkeyValue(value);
setSelectedCustomKeyModelChildrenMycustomkeyRecords([
customKeyModelChildrenMycustomkeyRecords.find(
(r) => r.mycustomkey === value
),
]);
const selectedRecord = customKeyModelChildrenMycustomkeyRecords.find(
(r) => r.mycustomkey === value
);
if (selectedRecord) {
setSelectedCustomKeyModelChildrenMycustomkeyRecords([
selectedRecord,
]);
}
}}
inputFieldRef={customKeyModelChildrenMycustomkeyRef}
defaultFieldValue={\\"\\"}
Expand Down Expand Up @@ -20181,7 +20209,10 @@ export default function CommentUpdateForm(props) {
: \\"\\"
);
setCurrentPostIDValue(value);
setSelectedPostIDRecords([postIDRecords.find((r) => r.id === value)]);
const selectedRecord = postIDRecords.find((r) => r.id === value);
if (selectedRecord) {
setSelectedPostIDRecords([selectedRecord]);
}
}}
inputFieldRef={postIDRef}
defaultFieldValue={\\"\\"}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -359,13 +359,17 @@ export const renderArrayFieldComponent = (
);

if (dataApi === 'GraphQL') {
const selectedRecordName = 'selectedRecord';

setStateStatements.push(
factory.createExpressionStatement(
factory.createCallExpression(
getSetNameIdentifier(`selected${capitalizeFirstLetter(fieldName)}Records`),
undefined,
factory.createVariableStatement(
undefined,
factory.createVariableDeclarationList(
[
factory.createArrayLiteralExpression([
factory.createVariableDeclaration(
factory.createIdentifier(selectedRecordName),
undefined,
undefined,
factory.createCallExpression(
factory.createPropertyAccessExpression(
factory.createIdentifier(getRecordsName(fieldName)),
Expand All @@ -384,6 +388,7 @@ export const renderArrayFieldComponent = (
factory.createIdentifier('r'),
undefined,
undefined,
undefined,
),
],
undefined,
Expand All @@ -399,11 +404,26 @@ export const renderArrayFieldComponent = (
),
],
),
]),
),
],
NodeFlags.Const,
),
),
);
setStateStatements.push(
factory.createIfStatement(
factory.createIdentifier(selectedRecordName),
factory.createBlock([
factory.createExpressionStatement(
factory.createCallExpression(
getSetNameIdentifier(`selected${capitalizeFirstLetter(fieldName)}Records`),
undefined,
[factory.createArrayLiteralExpression([factory.createIdentifier(selectedRecordName)])],
),
),
]),
),
);
}

props.push(
Expand Down

0 comments on commit 604e0e9

Please sign in to comment.