All files / modules/75-cf/components/FlagActivation/hooks useDeleteFlagModal.tsx

93.33% Statements 42/45
78.57% Branches 33/42
100% Functions 5/5
93.33% Lines 42/45

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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124              3x 3x 3x 3x 3x   3x 3x   3x 3x 3x 3x 3x                               3x 53x   53x 53x   53x 53x 53x     53x           53x 5x           1x           53x 5x   5x 4x   4x 1x   3x       5x 5x   5x 1x     5x 5x                   53x                           7x 5x   2x         53x     3x  
/*
 * Copyright 2022 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 React from 'react'
import { useHistory, useParams } from 'react-router-dom'
import { useToaster, Text } from '@wings-software/uicore'
import { Intent } from '@harness/design-system'
import { useModalHook } from '@harness/use-modal'
import type { MutateRequestOptions } from 'restful-react/dist/Mutate'
import { useStrings } from 'framework/strings'
import routes from '@common/RouteDefinitions'
import type { DeleteFeatureFlagQueryParams, Feature, GitSyncErrorResponse } from 'services/cf'
import { useConfirmAction, useQueryParams } from '@common/hooks'
import { GitSyncFormValues, GIT_SYNC_ERROR_CODE, UseGitSync } from '@cf/hooks/useGitSync'
import { AUTO_COMMIT_MESSAGES } from '@cf/constants/GitSyncConstants'
import { getErrorMessage, showToaster } from '@cf/utils/CFUtils'
import SaveFlagToGitModal from '../../SaveFlagToGitModal/SaveFlagToGitModal'
 
interface UseDeleteFlagModalProps {
  featureFlag: Feature
  gitSync: UseGitSync
  deleteFeatureFlag: (
    data: string,
    mutateRequestOptions?: MutateRequestOptions<DeleteFeatureFlagQueryParams, void> | undefined
  ) => void
  queryParams: DeleteFeatureFlagQueryParams
}
 
interface UseDeleteFlagModalReturn {
  confirmDeleteFlag: () => void
}
 
const useDeleteFlagModal = (props: UseDeleteFlagModalProps): UseDeleteFlagModalReturn => {
  const { featureFlag, gitSync, queryParams, deleteFeatureFlag } = props
 
  const urlQuery: Record<string, string> = useQueryParams()
  const { projectIdentifier, orgIdentifier, accountId } = useParams<Record<string, string>>()
 
  const { showError } = useToaster()
  const { getString } = useStrings()
  const history = useHistory()
 
  const featureFlagListURL =
    routes.toCFFeatureFlags({
      projectIdentifier: projectIdentifier,
      orgIdentifier: orgIdentifier,
      accountId
    }) + `${urlQuery?.activeEnvironment ? `?activeEnvironment=${urlQuery.activeEnvironment}` : ''}`
 
  const [showGitModal, hideGitModal] = useModalHook(() => {
    return (
      <SaveFlagToGitModal
        flagName={featureFlag.name}
        flagIdentifier={featureFlag.identifier}
        onSubmit={handleDeleteFlag}
        onClose={() => {
          hideGitModal()
        }}
      />
    )
  }, [featureFlag.name, featureFlag.identifier])
 
  const handleDeleteFlag = async (gitSyncFormValues?: GitSyncFormValues): Promise<void> => {
    let commitMsg = ''
 
    if (gitSync.isGitSyncEnabled) {
      const { gitSyncInitialValues } = gitSync.getGitSyncFormMeta(AUTO_COMMIT_MESSAGES.DELETED_FLAG)
 
      if (gitSync.isAutoCommitEnabled) {
        commitMsg = gitSyncInitialValues.gitDetails.commitMsg
      } else {
        commitMsg = gitSyncFormValues?.gitDetails.commitMsg || ''
      }
    }
 
    try {
      await deleteFeatureFlag(featureFlag.identifier, { queryParams: { ...queryParams, commitMsg } })
 
      if (gitSync.isGitSyncEnabled && gitSyncFormValues?.autoCommit) {
        await gitSync.handleAutoCommit(gitSyncFormValues?.autoCommit)
      }
 
      history.replace(featureFlagListURL)
      showToaster(getString('cf.messages.flagDeleted'))
    } catch (error: any) {
      if (error.status === GIT_SYNC_ERROR_CODE) {
        gitSync.handleError(error.data as GitSyncErrorResponse)
      } else {
        showError(getErrorMessage(error), 0, 'cf.delete.ff.error')
      }
    }
  }
 
  const confirmDeleteFlag = useConfirmAction({
    title: getString('cf.featureFlags.deleteFlag'),
    confirmText: getString('delete'),
    message: (
      <Text>
        <span
          dangerouslySetInnerHTML={{
            __html: getString('cf.featureFlags.deleteFlagMessage', { name: featureFlag.name })
          }}
        />
      </Text>
    ),
    intent: Intent.DANGER,
    action: async () => {
      if (gitSync?.isGitSyncEnabled && !gitSync?.isAutoCommitEnabled) {
        showGitModal()
      } else {
        handleDeleteFlag()
      }
    }
  })
 
  return { confirmDeleteFlag }
}
 
export default useDeleteFlagModal