All files / modules/35-connectors/components/CreateConnector/CreateAzureKeyConnector/views AzureKeyVaultForm.tsx

84% Statements 21/25
54.55% Branches 12/22
66.67% Functions 4/6
84% Lines 21/25

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              219x 219x 219x 219x 219x   219x 219x     219x 219x                   219x 2x 1x   1x               1x 1x   1x 1x               1x                           10x         1x                                                         219x  
/*
 * 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, { useState } from 'react'
import * as Yup from 'yup'
import { StepProps, Container, Text, Formik, FormikForm, Layout, Button, ButtonVariation } from '@wings-software/uicore'
import { FontVariation } from '@harness/design-system'
import { useStrings } from 'framework/strings'
import type { AzureKeyVaultConnectorDTO } from 'services/cd-ng'
import { PageSpinner } from '@common/components'
import { setupAzureKeyVaultFormData } from '@connectors/pages/connectors/utils/ConnectorUtils'
import type { SecretReference } from '@secrets/components/CreateOrSelectSecret/CreateOrSelectSecret'
import type { StepDetailsProps, ConnectorDetailsProps } from '@connectors/interfaces/ConnectorInterface'
import AzureKeyVaultFormFields from './AzureKeyVaultFormFields'
import css from '../CreateAzureKeyVaultConnector.module.scss'
 
export interface AzureKeyVaultFormData {
  clientId?: string
  secretKey?: SecretReference
  tenantId?: string
  subscription?: string
  default?: boolean
}
 
const AzureKeyVaultForm: React.FC<StepProps<StepDetailsProps> & ConnectorDetailsProps> = props => {
  const { prevStepData, previousStep, isEditMode, nextStep, connectorInfo, accountId } = props
  const { getString } = useStrings()
 
  const defaultInitialFormData: AzureKeyVaultFormData = {
    clientId: undefined,
    tenantId: undefined,
    subscription: undefined,
    secretKey: undefined,
    default: false
  }
 
  const [initialValues, setInitialValues] = useState(defaultInitialFormData)
  const [loadingFormData, setLoadingFormData] = useState(isEditMode)
 
  React.useEffect(() => {
    Iif (isEditMode && connectorInfo) {
      setupAzureKeyVaultFormData(connectorInfo, accountId).then(data => {
        setInitialValues(data as AzureKeyVaultFormData)
        setLoadingFormData(false)
      })
    }
  }, [isEditMode, connectorInfo])
 
  return (
    <Container padding={{ top: 'medium' }} width="64%">
      <Text font={{ variation: FontVariation.H3 }} padding={{ bottom: 'xlarge' }}>
        {getString('details')}
      </Text>
      <Formik<AzureKeyVaultFormData>
        formName="azureKeyVaultForm"
        enableReinitialize
        initialValues={{ ...initialValues, ...prevStepData }}
        validationSchema={Yup.object().shape({
          clientId: Yup.string().required(getString('common.validation.clientIdIsRequired')),
          tenantId: Yup.string().required(getString('connectors.azureKeyVault.validation.tenantId')),
          subscription: Yup.string().required(getString('connectors.azureKeyVault.validation.subscription')),
          secretKey: Yup.string().when('vaultName', {
            is: () => !(prevStepData?.spec as AzureKeyVaultConnectorDTO)?.vaultName,
            then: Yup.string().trim().required(getString('common.validation.keyIsRequired'))
          })
        })}
        onSubmit={formData => {
          nextStep?.({ ...connectorInfo, ...prevStepData, ...formData } as StepDetailsProps)
        }}
      >
        <FormikForm>
          <Container className={css.formHeight} margin={{ top: 'medium', bottom: 'xxlarge' }}>
            <AzureKeyVaultFormFields />
          </Container>
          <Layout.Horizontal spacing="medium">
            <Button
              variation={ButtonVariation.SECONDARY}
              icon="chevron-left"
              text={getString('back')}
              onClick={() => previousStep?.(prevStepData)}
            />
            <Button
              type="submit"
              intent="primary"
              rightIcon="chevron-right"
              text={getString('continue')}
              disabled={loadingFormData}
            />
          </Layout.Horizontal>
        </FormikForm>
      </Formik>
      {loadingFormData ? <PageSpinner /> : null}
    </Container>
  )
}
 
export default AzureKeyVaultForm