All files / modules/75-cd/components/PipelineSteps/HttpStep OptionalConfiguration.tsx

87.5% Statements 21/24
40% Branches 2/5
70% Functions 7/10
87.5% Lines 21/24

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 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186              97x 97x 97x 97x   97x 97x   97x 97x   97x 97x   97x         19x 19x         19x   19x                                                                         19x             17x                                                           1x                                             19x               13x                                                             1x                              
/*
 * 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 { FieldArray, FormikProps } from 'formik'
import { Button, ButtonVariation, FormInput, getMultiTypeFromValue, MultiTypeInputType } from '@wings-software/uicore'
import { v4 as uuid } from 'uuid'
 
import MultiTypeFieldSelector from '@common/components/MultiTypeFieldSelector/MultiTypeFieldSelector'
import { useStrings } from 'framework/strings'
 
import { useVariablesExpression } from '@pipeline/components/PipelineStudio/PiplineHooks/useVariablesExpression'
import { ConfigureOptions } from '@common/components/ConfigureOptions/ConfigureOptions'
import type { HttpStepFormData, HttpStepHeaderConfig, HttpStepOutputVariable } from './types'
import stepCss from '@pipeline/components/PipelineSteps/Steps/Steps.module.scss'
import css from './HttpStep.module.scss'
 
export default function OptionalConfiguration(props: {
  formik: FormikProps<HttpStepFormData>
  readonly?: boolean
  allowableTypes?: MultiTypeInputType[]
}): React.ReactElement {
  const { getString } = useStrings()
  const { expressions } = useVariablesExpression()
  const {
    formik: { values: formValues, setFieldValue },
    readonly,
    allowableTypes = [MultiTypeInputType.FIXED, MultiTypeInputType.EXPRESSION, MultiTypeInputType.RUNTIME]
  } = props
 
  return (
    <div className={stepCss.stepPanel}>
      <div className={stepCss.formGroup}>
        <FormInput.MultiTextInput
          name="spec.assertion"
          placeholder={getString('pipeline.utilitiesStep.assertion')}
          label={getString('assertionLabel')}
          isOptional
          optionalLabel={getString('common.optionalLabel')}
          disabled={readonly}
          multiTextInputProps={{ expressions, disabled: readonly, allowableTypes }}
        />
        {getMultiTypeFromValue(formValues.spec.assertion) === MultiTypeInputType.RUNTIME && (
          <ConfigureOptions
            value={formValues.spec.assertion}
            type="String"
            variableName="spec.assertion"
            showRequiredField={false}
            showDefaultField={false}
            showAdvanced={true}
            onChange={value => setFieldValue('spec.assertion', value)}
            isReadonly={readonly}
          />
        )}
      </div>
      <div className={stepCss.formGroup}>
        <MultiTypeFieldSelector
          name="spec.headers"
          label={getString('common.headers')}
          isOptional
          optionalLabel={getString('common.optionalLabel')}
          defaultValueToReset={[{ name: '', type: 'String', value: '', id: uuid() }]}
          disableTypeSelection
        >
          <FieldArray
            name="spec.headers"
            render={({ push, remove }) => {
              return (
                <div className={css.panel}>
                  <div className={css.headerRow}>
                    <span className={css.label}>Key</span>
                    <span className={css.label}>Value</span>
                  </div>
                  {formValues.spec.headers.map(({ id }: HttpStepHeaderConfig, i: number) => (
                    <div className={css.headerRow} key={id}>
                      <FormInput.Text
                        name={`spec.headers[${i}].key`}
                        placeholder={getString('pipeline.keyPlaceholder')}
                        disabled={readonly}
                      />
                      <FormInput.MultiTextInput
                        name={`spec.headers[${i}].value`}
                        placeholder={getString('common.valuePlaceholder')}
                        disabled={readonly}
                        multiTextInputProps={{
                          allowableTypes: allowableTypes,
                          expressions,
                          disabled: readonly
                        }}
                        label=""
                      />
                      <Button
                        variation={ButtonVariation.ICON}
                        icon="main-trash"
                        data-testid={`remove-header-${i}`}
                        onClick={() => remove(i)}
                        disabled={readonly}
                      />
                    </div>
                  ))}
                  <Button
                    icon="plus"
                    variation={ButtonVariation.LINK}
                    data-testid="add-header"
                    onClick={() => push({ key: '', value: '', id: uuid() })}
                    disabled={readonly}
                    className={css.addButton}
                  >
                    {getString('add')}
                  </Button>
                </div>
              )
            }}
          />
        </MultiTypeFieldSelector>
      </div>
      <div className={stepCss.formGroup}>
        <MultiTypeFieldSelector
          name="spec.outputVariables"
          label={getString('outputLabel')}
          isOptional
          optionalLabel={getString('common.optionalLabel')}
          disableTypeSelection
        >
          <FieldArray
            name="spec.outputVariables"
            render={({ push, remove }) => {
              return (
                <div className={css.panel}>
                  <div className={css.responseMappingRow}>
                    <span className={css.label}>Variable Name</span>
                    <span className={css.label}>Value</span>
                  </div>
                  {((formValues.spec.outputVariables as HttpStepOutputVariable[]) || []).map(
                    ({ id }: HttpStepOutputVariable, i: number) => (
                      <div className={css.responseMappingRow} key={id}>
                        <FormInput.Text
                          name={`spec.outputVariables[${i}].name`}
                          placeholder={getString('name')}
                          disabled={readonly}
                        />
                        <FormInput.MultiTextInput
                          name={`spec.outputVariables[${i}].value`}
                          placeholder={getString('valueLabel')}
                          disabled={readonly}
                          multiTextInputProps={{
                            allowableTypes: allowableTypes,
                            expressions,
                            disabled: readonly
                          }}
                          label=""
                        />
                        <Button
                          variation={ButtonVariation.ICON}
                          icon="main-trash"
                          data-testid={`remove-response-mapping-${i}`}
                          onClick={() => remove(i)}
                          disabled={readonly}
                        />
                      </div>
                    )
                  )}
                  <Button
                    icon="plus"
                    variation={ButtonVariation.LINK}
                    data-testid="add-response-mapping"
                    onClick={() => push({ name: '', value: '', type: 'String', id: uuid() })}
                    disabled={readonly}
                    className={css.addButton}
                  >
                    {getString('add')}
                  </Button>
                </div>
              )
            }}
          />
        </MultiTypeFieldSelector>
      </div>
    </div>
  )
}