Skip to content

Commit

Permalink
Merge pull request #1001 from aehrc/dev
Browse files Browse the repository at this point in the history
Merging dev into e request demo
  • Loading branch information
fongsean authored Sep 9, 2024
2 parents 271a955 + 9e4f66d commit f88d723
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion packages/smart-forms-renderer/src/utils/removeEmptyAnswers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export function removeEmptyAnswers(params: removeEmptyAnswersParams): Questionna
return updatedQuestionnaireResponse;
}

// introduce the mapper here
const newQuestionnaireResponse: QuestionnaireResponse = { ...questionnaireResponse, item: [] };
for (const [i, topLevelQRItem] of topLevelQRItems.entries()) {
const qItem = topLevelQItems[i];
Expand Down Expand Up @@ -133,8 +134,11 @@ function removeEmptyAnswersFromItemRecursive(
qItemIndex < qItems.length || qrItemIndex < qrItems.length;
qItemIndex++
) {
const initialQItemIndex = qItemIndex;
const initialQrItemIndex = qrItemIndex;

// Save qrItem if linkIds of current qItem and qrItem are the same
if (qrItems[qrItemIndex] && qItems[qItemIndex].linkId === qrItems[qrItemIndex].linkId) {
if (qrItems[qrItemIndex] && qItems[qItemIndex]?.linkId === qrItems[qrItemIndex]?.linkId) {
const newQrItem = removeEmptyAnswersFromItemRecursive({
qItem: qItems[qItemIndex],
qrItem: qrItems[qrItemIndex],
Expand All @@ -159,6 +163,14 @@ function removeEmptyAnswersFromItemRecursive(
// Only Increment qrItem index whenever the current qrItem linkId matches up with the current qItem
qrItemIndex++;
}

// Fail-safe: If neither index was incremented or decremented, break to prevent infinite loop
if (qItemIndex === initialQItemIndex && qrItemIndex === initialQrItemIndex) {
console.error(
'Possible infinite loop detected in removeEmptyAnswersFromItemRecursive(), breaking the loop'
);
break;
}
}
return { ...qrItem, item: newQrItems };
}
Expand Down

0 comments on commit f88d723

Please sign in to comment.