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

100% Statements 19/19
100% Branches 6/6
100% Functions 1/1
100% Lines 19/19

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              97x   97x   97x 97x   97x 97x 97x 97x   97x 97x     97x   97x   97x   97x             7x   7x 7x 7x   7x                                                                                                                                                                  
/*
 * 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 type { FormikProps } from 'formik'
import cx from 'classnames'
 
import { SelectOption, FormInput, MultiTypeInputType } from '@harness/uicore'
import { useStrings } from 'framework/strings'
 
import { FormMultiTypeDurationField } from '@common/components/MultiTypeDuration/MultiTypeDuration'
import { NameId } from '@common/components/NameIdDescriptionTags/NameIdDescriptionTags'
import MultiTypeFieldSelector from '@common/components/MultiTypeFieldSelector/MultiTypeFieldSelector'
import { MonacoTextField } from '@common/components/MonacoTextField/MonacoTextField'
 
import { useVariablesExpression } from '@pipeline/components/PipelineStudio/PiplineHooks/useVariablesExpression'
import { StepViewType } from '@pipeline/components/AbstractSteps/Step'
 
import type { PolicyStepFormData } from './PolicyStepTypes'
import { MultiTypePolicySetSelector } from './PolicySets/MultiTypePolicySetSelector/MultiTypePolicySetSelector'
 
import stepCss from '@pipeline/components/PipelineSteps/Steps/Steps.module.scss'
 
export const entityTypeOptions: SelectOption[] = [{ label: 'Custom', value: 'Custom' }]
 
export default function BasePolicyStep(props: {
  formik: FormikProps<PolicyStepFormData>
  isNewStep: boolean
  readonly?: boolean
  stepViewType?: StepViewType
  allowableTypes: MultiTypeInputType[]
}): React.ReactElement {
  const { isNewStep, readonly, stepViewType, allowableTypes } = props
 
  const [entityType, setEntityType] = useState<string | number | symbol>('Custom')
  const { getString } = useStrings()
  const { expressions } = useVariablesExpression()
 
  return (
    <>
      {stepViewType !== StepViewType.Template && (
        <div className={cx(stepCss.formGroup, stepCss.lg)}>
          <NameId
            nameLabel={getString('pipelineSteps.stepNameLabel')}
            inputGroupProps={{ disabled: readonly }}
            identifierProps={{ isIdentifierEditable: isNewStep && !readonly }}
          />
        </div>
      )}
      <div className={cx(stepCss.formGroup, stepCss.lg)}>
        <FormMultiTypeDurationField
          name="timeout"
          label={getString('pipelineSteps.timeoutLabel')}
          disabled={readonly}
          className={stepCss.duration}
          multiTypeDurationProps={{
            enableConfigureOptions: false,
            expressions,
            disabled: readonly
          }}
        />
      </div>
      <div className={cx(stepCss.formGroup, stepCss.lg)}>
        <FormInput.Select
          name="spec.type"
          label={getString('common.entityType')}
          disabled={readonly}
          onChange={/* istanbul ignore next */ option => setEntityType(option?.value)}
          items={entityTypeOptions}
        />
      </div>
      <div className={cx(stepCss.formGroup, stepCss.alignStart)}>
        <MultiTypePolicySetSelector
          name="spec.policySets"
          label={getString('common.policiesSets.policyset')}
          expressions={expressions}
          allowableTypes={allowableTypes}
          disabled={readonly}
        />
      </div>
      {entityType === 'Custom' && (
        <div className={cx(stepCss.formGroup, stepCss.alignStart)}>
          <MultiTypeFieldSelector
            name="spec.policySpec.payload"
            label={getString('common.payload')}
            defaultValueToReset=""
            allowedTypes={allowableTypes}
            skipRenderValueInExpressionLabel
            disabled={readonly}
            disableTypeSelection={readonly}
            expressionRender={
              /* istanbul ignore next */ () => {
                return (
                  <MonacoTextField
                    name={'spec.policySpec.payload'}
                    expressions={expressions}
                    height={300}
                    disabled={readonly}
                    fullScreenAllowed
                    fullScreenTitle={getString('common.payload')}
                  />
                )
              }
            }
          >
            <MonacoTextField
              name={'spec.policySpec.payload'}
              expressions={expressions}
              height={300}
              disabled={readonly}
              fullScreenAllowed
              fullScreenTitle={getString('common.payload')}
            />
          </MultiTypeFieldSelector>
        </div>
      )}
    </>
  )
}