All files / modules/35-connectors/components/ConnectorReferenceField FormConnectorReferenceField.tsx

92.86% Statements 13/14
30.77% Branches 8/26
50% Functions 1/2
92.86% Lines 13/14

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              25x 25x 25x 25x 25x 25x         25x 51x 51x 51x   51x   51x                                   25x  
/*
 * 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 from 'react'
import { connect, FormikContext } from 'formik'
import { get } from 'lodash-es'
import { errorCheck } from '@common/utils/formikHelpers'
import { Scope } from '@common/interfaces/SecretsInterface'
import { ConnectorReferenceFieldProps, ConnectorReferenceField } from './ConnectorReferenceField'
export interface FormConnectorFieldProps extends Omit<ConnectorReferenceFieldProps, 'onChange' | 'error'> {
  formik?: FormikContext<any>
}
 
const FormConnectorReference = (props: FormConnectorFieldProps): React.ReactElement => {
  const { name, formik, placeholder, disabled, ...restProps } = props
  const hasError = errorCheck(name, formik)
  const error = hasError ? get(formik?.errors, name) : undefined
 
  const selected = get(formik?.values, name, '')
 
  return (
    <ConnectorReferenceField
      {...restProps}
      name={name}
      placeholder={placeholder}
      selected={selected as ConnectorReferenceFieldProps['selected']}
      onChange={(record, scope) => {
        formik?.setFieldValue(
          name,
          scope === Scope.ORG || scope === Scope.ACCOUNT ? `${scope}.${record?.identifier}` : record?.identifier
        )
      }}
      error={error as string}
      disabled={disabled}
    />
  )
}
 
export const FormConnectorReferenceField = connect(FormConnectorReference)