Skip to content

Commit cc4b189

Browse files
(fix) O3-4489: obsGroup child question id gets flagged as duplicate (#409)
* (fix) obsGroup child question id gets flagged as duplicate * update yarn.lock * update yarn.lock
1 parent e5bd73c commit cc4b189

File tree

2 files changed

+31
-15
lines changed

2 files changed

+31
-15
lines changed

src/components/interactive-builder/modals/question/question.modal.tsx

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ interface QuestionModalProps {
2929
resetIndices: () => void;
3030
}
3131

32+
const getAllQuestionIds = (questions?: FormField[]): string[] => {
33+
if (!questions) return [];
34+
return flattenDeep(questions.map((question) => [question.id, getAllQuestionIds(question.questions)]));
35+
};
36+
3237
const QuestionModalContent: React.FC<QuestionModalProps> = ({
3338
formField: formFieldProp,
3439
closeModal,
@@ -41,32 +46,43 @@ const QuestionModalContent: React.FC<QuestionModalProps> = ({
4146
const { t } = useTranslation();
4247
const { formField, setFormField } = useFormField();
4348

44-
const getAllQuestionIds = useCallback((questions?: FormField[]): string[] => {
45-
if (!questions) return [];
46-
return flattenDeep(questions.map((question) => [question.id, getAllQuestionIds(question.questions)]));
47-
}, []);
48-
49+
/**
50+
* NOTE - this does not support nested obsGroup questions
51+
*/
4952
const checkIfQuestionIdExists = useCallback(
5053
(idToTest: string): boolean => {
5154
// Get all IDs from the schema
5255
const schemaIds: string[] =
5356
schema?.pages?.flatMap((page) => page?.sections?.flatMap((section) => getAllQuestionIds(section.questions))) ||
5457
[];
5558

56-
// Get all IDs from the current formField's questions array
59+
// Get all IDs from the obsGroup questions
5760
const formFieldIds: string[] = formField?.questions ? getAllQuestionIds(formField.questions) : [];
5861

62+
// The main question's id
63+
formFieldIds.push(formField.id);
64+
65+
const originalFormFieldQuestionIds =
66+
formFieldProp && formFieldProp.id !== '' ? getAllQuestionIds(formFieldProp.questions) : [];
67+
68+
if (formFieldProp && formFieldProp.id !== '') originalFormFieldQuestionIds.push(formFieldProp.id);
69+
70+
// Remove the ids from the original question from the schema ids
71+
const filteredSchemaIds = schemaIds.slice(); // Create a copy to modify
72+
originalFormFieldQuestionIds.forEach((idToRemove) => {
73+
const indexToRemove = filteredSchemaIds.indexOf(idToRemove);
74+
if (indexToRemove !== -1) {
75+
filteredSchemaIds.splice(indexToRemove, 1);
76+
}
77+
});
78+
5979
// Combine both arrays, along with the parent question ID and count occurrences of the ID
60-
const allIds = [...schemaIds, ...formFieldIds];
61-
if (!formFieldProp || formFieldProp.id !== formField.id) {
62-
allIds.push(formField.id);
63-
}
80+
const allIds = [...filteredSchemaIds, ...formFieldIds];
6481
const occurrences = allIds.filter((id) => id === idToTest).length;
6582

66-
// Return true if ID occurs more than once
6783
return occurrences > 1;
6884
},
69-
[schema, getAllQuestionIds, formField, formFieldProp],
85+
[schema, formField, formFieldProp],
7086
);
7187

7288
const addObsGroupQuestion = useCallback(() => {

yarn.lock

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3738,8 +3738,8 @@ __metadata:
37383738
linkType: hard
37393739

37403740
"@openmrs/esm-patient-common-lib@npm:next":
3741-
version: 9.2.1-pre.6870
3742-
resolution: "@openmrs/esm-patient-common-lib@npm:9.2.1-pre.6870"
3741+
version: 9.2.2-pre.6922
3742+
resolution: "@openmrs/esm-patient-common-lib@npm:9.2.2-pre.6922"
37433743
dependencies:
37443744
"@carbon/react": "npm:^1.12.0"
37453745
lodash-es: "npm:^4.17.21"
@@ -3748,7 +3748,7 @@ __metadata:
37483748
"@openmrs/esm-framework": 6.x
37493749
react: 18.x
37503750
single-spa: 6.x
3751-
checksum: 10/0891a1c2b020d16c5dc57c69dd58bbcaac1ef081686a038a16a3b3fc18abe71b03fb26360b39dd421c862d7ec3f63ec70e4ccf0b08fc726a29188f55c6f6ec26
3751+
checksum: 10/41001b0beba68c277b2013bd6457dc3c742436f188fec23ae3ea395bca3802c6aae725eac783a00110c434100615c1f41a7348b01a7789ded180d75bbb459d18
37523752
languageName: node
37533753
linkType: hard
37543754

0 commit comments

Comments
 (0)