Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unclaim gateway on delete #7356

Merged
merged 4 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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,
PavelJankoski marked this conversation as resolved.
Show resolved Hide resolved
} = props

const lowerCaseEntity = entity.toLowerCase()
const [confirmId, setConfirmId] = React.useState('')
Expand Down Expand Up @@ -138,6 +146,9 @@ const DeleteEntityHeaderModal = props => {
if (setError) {
setError(undefined)
}
if (additionalAction) {
PavelJankoski marked this conversation as resolved.
Show resolved Hide resolved
await additionalAction()
}
await dispatch(
attachPromise(
deleteEntityActionMap[entity](entityId, { purge: purgeEntity || false }),
Expand All @@ -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(
Expand Down Expand Up @@ -266,6 +277,7 @@ const DeleteEntityHeaderModal = props => {
}

DeleteEntityHeaderModal.propTypes = {
additionalAction: PropTypes.func,
additionalConditions: PropTypes.bool,
entity: PropTypes.string.isRequired,
entityId: PropTypes.string.isRequired,
Expand All @@ -279,6 +291,7 @@ DeleteEntityHeaderModal.defaultProps = {
additionalConditions: false,
entityName: undefined,
setError: undefined,
additionalAction: undefined,
}

export default DeleteEntityHeaderModal
10 changes: 10 additions & 0 deletions pkg/webui/console/containers/gateway-overview-header/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand All @@ -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),
Expand Down Expand Up @@ -126,6 +129,12 @@ const GatewayOverviewHeader = ({ gateway }) => {
</>
)

const handleUnclaimGateway = useCallback(async () => {
if (supportsClaiming) {
await dispatch(attachPromise(unclaimGateway(gateway_id)))
}
}, [dispatch, gateway_id, supportsClaiming])

return (
<div className={style.root}>
<div className="overflow-hidden d-flex flex-column gap-cs-xs">
Expand Down Expand Up @@ -164,6 +173,7 @@ const GatewayOverviewHeader = ({ gateway }) => {
entityName={name}
setVisible={setDeleteGatewayVisible}
visible={deleteGatewayVisible}
additionalAction={handleUnclaimGateway}
/>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand All @@ -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'

Expand Down Expand Up @@ -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)
}, [])
Expand All @@ -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 (
<Form
error={error}
Expand Down Expand Up @@ -272,6 +283,7 @@ const BasicSettingsForm = React.memo(props => {
entityName={gateway.name}
setVisible={setDeleteGtwVisible}
visible={deleteGtwVisible}
additionalAction={handleUnclaimGateway}
/>
</Require>
</SubmitBar>
Expand Down
Loading