From 6e72b1484d6c33a313c5fc7c48a1e3810bf6c975 Mon Sep 17 00:00:00 2001 From: Johan Stokking Date: Fri, 13 Dec 2024 16:06:31 +0100 Subject: [PATCH] fixup! console: Conditionally show WiFi and ethernet config and connections --- .../connection-settings/index.js | 95 +++++++++---------- pkg/webui/console/store/selectors/gateways.js | 2 +- 2 files changed, 46 insertions(+), 51 deletions(-) diff --git a/pkg/webui/console/containers/gateway-managed-gateway/connection-settings/index.js b/pkg/webui/console/containers/gateway-managed-gateway/connection-settings/index.js index 778a775b034..d874f789091 100644 --- a/pkg/webui/console/containers/gateway-managed-gateway/connection-settings/index.js +++ b/pkg/webui/console/containers/gateway-managed-gateway/connection-settings/index.js @@ -195,8 +195,7 @@ const GatewayConnectionSettings = () => { }, [dispatch, hasEthernetProfileSet, selectedManagedGateway.ethernet_profile_id]) const updateInitialWifiProfile = useCallback( - (values, profile, entityId, isNonSharedProfile) => ({ - ...values.wifi_profile, + (profile, entityId, isNonSharedProfile) => ({ ...(isNonSharedProfile && { ...profile.data }), profile_id: isNonSharedProfile ? 'non-shared' @@ -209,8 +208,7 @@ const GatewayConnectionSettings = () => { ) const updateInitialEthernetProfile = useCallback( - (values, profile) => ({ - ...values.ethernet_profile, + profile => ({ ...revertEthernetProfile(profile?.data ?? {}), profile_id: selectedManagedGateway.ethernet_profile_id ?? '', }), @@ -219,44 +217,45 @@ const GatewayConnectionSettings = () => { const loadData = useCallback( async dispatch => { - let collaborators = [] - if (mayViewCollaborators) { - const { entities } = await dispatch(attachPromise(getCollaboratorsList('gateway', gtwId))) - collaborators = entities + const fetchCollaborators = async () => { + if (mayViewCollaborators) { + const { entities } = await dispatch(attachPromise(getCollaboratorsList('gateway', gtwId))) + return entities + } + return [] } - let newValues = {} + + const newValues = {} + if (hasWifi) { + const collaborators = await fetchCollaborators() const { wifiProfile, wifiProfileEntityId } = await fetchWifiProfile(collaborators) const isNonSharedWifiProfile = Boolean(wifiProfile) && !Boolean(wifiProfile.data.shared) + if (isNonSharedWifiProfile) { setNonSharedWifiProfileId(selectedManagedGateway.wifi_profile_id) } - newValues = { - ...newValues, - wifi_profile: updateInitialWifiProfile( - initialValues, - wifiProfile, - wifiProfileEntityId, - isNonSharedWifiProfile, - ), - } + + newValues.wifi_profile = updateInitialWifiProfile( + wifiProfile, + wifiProfileEntityId, + isNonSharedWifiProfile, + ) } + if (hasEthernet) { const { ethernetProfile } = await fetchEthernetProfile() - newValues = { - ...newValues, - ethernet_profile: updateInitialEthernetProfile(initialValues, ethernetProfile), - } + newValues.ethernet_profile = updateInitialEthernetProfile(ethernetProfile) } - setInitialValues(oldValues => ({ - ...oldValues, - ...newValues, - })) + + setInitialValues(oldValues => ({ ...oldValues, ...newValues })) }, [ fetchEthernetProfile, fetchWifiProfile, gtwId, + hasEthernet, + hasWifi, mayViewCollaborators, selectedManagedGateway.wifi_profile_id, updateInitialEthernetProfile, @@ -332,47 +331,41 @@ const GatewayConnectionSettings = () => { let body = {} const { wifi_profile, ethernet_profile } = values - let wifiProfileId if (hasWifi) { const shouldUpdateNonSharedWifiProfile = wifi_profile.profile_id === 'non-shared' && Boolean(nonSharedWifiProfileId) && wifi_profile._enable_wifi_connection - wifiProfileId = await getWifiProfileId(wifi_profile, shouldUpdateNonSharedWifiProfile) - body = { - ...body, - wifi_profile_id: - !wifi_profile._enable_wifi_connection && !wifi_profile._override - ? null - : wifiProfileId, + if (!wifi_profile._enable_wifi_connection && !wifi_profile._override) { + body.wifi_profile_id = null + } else { + body.wifi_profile_id = await getWifiProfileId( + wifi_profile, + shouldUpdateNonSharedWifiProfile, + ) } } if (hasEthernet) { - const ethernetProfileId = await getEthernetProfileId( + body.ethernet_profile_id = await getEthernetProfileId( ethernet_profile, cleanValues.ethernet_profile, ) - body = { - ...body, - ethernet_profile_id: ethernetProfileId, - } } await dispatch(attachPromise(updateManagedGateway(gtwId, body))) // Reset the form and the initial values - let resetValues = { ...values } - if (hasWifi && wifi_profile.profile_id !== 'non-shared') { - resetValues = { - ...resetValues, - wifi_profile: { - ...values.wifi_profile, - ...initialWifiProfile, - profile_id: wifiProfileId, - _profile_of: wifi_profile._profile_of, - }, - } + const resetValues = { + ...values, + ...(hasWifi && + wifi_profile.profile_id !== 'non-shared' && { + wifi_profile: { + ...initialWifiProfile, + profile_id: body.wifi_profile_id, + _profile_of: wifi_profile._profile_of, + }, + }), } setInitialValues(resetValues) @@ -402,6 +395,8 @@ const GatewayConnectionSettings = () => { [ dispatch, gtwId, + hasEthernet, + hasWifi, hasEthernetProfileSet, initialValues.ethernet_profile, initialValues.wifi_profile, diff --git a/pkg/webui/console/store/selectors/gateways.js b/pkg/webui/console/store/selectors/gateways.js index 69d1bba2db0..1ca5e69251a 100644 --- a/pkg/webui/console/store/selectors/gateways.js +++ b/pkg/webui/console/store/selectors/gateways.js @@ -57,7 +57,7 @@ export const selectSelectedManagedGateway = state => selectSelectedGateway(state export const selectSelectedManagedGatewayHasCellular = state => Boolean(selectSelectedManagedGateway(state)?.capabilities?.cellular) export const selectSelectedManagedGatewayHasWifi = state => { - const capabilities = selectSelectedManagedGateway(state)?.capabilities; + const capabilities = selectSelectedManagedGateway(state)?.capabilities return Boolean(capabilities?.wifi_2_4_ghz) || Boolean(capabilities?.wifi_5_ghz) } export const selectSelectedManagedGatewayHasEthernet = state =>