All files / modules/35-connectors/modals/ConnectorModal useCreateConnectorModal.tsx

83.33% Statements 30/36
52.5% Branches 21/40
42.86% Functions 3/7
83.33% Lines 30/36

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 125 126 127 128              217x 217x 217x 217x 217x 217x 217x       217x 217x 217x                                           217x 1667x 1667x 1667x 1667x 1667x     1667x                         1667x   1667x       1667x   3x                                                                     1667x             3x 3x 3x 3x 3x     3x 3x           217x  
/*
 * 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 { Button } from '@wings-software/uicore'
import { useModalHook } from '@harness/use-modal'
import { Dialog, IDialogProps } from '@blueprintjs/core'
import { useParams } from 'react-router-dom'
import { CreateConnectorWizard } from '@connectors/components/CreateConnectorWizard/CreateConnectorWizard'
import { Connectors } from '@connectors/constants'
import type { ConnectorInfoDTO, ConnectorRequestBody } from 'services/cd-ng'
import type { ProjectPathProps } from '@common/interfaces/RouteInterfaces'
import type { IGitContextFormProps } from '@common/components/GitContextForm/GitContextForm'
import { ConnectivityModeType } from '@common/components/ConnectivityMode/ConnectivityMode'
import { getConnectivityMode } from '@connectors/pages/connectors/utils/ConnectorUtils'
import css from '../../components/CreateConnectorWizard/CreateConnectorWizard.module.scss'
 
export interface UseCreateConnectorModalProps {
  onSuccess?: (data?: ConnectorRequestBody) => void
  onClose?: () => void
}
 
export interface ConnectorModaldata {
  connectorInfo?: ConnectorInfoDTO
  gitDetails?: IGitContextFormProps
}
 
export interface UseCreateConnectorModalReturn {
  openConnectorModal: (
    isEditMode: boolean,
    type: ConnectorInfoDTO['type'],
    connector?: ConnectorModaldata,
    modalProps?: IDialogProps
  ) => void
  hideConnectorModal: () => void
}
 
const useCreateConnectorModal = (props: UseCreateConnectorModalProps): UseCreateConnectorModalReturn => {
  const [isEditMode, setIsEditMode] = useState(false)
  const [type, setType] = useState(Connectors.KUBERNETES_CLUSTER)
  const [connectorInfo, setConnectorInfo] = useState<ConnectorInfoDTO | undefined>()
  const [gitDetails, setGitDetails] = useState<IGitContextFormProps | undefined>()
  const [connectivityMode, setConnectivityMode] = useState<ConnectivityModeType | undefined>(
    ConnectivityModeType.Manager
  )
  const [modalProps, setModalProps] = useState<IDialogProps>({
    isOpen: true,
    enforceFocus: false,
    style: {
      width: 'auto',
      minWidth: 1175,
      minHeight: 640,
      borderLeft: 0,
      paddingBottom: 0,
      position: 'relative',
      overflow: 'auto'
    }
  })
  const { accountId, projectIdentifier, orgIdentifier } = useParams<ProjectPathProps>()
 
  const handleSuccess = (data?: ConnectorRequestBody): void => {
    props.onSuccess?.(data)
  }
 
  const [showModal, hideModal] = useModalHook(
    () => (
      <Dialog {...modalProps}>
        <CreateConnectorWizard
          accountId={accountId}
          orgIdentifier={(connectorInfo ? connectorInfo.orgIdentifier : orgIdentifier) as string} //For create take scope from url else from entities
          projectIdentifier={(connectorInfo ? connectorInfo.projectIdentifier : projectIdentifier) as string}
          type={type}
          isEditMode={isEditMode}
          setIsEditMode={setIsEditMode}
          connectivityMode={connectivityMode}
          setConnectivityMode={setConnectivityMode}
          connectorInfo={connectorInfo}
          gitDetails={gitDetails}
          onSuccess={data => {
            handleSuccess(data)
          }}
          onClose={() => {
            props.onClose?.()
            hideModal()
          }}
        />
        <Button
          minimal
          icon="cross"
          iconProps={{ size: 18 }}
          onClick={() => {
            props.onClose?.()
            hideModal()
          }}
          className={css.crossIcon}
        />
      </Dialog>
    ),
    [type, isEditMode, connectorInfo, gitDetails, connectivityMode]
  )
 
  return {
    openConnectorModal: (
      isEditing: boolean,
      connectorType: ConnectorInfoDTO['type'],
      connector?: ConnectorModaldata,
      _modalProps?: IDialogProps
    ) => {
      setConnectorInfo(connector?.connectorInfo)
      setGitDetails(connector?.gitDetails)
      setIsEditMode(isEditing)
      setType(connectorType)
      setConnectivityMode(
        isEditing ? getConnectivityMode(connector?.connectorInfo?.spec?.executeOnDelegate) : undefined
      )
      setModalProps(_modalProps || modalProps)
      showModal()
    },
    hideConnectorModal: hideModal
  }
}
 
export default useCreateConnectorModal