@@ -11,83 +11,67 @@ import {
11
11
Switch ,
12
12
TextField
13
13
} from '@mui/material' ;
14
- import { ICreateJobModel } from '../model' ;
14
+ import { INotificationsConfig } from '../model' ;
15
15
import Select , { SelectChangeEvent } from '@mui/material/Select' ;
16
16
import { Stack } from './stack' ;
17
17
import { useTranslator } from '../hooks' ;
18
18
19
19
type NotificationsConfigProps = {
20
20
notificationEvents : string [ ] ;
21
21
id : string ;
22
- model : ICreateJobModel ;
23
- handleModelChange : ( model : ICreateJobModel ) => void ;
22
+ notificationsConfig : INotificationsConfig | undefined ;
23
+ notificationsConfigChange : (
24
+ updatedFields : Partial < INotificationsConfig >
25
+ ) => void ;
24
26
} ;
25
27
26
28
export function NotificationsConfig (
27
29
props : NotificationsConfigProps
28
30
) : JSX . Element | null {
29
31
const trans = useTranslator ( 'jupyterlab' ) ;
30
32
const [ selectedEvents , setSelectedEvents ] = useState < string [ ] > (
31
- props . model . notificationsConfig ?. selectedEvents || [ ]
32
- ) ;
33
- const [ enableNotification , setEnableNotification ] = useState < boolean > (
34
- props . model . notificationsConfig ?. enableNotification ?? true
33
+ props . notificationsConfig ?. selectedEvents || [ ]
35
34
) ;
36
35
const [ sendToInput , setSendToInput ] = useState < string > (
37
- props . model . notificationsConfig ?. sendTo ?. join ( ', ' ) || ''
36
+ props . notificationsConfig ?. sendTo ?. join ( ', ' ) || ''
38
37
) ;
39
38
const [ includeOutput , setIncludeOutput ] = useState < boolean > (
40
- props . model . notificationsConfig ?. includeOutput || false
39
+ props . notificationsConfig ?. includeOutput || false
41
40
) ;
42
41
43
- const enableNotificationChange = ( e : React . ChangeEvent < HTMLInputElement > ) => {
44
- const updatedEnableNotification = e . target . checked ;
45
- setEnableNotification ( updatedEnableNotification ) ;
46
- props . handleModelChange ( {
47
- ...props . model ,
48
- notificationsConfig : {
49
- ...props . model . notificationsConfig ,
50
- enableNotification : updatedEnableNotification
51
- }
42
+ function enableNotificationChange ( e : React . ChangeEvent < HTMLInputElement > ) {
43
+ props . notificationsConfigChange ( {
44
+ enableNotification : e . target . checked
52
45
} ) ;
53
- } ;
46
+ }
54
47
55
48
const selectChange = useCallback (
56
49
( e : SelectChangeEvent < string > ) => {
57
50
const newEvent = e . target . value ;
58
51
if ( ! selectedEvents . includes ( newEvent ) ) {
59
52
const updatedEvents = [ ...selectedEvents , newEvent ] ;
60
53
setSelectedEvents ( updatedEvents ) ;
61
- props . handleModelChange ( {
62
- ...props . model ,
63
- notificationsConfig : {
64
- ...props . model . notificationsConfig ,
65
- selectedEvents : updatedEvents
66
- }
54
+ props . notificationsConfigChange ( {
55
+ selectedEvents : updatedEvents
67
56
} ) ;
68
57
}
69
58
} ,
70
- [ selectedEvents , props . model , props . handleModelChange ]
59
+ [ selectedEvents , props . notificationsConfig ]
71
60
) ;
72
61
73
- const sendToChange = useCallback ( ( e : React . ChangeEvent < HTMLInputElement > ) => {
62
+ function sendToChange ( e : React . ChangeEvent < HTMLInputElement > ) {
74
63
setSendToInput ( e . target . value ) ;
75
- } , [ ] ) ;
64
+ }
76
65
77
66
const blur = useCallback ( ( ) => {
78
67
const emailArray = sendToInput
79
68
. split ( ',' )
80
69
. map ( email => email . trim ( ) )
81
70
. filter ( email => email ) ;
82
- const updatedNotification = {
83
- ...props . model . notificationsConfig ,
71
+ props . notificationsConfigChange ( {
84
72
sendTo : emailArray
85
- } ;
86
- props . handleModelChange ( {
87
- ...props . model ,
88
- notificationsConfig : updatedNotification
89
73
} ) ;
90
- } , [ sendToInput , props . model , props . handleModelChange ] ) ;
74
+ } , [ sendToInput , props . notificationsConfigChange ] ) ;
91
75
92
76
const keyDown = useCallback (
93
77
( e : React . KeyboardEvent ) => {
@@ -99,33 +83,28 @@ export function NotificationsConfig(
99
83
[ blur ]
100
84
) ;
101
85
102
- const includeOutputChange = ( event : React . ChangeEvent < HTMLInputElement > ) => {
103
- const updatedValue = event . target . checked ;
104
- setIncludeOutput ( updatedValue ) ;
105
- props . handleModelChange ( {
106
- ...props . model ,
107
- notificationsConfig : {
108
- ...props . model . notificationsConfig ,
86
+ const includeOutputChange = useCallback (
87
+ ( event : React . ChangeEvent < HTMLInputElement > ) => {
88
+ const updatedValue = event . target . checked ;
89
+ setIncludeOutput ( updatedValue ) ;
90
+ props . notificationsConfigChange ( {
109
91
includeOutput : updatedValue
110
- }
111
- } ) ;
112
- } ;
92
+ } ) ;
93
+ } ,
94
+ [ props . notificationsConfigChange ]
95
+ ) ;
113
96
114
97
const deleteSelectedEvent = useCallback (
115
98
( eventToDelete : string ) => ( ) => {
116
99
const updatedEvents = selectedEvents . filter (
117
100
event => event !== eventToDelete
118
101
) ;
119
102
setSelectedEvents ( updatedEvents ) ;
120
- props . handleModelChange ( {
121
- ...props . model ,
122
- notifications : {
123
- ...props . model . notificationsConfig ,
124
- selectedEvents : updatedEvents
125
- }
103
+ props . notificationsConfigChange ( {
104
+ selectedEvents : updatedEvents
126
105
} ) ;
127
106
} ,
128
- [ selectedEvents , props . model , props . handleModelChange ]
107
+ [ selectedEvents , props . notificationsConfigChange ]
129
108
) ;
130
109
131
110
if ( ! props . notificationEvents . length ) {
@@ -138,7 +117,7 @@ export function NotificationsConfig(
138
117
< FormControlLabel
139
118
control = {
140
119
< Switch
141
- checked = { enableNotification }
120
+ checked = { props . notificationsConfig ?. enableNotification ?? true }
142
121
onChange = { enableNotificationChange }
143
122
/>
144
123
}
@@ -152,27 +131,27 @@ export function NotificationsConfig(
152
131
onChange = { sendToChange }
153
132
onBlur = { blur }
154
133
onKeyDown = { keyDown }
155
- disabled = { ! enableNotification }
134
+ disabled = { ! props . notificationsConfig ?. enableNotification }
156
135
/>
157
136
< NotificationEventsSelect
158
137
id = { props . id }
159
138
value = { props . notificationEvents . filter (
160
139
e => ! selectedEvents . includes ( e )
161
140
) }
162
141
onChange = { selectChange }
163
- disabled = { ! enableNotification }
142
+ disabled = { ! props . notificationsConfig ?. enableNotification }
164
143
/>
165
144
< SelectedEventsChips
166
145
value = { selectedEvents }
167
146
onChange = { deleteSelectedEvent }
168
- disabled = { ! enableNotification }
147
+ disabled = { ! props . notificationsConfig ?. enableNotification }
169
148
/>
170
149
< FormControlLabel
171
150
control = {
172
151
< Checkbox
173
152
checked = { includeOutput }
174
153
onChange = { includeOutputChange }
175
- disabled = { ! enableNotification }
154
+ disabled = { ! props . notificationsConfig ?. enableNotification }
176
155
/>
177
156
}
178
157
label = { trans . __ ( 'Include output' ) }
0 commit comments