From 7638e97df6ac58d708f8d7aad7e6d3570418550f Mon Sep 17 00:00:00 2001 From: Pavel Jankoski Date: Wed, 23 Oct 2024 17:03:25 +0200 Subject: [PATCH] console: Unclaim gateway on delete --- .../delete-entity-header-modal/index.js | 19 ++++++++++++++++--- .../gateway-overview-header/index.js | 10 ++++++++++ .../basic-settings-form/index.js | 14 +++++++++++++- 3 files changed, 39 insertions(+), 4 deletions(-) diff --git a/pkg/webui/console/containers/delete-entity-header-modal/index.js b/pkg/webui/console/containers/delete-entity-header-modal/index.js index cbfd526d55..a7a9983c7f 100644 --- a/pkg/webui/console/containers/delete-entity-header-modal/index.js +++ b/pkg/webui/console/containers/delete-entity-header-modal/index.js @@ -95,8 +95,16 @@ const deletedErrorMessageMap = { } const DeleteEntityHeaderModal = props => { - const { entity, entityId, entityName, visible, setVisible, setError, additionalConditions } = - props + const { + entity, + entityId, + entityName, + visible, + setVisible, + setError, + additionalConditions, + additionalAction, + } = props const lowerCaseEntity = entity.toLowerCase() const [confirmId, setConfirmId] = React.useState('') @@ -138,6 +146,9 @@ const DeleteEntityHeaderModal = props => { if (setError) { setError(undefined) } + if (additionalAction) { + await additionalAction() + } await dispatch( attachPromise( deleteEntityActionMap[entity](entityId, { purge: purgeEntity || false }), @@ -163,7 +174,7 @@ const DeleteEntityHeaderModal = props => { } setVisible(false) }, - [dispatch, entityId, navigate, purgeEntity, setError, setVisible, entity], + [setVisible, setError, additionalAction, dispatch, entity, entityId, purgeEntity, navigate], ) const loadData = useCallback( @@ -266,6 +277,7 @@ const DeleteEntityHeaderModal = props => { } DeleteEntityHeaderModal.propTypes = { + additionalAction: PropTypes.func, additionalConditions: PropTypes.bool, entity: PropTypes.string.isRequired, entityId: PropTypes.string.isRequired, @@ -279,6 +291,7 @@ DeleteEntityHeaderModal.defaultProps = { additionalConditions: false, entityName: undefined, setError: undefined, + additionalAction: undefined, } export default DeleteEntityHeaderModal diff --git a/pkg/webui/console/containers/gateway-overview-header/index.js b/pkg/webui/console/containers/gateway-overview-header/index.js index f231f72361..ce0e2f20c8 100644 --- a/pkg/webui/console/containers/gateway-overview-header/index.js +++ b/pkg/webui/console/containers/gateway-overview-header/index.js @@ -44,9 +44,11 @@ import { DELETE_BOOKMARK_BASE, deleteBookmark, } from '@console/store/actions/user-preferences' +import { unclaimGateway } from '@console/store/actions/gateways' import { selectUser } from '@console/store/selectors/user' import { selectBookmarksList } from '@console/store/selectors/user-preferences' +import { selectSelectedGatewayClaimable } from '@console/store/selectors/gateways' import style from './gateway-overview-header.styl' @@ -69,6 +71,7 @@ const GatewayOverviewHeader = ({ gateway }) => { selectFetchingEntry(state, DELETE_BOOKMARK_BASE), ) const mayDeleteGtw = useSelector(state => checkFromState(mayDeleteGateway, state)) + const supportsClaiming = useSelector(selectSelectedGatewayClaimable) const isBookmarked = useMemo( () => bookmarks.map(b => b.entity_ids?.gateway_ids?.gateway_id).some(b => b === gateway_id), @@ -126,6 +129,12 @@ const GatewayOverviewHeader = ({ gateway }) => { ) + const handleUnclaimGateway = useCallback(async () => { + if (supportsClaiming) { + await dispatch(attachPromise(unclaimGateway(gateway_id))) + } + }, [dispatch, gateway_id, supportsClaiming]) + return (
@@ -164,6 +173,7 @@ const GatewayOverviewHeader = ({ gateway }) => { entityName={name} setVisible={setDeleteGatewayVisible} visible={deleteGatewayVisible} + additionalAction={handleUnclaimGateway} />
diff --git a/pkg/webui/console/views/gateway-general-settings/basic-settings-form/index.js b/pkg/webui/console/views/gateway-general-settings/basic-settings-form/index.js index 89b40fb727..2fb91faf25 100644 --- a/pkg/webui/console/views/gateway-general-settings/basic-settings-form/index.js +++ b/pkg/webui/console/views/gateway-general-settings/basic-settings-form/index.js @@ -13,7 +13,7 @@ // limitations under the License. import React, { useCallback } from 'react' -import { useSelector } from 'react-redux' +import { useDispatch, useSelector } from 'react-redux' import { GATEWAY } from '@console/constants/entities' @@ -38,10 +38,13 @@ import Require from '@console/lib/components/require' import PropTypes from '@ttn-lw/lib/prop-types' import sharedMessages from '@ttn-lw/lib/shared-messages' import tooltipIds from '@ttn-lw/lib/constants/tooltip-ids' +import attachPromise from '@ttn-lw/lib/store/actions/attach-promise' import { encodeAttributes, decodeAttributes } from '@console/lib/attributes' import { checkFromState, mayDeleteGateway } from '@console/lib/feature-checks' +import { unclaimGateway } from '@console/store/actions/gateways' + import { selectIsConfiguration } from '@console/store/selectors/identity-server' import { selectUserId } from '@console/store/selectors/user' @@ -76,6 +79,8 @@ const BasicSettingsForm = React.memo(props => { const [error, setError] = React.useState(undefined) const [deleteGtwVisible, setDeleteGtwVisible] = React.useState(false) + const dispatch = useDispatch() + const handleOpenDeleteGatewayModal = useCallback(() => { setDeleteGtwVisible(true) }, []) @@ -102,6 +107,12 @@ const BasicSettingsForm = React.memo(props => { [onSubmit], ) + const handleUnclaimGateway = useCallback(async () => { + if (supportsClaiming) { + await dispatch(attachPromise(unclaimGateway(gtwId))) + } + }, [dispatch, gtwId, supportsClaiming]) + return (
{ entityName={gateway.name} setVisible={setDeleteGtwVisible} visible={deleteGtwVisible} + additionalAction={handleUnclaimGateway} />