All files / modules/85-cv/pages/ChangeSource/ChangeSourceDrawer/components/KubernetesChangeSource KubernetesChangeSource.tsx

82.76% Statements 24/29
52.5% Branches 42/80
100% Functions 5/5
82.76% Lines 24/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              18x 18x 18x 18x 18x 18x     18x 18x 18x     18x 10x 10x 10x 10x   10x 9x 3x                       10x 6x 6x                     10x 6x       10x                                                 2x                            
/*
 * 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, { useEffect, useMemo } from 'react'
import { useParams } from 'react-router-dom'
import { Text, Container, Utils } from '@wings-software/uicore'
import { Color } from '@harness/design-system'
import { useValidateK8sConnectivity } from 'services/cv'
import { FormConnectorReferenceField } from '@connectors/components/ConnectorReferenceField/FormConnectorReferenceField'
import type { ConnectorReferenceFieldProps } from '@connectors/components/ConnectorReferenceField/ConnectorReferenceField'
import type { ProjectPathProps } from '@common/interfaces/RouteInterfaces'
import ValidationStatus from '@cv/pages/components/ValidationStatus/ValidationStatus'
import { useStrings } from 'framework/strings'
import { determineValidationStatus } from './kubernetesChangeSource.utils'
import type { ChangeSourceProps } from '../../ChangeSourceDrawer.types'
 
export default function KubernetesChangeSource(props: ChangeSourceProps): JSX.Element {
  const { formik, isEdit } = props
  const { getString } = useStrings()
  const { orgIdentifier, projectIdentifier, accountId } = useParams<ProjectPathProps & { identifier: string }>()
  const { loading, error, refetch, data } = useValidateK8sConnectivity({ lazy: true })
 
  useEffect(() => {
    if (!isEdit && formik.values?.spec?.connectorRef && !loading) {
      refetch({
        queryParams: {
          accountId,
          projectIdentifier,
          orgIdentifier,
          tracingId: Utils.randomId(),
          connectorIdentifier: formik.values.spec.connectorRef
        }
      })
    }
  }, [formik.values?.spec?.connectorRef])
 
  useEffect(() => {
    Eif (isEdit || !formik.values?.spec?.connectorRef) {
      return
    }
 
    if (error || data?.data === false || loading) {
      formik.setFieldValue('spec', { ...formik.values?.spec, isConnectorInvalid: true })
    } else if (data?.data) {
      delete formik.values?.spec?.isConnectorInvalid
      formik.setFieldValue('spec', { ...formik.values?.spec })
    }
  }, [data, error, loading])
 
  const validationResult = useMemo(
    () => determineValidationStatus({ isEdit, loading, result: data?.data, error, getString }),
    [isEdit, loading, error]
  )
 
  return (
    <Container data-name="kubechangesource">
      <FormConnectorReferenceField
        width={400}
        formik={formik}
        type={formik.values?.type as ConnectorReferenceFieldProps['type']}
        name={'spec.connectorRef'}
        disabled={isEdit}
        accountIdentifier={accountId}
        projectIdentifier={projectIdentifier}
        orgIdentifier={orgIdentifier}
        placeholder={getString('cv.healthSource.connectors.selectConnector', {
          sourceType: formik?.values?.type
        })}
        label={
          <Text color={Color.BLACK} font={'small'} margin={{ bottom: 'small' }}>
            {getString('connectors.selectConnector')}
          </Text>
        }
        tooltipProps={{ dataTooltipId: 'selectKubeConnector' }}
      />
      <ValidationStatus
        validationStatus={validationResult.status}
        textToDisplay={validationResult.text}
        onRetry={() =>
          refetch({
            queryParams: {
              accountId,
              projectIdentifier,
              orgIdentifier,
              tracingId: Utils.randomId(),
              connectorIdentifier: formik.values.spec.connectorRef
            }
          })
        }
      />
    </Container>
  )
}