All files / modules/35-connectors/components/CreateConnector/commonSteps/ConnectivityModeStep ConnectivityModeStep.tsx

79.07% Statements 34/43
22.45% Branches 22/98
50% Functions 3/6
79.07% Lines 34/43

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 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201              219x 219x                     219x 219x 219x               219x 219x         219x 219x 219x 219x                                             219x 9x 9x         9x   9x 9x 9x       9x   9x                             9x               9x 9x   9x 9x                                           3x 2x 2x     1x             1x 1x 1x                     14x                                                                                             219x  
/*
 * 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,
  ButtonVariation,
  Formik,
  FormikForm,
  Layout,
  ModalErrorHandler,
  ModalErrorHandlerBinding,
  StepProps,
  Text
} from '@wings-software/uicore'
import * as Yup from 'yup'
import { FontVariation, Color } from '@harness/design-system'
import { useParams } from 'react-router-dom'
import type {
  ConnectorConfigDTO,
  ConnectorInfoDTO,
  ConnectorRequestBody,
  EntityGitDetails,
  ResponseConnectorResponse
} from 'services/cd-ng'
import { PageSpinner } from '@common/components'
import ConnectivityMode, {
  ConnectivityCardItem,
  ConnectivityModeType
} from '@common/components/ConnectivityMode/ConnectivityMode'
import type { ProjectPathProps } from '@common/interfaces/RouteInterfaces'
import { useStrings } from 'framework/strings'
import { useAppStore } from 'framework/AppStore/AppStoreContext'
import useCreateEditConnector, { BuildPayloadProps } from '@connectors/hooks/useCreateEditConnector'
import css from './ConnectivityModeStep.module.scss'
 
interface ConnectivityModeStepData extends BuildPayloadProps {
  connectivityMode: ConnectivityModeType | undefined
}
 
interface ConnectivityModeStepProps {
  type: ConnectorConfigDTO['type']
  isEditMode: boolean
  setIsEditMode?: (val: boolean) => void
  connectorInfo: ConnectorInfoDTO | void
  buildPayload: (data: BuildPayloadProps) => ConnectorRequestBody
  gitDetails?: EntityGitDetails
  disableGitSync?: boolean
  submitOnNextStep?: boolean
  connectivityMode?: ConnectivityModeType
  setConnectivityMode?: (val: ConnectivityModeType) => void
  onConnectorCreated?: (data?: ConnectorRequestBody) => void | Promise<void>
  hideModal?: () => void
  customHandleCreate?: (payload: ConnectorConfigDTO) => Promise<ConnectorInfoDTO | undefined>
  customHandleUpdate?: (payload: ConnectorConfigDTO) => Promise<ConnectorInfoDTO | undefined>
}
 
const ConnectivityModeStep: React.FC<StepProps<ConnectorConfigDTO> & ConnectivityModeStepProps> = props => {
  const { prevStepData, nextStep, connectorInfo, buildPayload, customHandleUpdate, customHandleCreate } = props
  const { getString } = useStrings()
  const {
    accountId,
    projectIdentifier: projectIdentifierFromUrl,
    orgIdentifier: orgIdentifierFromUrl
  } = useParams<ProjectPathProps>()
 
  const projectIdentifier = connectorInfo ? connectorInfo.projectIdentifier : projectIdentifierFromUrl
  const orgIdentifier = connectorInfo ? connectorInfo.orgIdentifier : orgIdentifierFromUrl
  const isGitSyncEnabled = (useAppStore().isGitSyncEnabled &&
    !props.disableGitSync &&
    orgIdentifier &&
    projectIdentifier) as boolean
  const [modalErrorHandler, setModalErrorHandler] = useState<ModalErrorHandlerBinding | undefined>()
 
  const afterSuccessHandler = (response: ResponseConnectorResponse): void => {
    props.onConnectorCreated?.(response?.data)
    if (prevStepData?.branch) {
      // updating connector branch to handle if new branch was created while commit
      prevStepData.branch = response?.data?.gitDetails?.branch
    }
 
    if (stepDataRef?.skipDefaultValidation) {
      props.hideModal?.()
    } else {
      nextStep?.({ ...prevStepData, ...stepDataRef } as ConnectorConfigDTO)
      props.setIsEditMode?.(true)
    }
  }
 
  const { onInitiate, loading } = useCreateEditConnector<ConnectivityModeStepData>({
    accountId,
    isEditMode: props.isEditMode,
    isGitSyncEnabled,
    afterSuccessHandler,
    gitDetails: props.gitDetails
  })
 
  let stepDataRef: ConnectorConfigDTO | null = null
  const defaultInitialValues = { connectivityMode: undefined }
 
  const connectorName = (prevStepData as ConnectorConfigDTO)?.name || (connectorInfo as ConnectorInfoDTO)?.name
  return (
    <>
      {!isGitSyncEnabled && loading ? (
        <PageSpinner
          message={
            props.isEditMode
              ? getString('connectors.updating', { name: connectorName })
              : getString('connectors.creating', { name: connectorName })
          }
        />
      ) : null}
      <Layout.Vertical>
        <ModalErrorHandler bind={setModalErrorHandler} />
        <Formik
          initialValues={{
            ...defaultInitialValues,
            ...prevStepData
          }}
          validationSchema={Yup.object().shape({
            connectivityMode: Yup.string().required(getString('connectors.connectivityMode.validation'))
          })}
          onSubmit={stepData => {
            if (props.submitOnNextStep || stepData.connectivityMode === ConnectivityModeType.Delegate) {
              nextStep?.({ ...prevStepData, ...stepData, projectIdentifier, orgIdentifier })
              return
            }
 
            const connectorData = {
              ...prevStepData,
              ...stepData,
              projectIdentifier: projectIdentifier,
              orgIdentifier: orgIdentifier
            }
 
            stepDataRef = stepData
            modalErrorHandler?.hide()
            onInitiate({
              connectorFormData: connectorData,
              buildPayload,
              customHandleCreate,
              customHandleUpdate
            })
          }}
          formName={`connectivityModeForm${props.type}`}
          enableReinitialize
        >
          {formik => {
            return (
              <FormikForm>
                <Layout.Vertical className={css.formCss} spacing={'medium'}>
                  <Text
                    font={{ variation: FontVariation.H3 }}
                    color={Color.BLACK}
                    tooltipProps={{ dataTooltipId: 'ConnectivityModeTitle' }}
                  >
                    {getString('connectors.connectivityMode.connectToProvider')}
                  </Text>
                  <Text color={Color.BLACK}>{getString('connectors.connectivityMode.selectText')}</Text>
                  <ConnectivityMode
                    formik={formik}
                    className={css.cardClass}
                    onChange={(val: ConnectivityCardItem) => {
                      props.setConnectivityMode?.(val.type)
                    }}
                  />
                </Layout.Vertical>
                <Layout.Horizontal padding={{ top: 'medium' }} margin={{ top: 'xxxlarge' }} spacing="medium">
                  <Button
                    text={getString('back')}
                    icon="chevron-left"
                    onClick={() => props?.previousStep?.(props?.prevStepData)}
                    data-name="awsBackButton"
                    variation={ButtonVariation.SECONDARY}
                  />
                  <Button
                    type="submit"
                    intent={'primary'}
                    text={getString(
                      formik.values.connectivityMode === ConnectivityModeType.Delegate ? 'continue' : 'saveAndContinue'
                    )}
                    disabled={loading}
                    rightIcon="chevron-right"
                    data-testid="connectivitySaveAndContinue"
                  />
                </Layout.Horizontal>
              </FormikForm>
            )
          }}
        </Formik>
      </Layout.Vertical>
    </>
  )
}
 
export default ConnectivityModeStep