All files / modules/70-pipeline/pages/utils DeleteConfirmDialog.tsx

93.1% Statements 27/29
73.53% Branches 25/34
85.71% Functions 6/7
93.1% Lines 27/29

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              5x 5x 5x   5x       5x 135x   135x   44x   38x   38x   15x                           5x               10x 10x                                               5x           125x 125x 125x   125x 46x     125x                                   5x 5x         125x 5x 5x   125x    
/*
 * 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 React, { useState } from 'react'
import { Intent, TextArea } from '@blueprintjs/core'
import { Text, useConfirmationDialog } from '@wings-software/uicore'
import type { EntityGitDetails } from 'services/pipeline-ng'
import { useStrings } from 'framework/strings'
import type { PipelineDTO } from '../pipelines/views/PipelineListView'
import type { InputSetLocal } from '../inputSet-list/InputSetListView'
 
const useGetEntityText = (entityType: string): string => {
  const { getString } = useStrings()
 
  switch (entityType) {
    case 'pipeline':
      return getString('common.pipeline')
    case 'inputSet':
      return getString('inputSets.inputSetLabel')
    case 'overlayInputSet':
      return getString('inputSets.overlayInputSet')
    case 'template':
      return getString('common.template.label')
    default:
      return ''
  }
}
interface DeleteConfirmDialogContentProps {
  gitDetails?: EntityGitDetails
  entityName: string
  commitMsg: string
  onCommitMsgChange: (commitMsg: string) => void
  entityType: string
  forceComments?: boolean
}
 
export function DeleteConfirmDialogContent({
  gitDetails = {},
  entityName = '',
  commitMsg,
  onCommitMsgChange,
  entityType = '',
  forceComments = false
}: DeleteConfirmDialogContentProps): React.ReactElement {
  const { getString } = useStrings()
  return (
    <div className={'pipelineDeleteDialog'}>
      <Text margin={{ bottom: 'medium' }} title={entityName}>{`${getString(
        'common.git.confirmDelete'
      )} ${useGetEntityText(entityType)} ${entityName}?`}</Text>
      {(gitDetails?.objectId || forceComments) && (
        <>
          <Text>{forceComments ? getString('common.comments') : getString('common.git.commitMessage')}</Text>
          <TextArea
            value={commitMsg}
            onInput={(event: React.ChangeEvent<HTMLTextAreaElement>) => {
              onCommitMsgChange(event.target.value)
            }}
          />
        </>
      )}
    </div>
  )
}
 
interface DeleteMetaData {
  versions?: string[]
}
 
export default function useDeleteConfirmationDialog(
  entityData: PipelineDTO | InputSetLocal,
  entityType: string,
  onDelete?: (commitMsg: string, versions?: string[]) => Promise<void>,
  forceComments = false
): { confirmDelete: (deleteMetaData?: DeleteMetaData) => void } {
  const { getString } = useStrings()
  const [deleteCallMetaData, setDeleteCallMetaData] = useState<DeleteMetaData>()
  const [commitMsg, setCommitMsg] = React.useState<string>(`${getString('delete')} ${entityData.name}`)
 
  React.useEffect(() => {
    setCommitMsg(`${getString('delete')} ${entityData.name}`)
  }, [entityData])
 
  const { openDialog: openConfirmDeleteDialog } = useConfirmationDialog({
    contentText: (
      <DeleteConfirmDialogContent
        entityName={entityData?.name || ''}
        entityType={entityType}
        gitDetails={entityData.gitDetails}
        commitMsg={commitMsg}
        onCommitMsgChange={setCommitMsg}
        forceComments={forceComments}
      />
    ),
    titleText: `${getString('delete')} ${useGetEntityText(entityType)}`,
    confirmButtonText: getString('delete'),
    cancelButtonText: getString('cancel'),
    intent: Intent.DANGER,
    buttonIntent: Intent.DANGER,
    onCloseDialog: async (isConfirmed: boolean) => {
      /* istanbul ignore else */
      if (isConfirmed) {
        await onDelete?.(commitMsg, deleteCallMetaData?.versions)
      }
    }
  })
 
  const confirmDelete = (deleteMetaData?: DeleteMetaData): void => {
    setDeleteCallMetaData(deleteMetaData)
    openConfirmDeleteDialog()
  }
  return { confirmDelete }
}