Skip to content

Commit c98bf6b

Browse files
committed
Updated mapResponseToFormRecursive
Updated mapResponseToFormRecursive to also pupulate repeatable required groups with _itemKey
1 parent b2de358 commit c98bf6b

File tree

3 files changed

+24
-16
lines changed

3 files changed

+24
-16
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "sdc-qrf",
33
"license": "MIT",
4-
"version": "0.3.20",
4+
"version": "0.3.21",
55
"scripts": {
66
"test": "vitest --watch=false",
77
"test:watch": "vitest --watch",

src/utils.ts

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ function mapResponseToFormRecursive(
274274
questionnaireItems: QuestionnaireItem[],
275275
): FormItems {
276276
return questionnaireItems.reduce((acc, question) => {
277-
const { linkId, initial, repeats, text } = question;
277+
const { linkId, initial, repeats, text, required } = question;
278278

279279
if (!linkId) {
280280
console.warn('The question has no linkId');
@@ -287,20 +287,30 @@ function mapResponseToFormRecursive(
287287
if (repeats) {
288288
return {
289289
...acc,
290-
[linkId]: populateItemKey({
290+
[linkId]: {
291291
question: text,
292292
items: qrItems.map((qrItem) => {
293293
return mapResponseToFormRecursive(qrItem.item ?? [], question.item ?? []);
294294
}),
295-
}),
295+
},
296296
};
297297
} else {
298298
return {
299299
...acc,
300-
[linkId]: populateItemKey({
300+
[linkId]: {
301301
question: text,
302302
items: mapResponseToFormRecursive(qrItems[0]?.item ?? [], question.item ?? []),
303-
}),
303+
},
304+
};
305+
}
306+
} else {
307+
if (repeats && required) {
308+
return {
309+
...acc,
310+
[linkId]: {
311+
question: text,
312+
items: [populateItemKey({})],
313+
},
304314
};
305315
}
306316
}
@@ -315,15 +325,13 @@ function mapResponseToFormRecursive(
315325

316326
return {
317327
...acc,
318-
[linkId]: answers.map((answer) =>
319-
populateItemKey({
320-
question: text,
321-
value: answer.value,
322-
items: mapResponseToFormRecursive(answer.item ?? [], question.item ?? []),
323-
}),
324-
),
328+
[linkId]: answers.map((answer) => ({
329+
question: text,
330+
value: answer.value,
331+
items: mapResponseToFormRecursive(answer.item ?? [], question.item ?? []),
332+
})),
325333
};
326-
}, {});
334+
}, populateItemKey({}));
327335
}
328336

329337
export function mapResponseToForm(resource: QuestionnaireResponse, questionnaire: Questionnaire) {

0 commit comments

Comments
 (0)