Skip to content

Commit

Permalink
fixup! console: Conditionally show WiFi and ethernet config and conne…
Browse files Browse the repository at this point in the history
…ctions
  • Loading branch information
johanstokking committed Dec 13, 2024
1 parent c91acf8 commit 6e72b14
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -209,8 +208,7 @@ const GatewayConnectionSettings = () => {
)

const updateInitialEthernetProfile = useCallback(
(values, profile) => ({
...values.ethernet_profile,
profile => ({
...revertEthernetProfile(profile?.data ?? {}),
profile_id: selectedManagedGateway.ethernet_profile_id ?? '',
}),
Expand All @@ -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,
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -402,6 +395,8 @@ const GatewayConnectionSettings = () => {
[
dispatch,
gtwId,
hasEthernet,
hasWifi,
hasEthernetProfileSet,
initialValues.ethernet_profile,
initialValues.wifi_profile,
Expand Down
2 changes: 1 addition & 1 deletion pkg/webui/console/store/selectors/gateways.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 =>
Expand Down

0 comments on commit 6e72b14

Please sign in to comment.