All files / modules/45-projects-orgs/pages/projects DeleteProject.tsx

100% Statements 24/24
71.43% Branches 5/7
100% Functions 2/2
100% Lines 24/24

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65              66x 66x 66x 66x 66x   66x 66x           66x 296x 296x 296x 296x           296x 296x 296x               2x 2x 2x     2x 2x     2x 2x               296x         66x  
/*
 * Copyright 2021 Harness Inc. All rights reserved.
 * Use of this source code is governed by the PolyForm Shield 1.0.0 license
 * that can be found in the licenses directory at the root of this repository, also available at
 * https://polyformproject.org/wp-content/uploads/2020/06/PolyForm-Shield-1.0.0.txt.
 */
 
import { Intent } from '@blueprintjs/core'
import { useParams } from 'react-router-dom'
import { useToaster, useConfirmationDialog } from '@wings-software/uicore'
import { useStrings } from 'framework/strings'
import { Project, useDeleteProject } from 'services/cd-ng'
import type { AccountPathProps } from '@common/interfaces/RouteInterfaces'
import { useAppStore } from 'framework/AppStore/AppStoreContext'
import useRBACError from '@rbac/utils/useRBACError/useRBACError'
 
interface UseDeleteProjectDialogReturn {
  openDialog: () => void
}
 
const useDeleteProjectDialog = (data: Project, onSuccess: () => void): UseDeleteProjectDialogReturn => {
  const { accountId } = useParams<AccountPathProps>()
  const { updateAppStore } = useAppStore()
  const { getRBACErrorMessage } = useRBACError()
  const { mutate: deleteProject } = useDeleteProject({
    queryParams: {
      accountIdentifier: accountId,
      orgIdentifier: data.orgIdentifier || /* istanbul ignore next */ ''
    }
  })
  const { getString } = useStrings()
  const { showSuccess, showError } = useToaster()
  const { openDialog } = useConfirmationDialog({
    contentText: getString('projectCard.confirmDelete', { name: data.name }),
    titleText: getString('projectCard.confirmDeleteTitle'),
    confirmButtonText: getString('delete'),
    cancelButtonText: getString('cancel'),
    intent: Intent.DANGER,
    buttonIntent: Intent.DANGER,
    onCloseDialog: async (isConfirmed: boolean) => {
      Eif (isConfirmed) {
        try {
          const deleted = await deleteProject(data.identifier || /* istanbul ignore next */ '', {
            headers: { 'content-type': 'application/json' }
          })
          Eif (deleted)
            showSuccess(
              getString('projectCard.successMessage', { projectName: data.name || /* istanbul ignore next */ '' })
            )
          updateAppStore({ selectedProject: undefined, selectedOrg: undefined })
          onSuccess()
        } catch (err) {
          /* istanbul ignore next */
          showError(getRBACErrorMessage(err))
        }
      }
    }
  })
  return {
    openDialog
  }
}
 
export default useDeleteProjectDialog