@@ -30,22 +30,37 @@ interface QuestionModalProps {
30
30
}
31
31
32
32
/**
33
- * Mapping of allowed top‑level property keys for each question type.
34
- * Adjust these keys as needed for your implementation.
33
+ * Common properties that are required for all question types.
35
34
*/
36
- const allowedPropertiesMapping : Record < string , string [ ] > = {
37
- control : [ 'id' , 'label' , 'type' , 'questionOptions' ] ,
38
- encounterDatetime : [ 'id' , 'label' , 'type' , 'questionOptions' , 'datePickerFormat' ] ,
39
- encounterLocation : [ 'id' , 'label' , 'type' , 'questionOptions' ] ,
40
- encounterProvider : [ 'id' , 'label' , 'type' , 'questionOptions' ] ,
41
- encounterRole : [ 'id' , 'label' , 'type' , 'questionOptions' ] ,
42
- obs : [ 'id' , 'label' , 'type' , 'questionOptions' ] ,
43
- obsGroup : [ 'id' , 'label' , 'type' , 'questionOptions' , 'questions' ] ,
44
- patientIdentifier : [ 'id' , 'label' , 'type' , 'questionOptions' ] ,
45
- testOrder : [ 'id' , 'label' , 'type' , 'questionOptions' ] ,
46
- programState : [ 'id' , 'label' , 'type' , 'questionOptions' ] ,
35
+ const requiredProperties : Array < keyof FormField > = [ 'id' , 'label' , 'type' , 'questionOptions' ] ;
36
+
37
+ /**
38
+ * Type-specific properties.
39
+ */
40
+ const typeSpecificProperties : Record < string , Array < keyof FormField > > = {
41
+ control : [ ] ,
42
+ encounterDatetime : [ 'datePickerFormat' ] ,
43
+ encounterLocation : [ ] ,
44
+ encounterProvider : [ ] ,
45
+ encounterRole : [ ] ,
46
+ obs : [ ] ,
47
+ obsGroup : [ 'questions' ] ,
48
+ patientIdentifier : [ ] ,
49
+ testOrder : [ ] ,
50
+ programState : [ ] ,
47
51
} ;
48
52
53
+ /**
54
+ * Build a final mapping of allowed top-level properties for each question type,
55
+ * merging the requiredProperties with any type-specific properties.
56
+ */
57
+ const allowedPropertiesMapping : Record < string , string [ ] > = Object . fromEntries (
58
+ Object . entries ( typeSpecificProperties ) . map ( ( [ type , props ] ) => {
59
+ const mergedProps = new Set < string > ( [ ...requiredProperties , ...props ] ) ;
60
+ return [ type , Array . from ( mergedProps ) ] ;
61
+ } ) ,
62
+ ) ;
63
+
49
64
/**
50
65
* Mapping of allowed keys for the nested questionOptions object per question type.
51
66
*/
@@ -81,12 +96,11 @@ function cleanQuestionOptionsForType(options: any, newType: string): any {
81
96
*/
82
97
function cleanFormFieldForType ( field : FormField , newType : string ) : FormField {
83
98
const allowedKeys = allowedPropertiesMapping [ newType ] || [ ] ;
84
- const cleaned : Partial < FormField > = { } as Partial < FormField > ;
99
+ const cleaned : Partial < FormField > = { } ;
85
100
86
- // Copy only allowed top‑level properties.
87
- ( allowedKeys as ( keyof FormField ) [ ] ) . forEach ( ( key ) => {
101
+ allowedKeys . forEach ( ( key ) => {
88
102
if ( key in field ) {
89
- ( cleaned as any ) [ key ] = field [ key ] ;
103
+ ( cleaned as any ) [ key ] = field [ key as keyof FormField ] ;
90
104
}
91
105
} ) ;
92
106
@@ -95,7 +109,9 @@ function cleanFormFieldForType(field: FormField, newType: string): FormField {
95
109
cleaned . questionOptions = cleanQuestionOptionsForType ( cleaned . questionOptions , newType ) ;
96
110
}
97
111
98
- return { ...cleaned , type : newType } as FormField ;
112
+ cleaned . type = newType ;
113
+
114
+ return cleaned as FormField ;
99
115
}
100
116
101
117
const QuestionModalContent : React . FC < QuestionModalProps > = ( {
@@ -158,12 +174,11 @@ const QuestionModalContent: React.FC<QuestionModalProps> = ({
158
174
} ) ) ;
159
175
} , [ setFormField ] ) ;
160
176
161
- // Updated deletion: filter out the question by its id.
162
177
const deleteObsGroupQuestion = useCallback (
163
- ( id : string ) => {
178
+ ( index : number ) => {
164
179
setFormField ( ( prevFormField ) => ( {
165
180
...prevFormField ,
166
- questions : prevFormField . questions ?. filter ( ( q ) => q . id !== id ) || [ ] ,
181
+ questions : prevFormField . questions ?. filter ( ( _ , i ) => i !== index ) || [ ] ,
167
182
} ) ) ;
168
183
} ,
169
184
[ setFormField ] ,
@@ -241,7 +256,7 @@ const QuestionModalContent: React.FC<QuestionModalProps> = ({
241
256
< Question checkIfQuestionIdExists = { checkIfQuestionIdExists } />
242
257
< Button
243
258
kind = "danger"
244
- onClick = { ( ) => deleteObsGroupQuestion ( question . id ) }
259
+ onClick = { ( ) => deleteObsGroupQuestion ( index ) }
245
260
className = { styles . deleteObsGroupQuestionButton }
246
261
>
247
262
{ t ( 'deleteQuestion' , 'Delete question' ) }
0 commit comments