@@ -63,12 +63,15 @@ import { updateManagedGateway } from '@console/store/actions/gateways'
63
63
import {
64
64
selectSelectedGateway ,
65
65
selectSelectedManagedGateway ,
66
+ selectSelectedManagedGatewayHasWifi ,
67
+ selectSelectedManagedGatewayHasEthernet ,
66
68
} from '@console/store/selectors/gateways'
67
69
import { selectUserId } from '@account/store/selectors/user'
68
70
69
71
const m = defineMessages ( {
70
72
firstNotification :
71
- 'You have just claimed a managed gateway. To connect it to WiFi or ethernet you can configure those connections here. The preprovisioned cellular backhaul typically connects automatically.' ,
73
+ 'You have just claimed a managed gateway. You can configure its connection settings here.' ,
74
+ noConnectionSettings : 'This gateway does not have any connection settings to configure.' ,
72
75
updateSuccess : 'Connection settings updated' ,
73
76
updateFailure : 'There was an error updating these connection settings' ,
74
77
} )
@@ -91,14 +94,16 @@ const GatewayConnectionSettings = () => {
91
94
92
95
const connectionsData = useConnectionsData ( )
93
96
97
+ const hasWifi = useSelector ( selectSelectedManagedGatewayHasWifi )
98
+ const hasWifiProfileSet = hasWifi && Boolean ( selectedManagedGateway . wifi_profile_id )
99
+ const hasEthernet = useSelector ( selectSelectedManagedGatewayHasEthernet )
100
+ const hasEthernetProfileSet = hasEthernet && Boolean ( selectedManagedGateway . ethernet_profile_id )
101
+
94
102
const [ initialValues , setInitialValues ] = useState ( {
95
- wifi_profile : { ...initialWifiProfile } ,
96
- ethernet_profile : { ...initialEthernetProfile } ,
103
+ ... ( hasWifi && { wifi_profile : { ...initialWifiProfile } } ) ,
104
+ ... ( hasEthernet && { ethernet_profile : { ...initialEthernetProfile } } ) ,
97
105
} )
98
106
99
- const hasWifiProfileSet = Boolean ( selectedManagedGateway . wifi_profile_id )
100
- const hasEthernetProfileSet = Boolean ( selectedManagedGateway . ethernet_profile_id )
101
-
102
107
useBreadcrumbs (
103
108
'gtws.single.managed-gateway.connection-settings' ,
104
109
< Breadcrumb
@@ -110,7 +115,7 @@ const GatewayConnectionSettings = () => {
110
115
const fetchWifiProfile = useCallback (
111
116
async collaborators => {
112
117
let wifiProfile
113
- let entityId
118
+ let wifiProfileEntityId
114
119
115
120
if ( hasWifiProfileSet ) {
116
121
setError ( undefined )
@@ -125,7 +130,7 @@ const GatewayConnectionSettings = () => {
125
130
) ,
126
131
) ,
127
132
)
128
- entityId = userId
133
+ wifiProfileEntityId = userId
129
134
} catch ( e ) {
130
135
if ( ! isNotFoundError ( e ) ) {
131
136
setError ( e )
@@ -150,7 +155,7 @@ const GatewayConnectionSettings = () => {
150
155
) ,
151
156
) ,
152
157
)
153
- entityId = orgId
158
+ wifiProfileEntityId = orgId
154
159
break
155
160
} catch ( e ) {
156
161
if ( ! isNotFoundError ( e ) ) {
@@ -161,7 +166,7 @@ const GatewayConnectionSettings = () => {
161
166
}
162
167
}
163
168
}
164
- return { wifiProfile, entityId }
169
+ return { wifiProfile, wifiProfileEntityId }
165
170
} ,
166
171
[ dispatch , hasWifiProfileSet , selectedManagedGateway . wifi_profile_id , userId ] ,
167
172
)
@@ -190,8 +195,8 @@ const GatewayConnectionSettings = () => {
190
195
} , [ dispatch , hasEthernetProfileSet , selectedManagedGateway . ethernet_profile_id ] )
191
196
192
197
const updateInitialWifiProfile = useCallback (
193
- ( values , profile , entityId , isNonSharedProfile ) => ( {
194
- ...values . wifi_profile ,
198
+ ( profile , entityId , isNonSharedProfile ) => ( {
199
+ ...initialWifiProfile ,
195
200
...( isNonSharedProfile && { ...profile . data } ) ,
196
201
profile_id : isNonSharedProfile
197
202
? 'non-shared'
@@ -204,8 +209,8 @@ const GatewayConnectionSettings = () => {
204
209
)
205
210
206
211
const updateInitialEthernetProfile = useCallback (
207
- ( values , profile ) => ( {
208
- ...values . ethernet_profile ,
212
+ profile => ( {
213
+ ...initialEthernetProfile ,
209
214
...revertEthernetProfile ( profile ?. data ?? { } ) ,
210
215
profile_id : selectedManagedGateway . ethernet_profile_id ?? '' ,
211
216
} ) ,
@@ -214,32 +219,45 @@ const GatewayConnectionSettings = () => {
214
219
215
220
const loadData = useCallback (
216
221
async dispatch => {
217
- let collaborators = [ ]
218
- if ( mayViewCollaborators ) {
219
- const { entities } = await dispatch ( attachPromise ( getCollaboratorsList ( 'gateway' , gtwId ) ) )
220
- collaborators = entities
221
- }
222
- const { wifiProfile, entityId } = await fetchWifiProfile ( collaborators )
223
- const { ethernetProfile } = await fetchEthernetProfile ( )
224
- const isNonSharedProfile = Boolean ( wifiProfile ) && ! Boolean ( wifiProfile . data . shared )
225
- if ( isNonSharedProfile ) {
226
- setNonSharedWifiProfileId ( selectedManagedGateway . wifi_profile_id )
222
+ const fetchCollaborators = async ( ) => {
223
+ if ( mayViewCollaborators ) {
224
+ const { entities } = await dispatch ( attachPromise ( getCollaboratorsList ( 'gateway' , gtwId ) ) )
225
+ return entities
226
+ }
227
+ return [ ]
227
228
}
228
- setInitialValues ( oldValues => ( {
229
- ...oldValues ,
230
- wifi_profile : updateInitialWifiProfile (
231
- oldValues ,
229
+
230
+ const newValues = { }
231
+
232
+ if ( hasWifi ) {
233
+ const collaborators = await fetchCollaborators ( )
234
+ const { wifiProfile, wifiProfileEntityId } = await fetchWifiProfile ( collaborators )
235
+ const isNonSharedWifiProfile = Boolean ( wifiProfile ) && ! Boolean ( wifiProfile . data . shared )
236
+
237
+ if ( isNonSharedWifiProfile ) {
238
+ setNonSharedWifiProfileId ( selectedManagedGateway . wifi_profile_id )
239
+ }
240
+
241
+ newValues . wifi_profile = updateInitialWifiProfile (
232
242
wifiProfile ,
233
- entityId ,
234
- isNonSharedProfile ,
235
- ) ,
236
- ethernet_profile : updateInitialEthernetProfile ( oldValues , ethernetProfile ) ,
237
- } ) )
243
+ wifiProfileEntityId ,
244
+ isNonSharedWifiProfile ,
245
+ )
246
+ }
247
+
248
+ if ( hasEthernet ) {
249
+ const { ethernetProfile } = await fetchEthernetProfile ( )
250
+ newValues . ethernet_profile = updateInitialEthernetProfile ( ethernetProfile )
251
+ }
252
+
253
+ setInitialValues ( oldValues => ( { ...oldValues , ...newValues } ) )
238
254
} ,
239
255
[
240
256
fetchEthernetProfile ,
241
257
fetchWifiProfile ,
242
258
gtwId ,
259
+ hasEthernet ,
260
+ hasWifi ,
243
261
mayViewCollaborators ,
244
262
selectedManagedGateway . wifi_profile_id ,
245
263
updateInitialEthernetProfile ,
@@ -312,46 +330,53 @@ const GatewayConnectionSettings = () => {
312
330
313
331
setError ( undefined )
314
332
try {
333
+ const body = { }
315
334
const { wifi_profile, ethernet_profile } = values
316
335
317
- const shouldUpdateNonSharedWifiProfile =
318
- wifi_profile . profile_id === 'non-shared' &&
319
- Boolean ( nonSharedWifiProfileId ) &&
320
- wifi_profile . _enable_wifi_connection
336
+ if ( hasWifi ) {
337
+ const shouldUpdateNonSharedWifiProfile =
338
+ wifi_profile . profile_id === 'non-shared' &&
339
+ Boolean ( nonSharedWifiProfileId ) &&
340
+ wifi_profile . _enable_wifi_connection
321
341
322
- const wifiProfileId = await getWifiProfileId ( wifi_profile , shouldUpdateNonSharedWifiProfile )
323
- const ethernetProfileId = await getEthernetProfileId (
324
- ethernet_profile ,
325
- cleanValues . ethernet_profile ,
326
- )
327
- const body = {
328
- wifi_profile_id :
329
- ! wifi_profile . _enable_wifi_connection && ! wifi_profile . _override ? null : wifiProfileId ,
330
- ethernet_profile_id : ethernetProfileId ,
342
+ if ( ! wifi_profile . _enable_wifi_connection && ! wifi_profile . _override ) {
343
+ body . wifi_profile_id = null
344
+ } else {
345
+ body . wifi_profile_id = await getWifiProfileId (
346
+ wifi_profile ,
347
+ shouldUpdateNonSharedWifiProfile ,
348
+ )
349
+ }
350
+ }
351
+ if ( hasEthernet ) {
352
+ body . ethernet_profile_id = await getEthernetProfileId (
353
+ ethernet_profile ,
354
+ cleanValues . ethernet_profile ,
355
+ )
331
356
}
332
357
333
358
await dispatch ( attachPromise ( updateManagedGateway ( gtwId , body ) ) )
334
359
335
360
// Reset the form and the initial values
336
- let resetValues = { ... values }
337
- if ( wifi_profile . profile_id !== 'non-shared' ) {
338
- resetValues = {
339
- ... values ,
340
- wifi_profile : {
341
- ... values . wifi_profile ,
342
- ...initialWifiProfile ,
343
- profile_id : wifiProfileId ,
344
- _profile_of : wifi_profile . _profile_of ,
345
- } ,
346
- }
361
+ const resetValues = {
362
+ ... values ,
363
+ ... ( hasWifi &&
364
+ wifi_profile . profile_id !== 'non-shared' && {
365
+ wifi_profile : {
366
+ .. .wifi_profile ,
367
+ ...initialWifiProfile ,
368
+ profile_id : body . wifi_profile_id ,
369
+ _profile_of : wifi_profile . _profile_of ,
370
+ } ,
371
+ } ) ,
347
372
}
348
373
349
374
setInitialValues ( resetValues )
350
375
resetForm ( {
351
376
values : resetValues ,
352
377
} )
353
378
354
- if ( resetValues . wifi_profile . _enable_wifi_connection ) {
379
+ if ( hasWifi && resetValues . wifi_profile . _enable_wifi_connection ) {
355
380
setSaveFormClicked ( true )
356
381
}
357
382
@@ -373,6 +398,8 @@ const GatewayConnectionSettings = () => {
373
398
[
374
399
dispatch ,
375
400
gtwId ,
401
+ hasEthernet ,
402
+ hasWifi ,
376
403
hasEthernetProfileSet ,
377
404
initialValues . ethernet_profile ,
378
405
initialValues . wifi_profile ,
@@ -386,31 +413,39 @@ const GatewayConnectionSettings = () => {
386
413
< div className = "item-12 d-flex gap-ls-s md:direction-column" >
387
414
< div className = "w-full" >
388
415
< PageTitle title = { sharedMessages . connectionSettings } />
389
- { isFirstClaim && < Notification info small content = { m . firstNotification } /> }
390
- < Form
391
- error = { error }
392
- onSubmit = { handleSubmit }
393
- initialValues = { initialValues }
394
- validationSchema = { validationSchema }
395
- >
416
+ { hasWifi || hasEthernet ? (
396
417
< >
397
- < WifiSettingsFormFields
418
+ { isFirstClaim && < Notification info small content = { m . firstNotification } /> }
419
+ < Form
420
+ error = { error }
421
+ onSubmit = { handleSubmit }
398
422
initialValues = { initialValues }
399
- isWifiConnected = { connectionsData . isWifiConnected }
400
- saveFormClicked = { saveFormClicked }
401
- />
402
- < hr />
403
- < EthernetSettingsFormFields />
404
-
405
- < SubmitBar className = "mb-cs-l" >
406
- < Form . Submit
407
- component = { SubmitButton }
408
- message = { sharedMessages . saveChanges }
409
- disabled = { isLoading }
410
- />
411
- </ SubmitBar >
423
+ validationSchema = { validationSchema }
424
+ >
425
+ < >
426
+ { hasWifi && (
427
+ < WifiSettingsFormFields
428
+ initialValues = { initialValues }
429
+ isWifiConnected = { connectionsData . isWifiConnected }
430
+ saveFormClicked = { saveFormClicked }
431
+ />
432
+ ) }
433
+ { hasWifi && hasEthernet && < hr /> }
434
+ { hasEthernet && < EthernetSettingsFormFields /> }
435
+
436
+ < SubmitBar className = "mb-cs-l" >
437
+ < Form . Submit
438
+ component = { SubmitButton }
439
+ message = { sharedMessages . saveChanges }
440
+ disabled = { isLoading }
441
+ />
442
+ </ SubmitBar >
443
+ </ >
444
+ </ Form >
412
445
</ >
413
- </ Form >
446
+ ) : (
447
+ < Notification info small content = { m . noConnectionSettings } />
448
+ ) }
414
449
</ div >
415
450
< ManagedGatewayConnections connectionsData = { connectionsData } />
416
451
</ div >
0 commit comments