All files / modules/70-pipeline/components/PipelineSteps/Steps/JiraCreate JiraFieldsRenderer.tsx

87.1% Statements 27/31
85.71% Branches 24/28
85.71% Functions 6/7
87.1% Lines 27/31

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              111x 111x 111x 111x 111x   111x 111x   111x                               11x 11x           10x   1x     1x     11x 1x     11x 1x     11x 10x                       1x                           1x 1x                             111x 17x 17x 17x     11x                                      
/*
 * Copyright 2021 Harness Inc. All rights reserved.
 * Use of this source code is governed by the PolyForm Free Trial 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/05/PolyForm-Free-Trial-1.0.0.txt.
 */
 
import React, { useCallback } from 'react'
import cx from 'classnames'
import { isEmpty } from 'lodash-es'
import { Button, FormInput, Layout } from '@wings-software/uicore'
import { useVariablesExpression } from '@pipeline/components/PipelineStudio/PiplineHooks/useVariablesExpression'
import type { JiraFieldNG } from 'services/cd-ng'
import { isApprovalStepFieldDisabled } from '../Common/ApprovalCommons'
import { setAllowedValuesOptions } from '../JiraApproval/helper'
import type { JiraFieldNGWithValue } from './types'
import css from './JiraCreate.module.scss'
 
export interface JiraFieldsRendererProps {
  selectedFields?: JiraFieldNGWithValue[]
  readonly?: boolean
  onDelete: (index: number, selectedField: JiraFieldNG) => void
}
 
interface MappedComponentInterface {
  selectedField: JiraFieldNG
  props: JiraFieldsRendererProps
  expressions: string[]
  index: number
}
 
function GetMappedFieldComponent({ selectedField, props, expressions, index }: MappedComponentInterface) {
  const showTextField = useCallback(() => {
    if (
      selectedField.schema.type === 'string' ||
      selectedField.schema.type === 'date' ||
      selectedField.schema.type === 'datetime' ||
      selectedField.schema.type === 'number'
    ) {
      return true
    }
    Iif (isEmpty(selectedField.allowedValues) && selectedField.schema.type === 'option' && selectedField.schema.array) {
      return true
    }
    return false
  }, [selectedField])
 
  const showMultiSelectField = useCallback(() => {
    return selectedField.allowedValues && selectedField.schema.type === 'option' && selectedField.schema.array
  }, [selectedField])
 
  const showMultiTypeField = useCallback(() => {
    return selectedField.allowedValues && selectedField.schema.type === 'option'
  }, [selectedField])
 
  if (showTextField()) {
    return (
      <FormInput.MultiTextInput
        label={selectedField.name}
        disabled={isApprovalStepFieldDisabled(props.readonly)}
        name={`spec.selectedFields[${index}].value`}
        placeholder={selectedField.name}
        className={css.md}
        multiTextInputProps={{
          expressions
        }}
      />
    )
  } else Iif (showMultiSelectField()) {
    return (
      <FormInput.MultiSelectTypeInput
        selectItems={setAllowedValuesOptions(selectedField.allowedValues)}
        label={selectedField.name}
        disabled={isApprovalStepFieldDisabled(props.readonly)}
        name={`spec.selectedFields[${index}].value`}
        placeholder={selectedField.name}
        className={cx(css.multiSelect, css.md)}
        multiSelectTypeInputProps={{
          expressions
        }}
      />
    )
  } else Eif (showMultiTypeField()) {
    return (
      <FormInput.MultiTypeInput
        selectItems={setAllowedValuesOptions(selectedField.allowedValues)}
        label={selectedField.name}
        name={`spec.selectedFields[${index}].value`}
        placeholder={selectedField.name}
        disabled={isApprovalStepFieldDisabled(props.readonly)}
        className={cx(css.multiSelect, css.md)}
        multiTypeInputProps={{ expressions }}
      />
    )
  }
  return null
}
 
export function JiraFieldsRenderer(props: JiraFieldsRendererProps) {
  const { expressions } = useVariablesExpression()
  const { readonly, selectedFields, onDelete } = props
  return selectedFields ? (
    <>
      {selectedFields?.map((selectedField: JiraFieldNG, index: number) => (
        <Layout.Horizontal className={css.alignCenter} key={selectedField.name}>
          <GetMappedFieldComponent
            selectedField={selectedField}
            props={props}
            expressions={expressions}
            index={index}
          />
          <Button
            minimal
            icon="trash"
            disabled={isApprovalStepFieldDisabled(readonly)}
            data-testid={`remove-selectedField-${index}`}
            onClick={() => onDelete(index, selectedField)}
          />
        </Layout.Horizontal>
      ))}
    </>
  ) : null
}