@@ -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+
3237const 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 ( ( ) => {
0 commit comments