All files / modules/30-secrets/components/MutiTypeSecretInput MultiTypeSecretInput.tsx

87.5% Statements 28/32
75% Branches 9/12
50% Functions 3/6
87.5% Lines 28/32

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              224x 224x 224x 224x                 224x 224x 224x 224x     224x 224x 224x 224x 224x           224x     20x 20x 20x                                                               224x                       22x   22x                       22x       1x                 22x 22x               22x   22x       22x                                                                     224x  
/*
 * 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 cx from 'classnames'
import {
  Button,
  FixedTypeComponentProps,
  ExpressionAndRuntimeType,
  ButtonProps,
  ExpressionAndRuntimeTypeProps,
  MultiTypeInputType,
  Text
} from '@wings-software/uicore'
import { get, pick } from 'lodash-es'
import { FormGroup, IFormGroupProps, Intent } from '@blueprintjs/core'
import useCreateSSHCredModal from '@secrets/modals/CreateSSHCredModal/useCreateSSHCredModal'
import useCreateOrSelectSecretModal from '@secrets/modals/CreateOrSelectSecretModal/useCreateOrSelectSecretModal'
import type { SecretReference } from '@secrets/components/CreateOrSelectSecret/CreateOrSelectSecret'
import type { SecretResponseWrapper, ResponsePageSecretResponseWrapper } from 'services/cd-ng'
import { useStrings } from 'framework/strings'
import { errorCheck } from '@common/utils/formikHelpers'
import { getScopeFromDTO } from '@common/components/EntityReference/EntityReference'
import { getReference } from '@common/utils/utils'
import css from './MultiTypeSecretInput.module.scss'
 
export interface MultiTypeSecretInputFixedTypeComponentProps
  extends FixedTypeComponentProps,
    Omit<ButtonProps, 'onChange'> {}
 
export function MultiTypeSecretInputFixedTypeComponent(
  props: MultiTypeSecretInputFixedTypeComponentProps
): React.ReactElement {
  const { value, onChange, disabled, ...rest } = props
  const { getString } = useStrings()
  return (
    <Button
      {...rest}
      withoutBoxShadow
      className={css.value}
      icon="key-main"
      iconProps={{ size: 24, height: 12 }}
      data-testid={'create-or-select-secret'}
      disabled={disabled}
    >
      <Text lineClamp={1}>{value || getString('createOrSelectSecret')}</Text>
    </Button>
  )
}
 
export interface MultiTypeSecretInputProps extends IFormGroupProps {
  name: string
  label?: string
  expressions?: string[]
  allowableTypes?: MultiTypeInputType[]
  type?: SecretResponseWrapper['secret']['type']
  onSuccess?: (secret: SecretReference) => void
  secretsListMockData?: ResponsePageSecretResponseWrapper
  isMultiType?: boolean
  small?: boolean
}
 
export interface ConnectedMultiTypeSecretInputProps extends MultiTypeSecretInputProps {
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  formik: FormikContext<any>
}
 
export function MultiTypeSecretInput(props: ConnectedMultiTypeSecretInputProps): React.ReactElement {
  const {
    formik,
    label,
    name,
    allowableTypes,
    expressions = [],
    onSuccess,
    type,
    secretsListMockData,
    isMultiType = true,
    ...restProps
  } = props
 
  const { openCreateSSHCredModal } = useCreateSSHCredModal({
    onSuccess: data => {
      const secret = {
        ...pick(data, ['name', 'identifier', 'orgIdentifier', 'projectIdentifier']),
        referenceString: getReference(getScopeFromDTO(data), data.identifier) as string
      }
      formik.setFieldValue(name, secret.referenceString)
      /* istanbul ignore next */
      onSuccess?.(secret)
    }
  })
 
  const { openCreateOrSelectSecretModal } = useCreateOrSelectSecretModal(
    {
      type,
      onSuccess: secret => {
        formik.setFieldValue(name, secret.referenceString)
        /* istanbul ignore next */
        onSuccess?.(secret)
      },
      secretsListMockData,
      handleInlineSSHSecretCreation: () => openCreateSSHCredModal()
    },
    [name, onSuccess]
  )
  const value = get(formik.values, name)
  const hasError = errorCheck(name, formik)
 
  const {
    intent = hasError ? Intent.DANGER : Intent.NONE,
    helperText = hasError ? get(formik.errors, name) : null,
    disabled,
    small,
    ...rest
  } = restProps
 
  const handleChange: ExpressionAndRuntimeTypeProps['onChange'] = val => {
    formik.setFieldValue(name, val)
  }
 
  return (
    <FormGroup
      {...rest}
      className={cx({ [css.smallForm]: small })}
      labelFor={name}
      label={label}
      intent={intent}
      helperText={helperText}
    >
      {isMultiType ? (
        <ExpressionAndRuntimeType
          name={name}
          value={value}
          disabled={disabled}
          onChange={handleChange}
          expressions={expressions}
          allowableTypes={allowableTypes}
          style={{ flexGrow: 1 }}
          fixedTypeComponentProps={{ onClick: openCreateOrSelectSecretModal }}
          fixedTypeComponent={MultiTypeSecretInputFixedTypeComponent}
          defaultValueToReset=""
        />
      ) : (
        <MultiTypeSecretInputFixedTypeComponent
          value={value}
          onChange={handleChange}
          onClick={openCreateOrSelectSecretModal}
          disabled={disabled}
          data-testid={name}
        />
      )}
    </FormGroup>
  )
}
 
export default connect<MultiTypeSecretInputProps>(MultiTypeSecretInput)