@@ -30,22 +30,37 @@ interface QuestionModalProps {
3030}
3131
3232/**
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.
3534 */
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 : [ ] ,
4751} ;
4852
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+
4964/**
5065 * Mapping of allowed keys for the nested questionOptions object per question type.
5166 */
@@ -81,12 +96,11 @@ function cleanQuestionOptionsForType(options: any, newType: string): any {
8196 */
8297function cleanFormFieldForType ( field : FormField , newType : string ) : FormField {
8398 const allowedKeys = allowedPropertiesMapping [ newType ] || [ ] ;
84- const cleaned : Partial < FormField > = { } as Partial < FormField > ;
99+ const cleaned : Partial < FormField > = { } ;
85100
86- // Copy only allowed top‑level properties.
87- ( allowedKeys as ( keyof FormField ) [ ] ) . forEach ( ( key ) => {
101+ allowedKeys . forEach ( ( key ) => {
88102 if ( key in field ) {
89- ( cleaned as any ) [ key ] = field [ key ] ;
103+ ( cleaned as any ) [ key ] = field [ key as keyof FormField ] ;
90104 }
91105 } ) ;
92106
@@ -95,7 +109,9 @@ function cleanFormFieldForType(field: FormField, newType: string): FormField {
95109 cleaned . questionOptions = cleanQuestionOptionsForType ( cleaned . questionOptions , newType ) ;
96110 }
97111
98- return { ...cleaned , type : newType } as FormField ;
112+ cleaned . type = newType ;
113+
114+ return cleaned as FormField ;
99115}
100116
101117const QuestionModalContent : React . FC < QuestionModalProps > = ( {
@@ -158,12 +174,11 @@ const QuestionModalContent: React.FC<QuestionModalProps> = ({
158174 } ) ) ;
159175 } , [ setFormField ] ) ;
160176
161- // Updated deletion: filter out the question by its id.
162177 const deleteObsGroupQuestion = useCallback (
163- ( id : string ) => {
178+ ( index : number ) => {
164179 setFormField ( ( prevFormField ) => ( {
165180 ...prevFormField ,
166- questions : prevFormField . questions ?. filter ( ( q ) => q . id !== id ) || [ ] ,
181+ questions : prevFormField . questions ?. filter ( ( _ , i ) => i !== index ) || [ ] ,
167182 } ) ) ;
168183 } ,
169184 [ setFormField ] ,
@@ -241,7 +256,7 @@ const QuestionModalContent: React.FC<QuestionModalProps> = ({
241256 < Question checkIfQuestionIdExists = { checkIfQuestionIdExists } />
242257 < Button
243258 kind = "danger"
244- onClick = { ( ) => deleteObsGroupQuestion ( question . id ) }
259+ onClick = { ( ) => deleteObsGroupQuestion ( index ) }
245260 className = { styles . deleteObsGroupQuestionButton }
246261 >
247262 { t ( 'deleteQuestion' , 'Delete question' ) }
0 commit comments