All files / modules/75-ce/components/Connectors createConnectorModal.tsx

0% Statements 0/31
0% Branches 0/14
0% Functions 0/6
0% Lines 0/31

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                                                                                                                                                                                                                                           
/*
 * 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 type { IDialogProps } from '@blueprintjs/core'
import { useParams } from 'react-router-dom'
import { Connectors } from '@connectors/constants'
import type { ConnectorInfoDTO } from 'services/cd-ng'
import type { ProjectPathProps } from '@common/interfaces/RouteInterfaces'
import DialogWithExtension from '@ce/common/DialogWithExtension/DialogWithExtension'
import { CreateConnectorWizard } from './CreateConnectorWizard'
import AWSConnectorExtension from './AWSCOConnector/AWSConnectorExtension'
import css from './CreateConnectorWizard.module.scss'
 
export interface UseCreateConnectorModalProps {
  onSuccess?: (data?: ConnectorInfoDTO) => void
  onClose?: () => void
  permission?: 'CE' | 'CO'
}
 
export interface UseCreateConnectorModalReturn {
  openConnectorModal: (
    isEditMode: boolean,
    type: ConnectorInfoDTO['type'],
    connectorInfo: ConnectorInfoDTO | void,
    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 | void>()
  const [modalProps, setModalProps] = useState<IDialogProps>({
    isOpen: true,
    enforceFocus: false,
    style: {
      width: 'auto',
      // minHeight: 640,
      borderLeft: 0,
      paddingBottom: 0,
      // position: 'relative',
      overflow: 'hidden'
    }
  })
  const { accountId, projectIdentifier, orgIdentifier } = useParams<ProjectPathProps>()
 
  const handleSuccess = (data?: ConnectorInfoDTO): void => {
    props.onSuccess?.(data)
  }
 
  const [showModal, hideModal] = useModalHook(
    () => (
      <DialogWithExtension
        modalProps={modalProps}
        renderExtension={<AWSConnectorExtension />}
        dialogStyles={{ height: 740 }}
      >
        <CreateConnectorWizard
          accountId={accountId}
          orgIdentifier={orgIdentifier}
          projectIdentifier={projectIdentifier}
          type={type}
          isEditMode={isEditMode}
          connectorInfo={connectorInfo}
          onSuccess={data => {
            props.onClose?.()
            hideModal()
            handleSuccess(data)
          }}
          hideLightModal={hideModal}
          permission={props.permission}
        />
        <Button
          minimal
          icon="cross"
          iconProps={{ size: 18 }}
          onClick={() => {
            props.onClose?.()
            hideModal()
          }}
          className={css.crossIcon}
        />
        {/* {({ triggerExtension }) => (
          <>
          </>
        )} */}
      </DialogWithExtension>
    ),
    [type, isEditMode, connectorInfo]
  )
 
  return {
    openConnectorModal: (
      isEditing: boolean,
      connectorType: ConnectorInfoDTO['type'],
      connectorDetails: ConnectorInfoDTO | void,
      _modalProps?: IDialogProps | undefined
    ) => {
      setConnectorInfo(connectorDetails)
      setIsEditMode(isEditing)
      setType(connectorType)
      setModalProps(_modalProps || modalProps)
      showModal()
    },
    hideConnectorModal: hideModal
  }
}
 
export default useCreateConnectorModal