All files / modules/75-cd/components/PipelineSteps/PolicyStep/PolicySets/MultiTypePolicySetSelector MultiTypePolicySetSelector.tsx

96.67% Statements 29/30
80% Branches 16/20
88.89% Functions 8/9
96.55% Lines 28/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 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117              98x 98x 98x   98x 98x   98x   98x 98x     98x 98x   98x                   98x               14x   14x   14x 1x     14x                                   98x                 14x   14x   1x           1x             14x 3x 1x 1x     14x     27x                            
/*
 * 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 from 'react'
import { connect, FormikContext } from 'formik'
import { get } from 'lodash-es'
 
import { Button, MultiTypeInputType } from '@harness/uicore'
import { useModalHook } from '@harness/use-modal'
import type { IFormGroupProps } from '@blueprintjs/core'
import { useStrings } from 'framework/strings'
 
import MultiTypeFieldSelector from '@common/components/MultiTypeFieldSelector/MultiTypeFieldSelector'
import { ExpressionsListInput } from '@common/components/ExpressionsListInput/ExpressionsListInput'
 
import type { PolicyStepFormData } from '../../PolicyStepTypes'
import { PolicySetModal } from '../PolicySetModal/PolicySetModal'
import { MiniPolicySetRenderer } from '../PolicySetListRenderer/MiniPolicySetRenderer'
 
import css from './MultiTypePolicySetSelector.module.scss'
 
export interface MultiTypePolicySetSelectorInternalProps extends Omit<IFormGroupProps, 'label'> {
  formik?: FormikContext<PolicyStepFormData>
  name: string
  label: string
  expressions?: string[]
  allowableTypes?: MultiTypeInputType[]
}
 
export function MultiTypePolicySetSelectorInternal(props: MultiTypePolicySetSelectorInternalProps): React.ReactElement {
  const {
    formik,
    name,
    label,
    expressions = [],
    allowableTypes = [MultiTypeInputType.FIXED, MultiTypeInputType.RUNTIME, MultiTypeInputType.EXPRESSION],
    disabled
  } = props
 
  const policySetIds = get(formik?.values, name) || []
 
  const onTypeChange = () => {
    formik?.setFieldTouched(name, true)
  }
 
  return (
    <MultiTypeFieldSelector
      name={name}
      label={label}
      defaultValueToReset={[]}
      allowedTypes={allowableTypes}
      supportListOfExpressions={true}
      disableMultiSelectBtn={disabled}
      expressionRender={() => (
        <ExpressionsListInput name={name} value={policySetIds} readOnly={disabled} expressions={expressions} />
      )}
      onTypeChange={onTypeChange}
    >
      <PolicySetFixedTypeSelector name={name} disabled={disabled} formik={formik} policySetIds={policySetIds} />
    </MultiTypeFieldSelector>
  )
}
 
export const MultiTypePolicySetSelector = connect(MultiTypePolicySetSelectorInternal)
 
interface PolicySetFixedTypeSelectorProps extends IFormGroupProps {
  name: string
  policySetIds: string[]
  formik?: FormikContext<PolicyStepFormData>
}
 
function PolicySetFixedTypeSelector({ formik, name, policySetIds, disabled }: PolicySetFixedTypeSelectorProps) {
  const { getString } = useStrings()
 
  const [showModal, closeModal] = useModalHook(
    () => (
      <PolicySetModal
        name={name}
        formikProps={formik}
        policySetIds={policySetIds}
        closeModal={() => {
          /* istanbul ignore next */ formik?.setFieldTouched(name, true)
          closeModal()
        }}
      />
    ),
    [name, formik, policySetIds]
  )
 
  const deletePolicySet = (policySetId: string) => {
    const newPolicySetIds = policySetIds.filter(id => id !== policySetId)
    formik?.setFieldTouched(name, true)
    formik?.setFieldValue(name, newPolicySetIds)
  }
 
  return (
    <>
      {policySetIds.map(policySetId => {
        return <MiniPolicySetRenderer policySetId={policySetId} key={policySetId} deletePolicySet={deletePolicySet} />
      })}
      <Button
        minimal
        text={getString('common.policiesSets.addOrModifyPolicySet')}
        className={css.addModifyButton}
        withoutCurrentColor={true}
        iconProps={{ size: 14 }}
        disabled={disabled}
        onClick={showModal}
      />
    </>
  )
}