@@ -83,29 +83,26 @@ const CalendarEventsModal = ({
83
83
return errors ;
84
84
} ;
85
85
86
- // TODO? - separate change handlers for checkboxes:
87
- // const onPolicyUpdate = ...
88
- // const onTextFieldUpdate = ...
89
-
90
- const onInputChange = useCallback (
91
- ( newVal : { name : FormNames ; value : string | number | boolean } ) => {
86
+ // two onChange handlers to handle different levels of nesting in the form data
87
+ const onFeatureEnabledOrUrlChange = useCallback (
88
+ ( newVal : { name : "enabled" | "url" ; value : string | boolean } ) => {
92
89
const { name, value } = newVal ;
93
- let newFormData : ICalendarEventsFormData ;
94
- // for the first two fields, set the new value directly
95
- if ( [ "enabled" , "url" ] . includes ( name ) ) {
96
- newFormData = { ... formData , [ name ] : value } ;
97
- } else if ( typeof value === "boolean" ) {
98
- // otherwise, set the value for a nested policy
99
- const newFormPolicies = formData . policies . map ( ( formPolicy ) => {
100
- if ( formPolicy . name === name ) {
101
- return { ... formPolicy , isChecked : value } ;
102
- }
103
- return formPolicy ;
104
- } ) ;
105
- newFormData = { ... formData , policies : newFormPolicies } ;
106
- } else {
107
- throw TypeError ( "Unexpected value type for policy checkbox" ) ;
108
- }
90
+ const newFormData = { ... formData , [ name ] : value } ;
91
+ setFormData ( newFormData ) ;
92
+ setFormErrors ( validateCalendarEventsFormData ( newFormData ) ) ;
93
+ } ,
94
+ [ formData ]
95
+ ) ;
96
+ const onPolicyEnabledChange = useCallback (
97
+ ( newVal : { name : FormNames ; value : boolean } ) => {
98
+ const { name , value } = newVal ;
99
+ const newFormPolicies = formData . policies . map ( ( formPolicy ) => {
100
+ if ( formPolicy . name === name ) {
101
+ return { ... formPolicy , isChecked : value } ;
102
+ }
103
+ return formPolicy ;
104
+ } ) ;
105
+ const newFormData = { ... formData , policies : newFormPolicies } ;
109
106
setFormData ( newFormData ) ;
110
107
setFormErrors ( validateCalendarEventsFormData ( newFormData ) ) ;
111
108
} ,
@@ -153,7 +150,7 @@ const CalendarEventsModal = ({
153
150
name = { name }
154
151
// can't use parseTarget as value needs to be set to !currentValue
155
152
onChange = { ( ) => {
156
- onInputChange ( { name, value : ! isChecked } ) ;
153
+ onPolicyEnabledChange ( { name, value : ! isChecked } ) ;
157
154
} }
158
155
>
159
156
{ name }
@@ -228,7 +225,10 @@ const CalendarEventsModal = ({
228
225
< Slider
229
226
value = { formData . enabled }
230
227
onChange = { ( ) => {
231
- onInputChange ( { name : "enabled" , value : ! formData . enabled } ) ;
228
+ onFeatureEnabledOrUrlChange ( {
229
+ name : "enabled" ,
230
+ value : ! formData . enabled ,
231
+ } ) ;
232
232
} }
233
233
inactiveText = "Disabled"
234
234
activeText = "Enabled"
@@ -247,7 +247,7 @@ const CalendarEventsModal = ({
247
247
< InputField
248
248
placeholder = "https://server.com/example"
249
249
label = "Resolution webhook URL"
250
- onChange = { onInputChange }
250
+ onChange = { onFeatureEnabledOrUrlChange }
251
251
name = "url"
252
252
value = { formData . url }
253
253
parseTarget
0 commit comments