All files / modules/72-triggers/pages/triggers/views AddConditionsSection.tsx

58.97% Statements 23/39
25.3% Branches 21/83
10% Functions 1/10
55.56% Lines 20/36

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 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225              6x 6x 6x 6x 6x 6x 6x 6x                                                 6x                               6x                 2x 2x 2x 2x 2x 2x 2x                                                                     6x                                                           6x                                                                                                                                                                               6x  
/*
 * 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 { FormInput, Text, Icon, Container, HarnessDocTooltip } from '@wings-software/uicore'
import { Color } from '@harness/design-system'
import cx from 'classnames'
import { FieldArray } from 'formik'
import { useStrings, UseStringsReturn } from 'framework/strings'
import { mockOperators, inNotInArr, inNotInPlaceholder } from '../utils/TriggersWizardPageUtils'
import css from './WebhookConditionsPanel.module.scss'
 
export interface AddConditionInterface {
  key: string
  operator: string
  value: string
}
 
interface AddConditionsSectionPropsInterface {
  title: string
  fieldId: string
  attributePlaceholder?: string
  formikValues?: { [key: string]: any }
  setFieldValue: (field: string, value: any, shouldValidate?: boolean) => void
  errors: { [key: string]: string }
}
 
interface AddConditionRowInterface {
  fieldId: string
  index: number
  attributePlaceholder: string
  operatorPlaceholder: string
  valuePlaceholder: string
}
 
export const ConditionsRowHeaders = ({ getString }: { getString?: UseStringsReturn['getString'] }) => (
  <Container className={css.conditionsRowHeaders}>
    <Text font={{ size: 'xsmall', weight: 'bold' }}>
      {getString?.('triggers.conditionsPanel.attribute').toUpperCase()}
    </Text>
    <Text font={{ size: 'xsmall', weight: 'bold' }}>
      {getString?.('triggers.conditionsPanel.operator').toUpperCase()}
    </Text>
    <Text font={{ size: 'xsmall', weight: 'bold' }}>
      {getString?.('triggers.conditionsPanel.matchesValue').toUpperCase()}
    </Text>
  </Container>
)
// }
 
// Has first class support with predefined attribute
export const ConditionRow = ({
  formikProps,
  name,
  label
}: {
  formikProps: any
  name: string
  label: string
}): JSX.Element => {
  const { getString } = useStrings()
  const operatorKey = `${name}Operator`
  const valueKey = `${name}Value`
  const operatorError = formikProps?.errors?.[operatorKey]
  const valueError = formikProps?.errors?.[valueKey]
  const operatorValue = formikProps?.values?.[operatorKey]
  return (
    <div className={cx(css.conditionsRow, css.predefinedRows)}>
      <Text color={Color.GREY_800} data-tooltip-id={name}>
        {label}
        <HarnessDocTooltip tooltipId={name} useStandAlone={true} />
      </Text>
      <FormInput.Select
        style={{ alignSelf: valueError ? 'baseline' : 'center' }}
        className={css.operatorContainer}
        items={mockOperators}
        name={operatorKey}
        label=""
        placeholder={getString('pipeline.operatorPlaceholder')}
        onChange={() => {
          formikProps.setFieldTouched(valueKey, true)
        }}
      />
      <FormInput.Text
        name={valueKey}
        style={{ alignSelf: operatorError ? 'baseline' : 'center' }}
        className={css.textContainer}
        label=""
        onChange={() => {
          formikProps.setFieldTouched(operatorKey, true)
        }}
        placeholder={
          inNotInArr.includes(operatorValue)
            ? inNotInPlaceholder
            : getString('triggers.conditionsPanel.matchesValuePlaceholder')
        }
      />
    </div>
  )
}
 
const AddConditionRow: React.FC<AddConditionRowInterface> = ({
  fieldId,
  index,
  attributePlaceholder,
  operatorPlaceholder,
  valuePlaceholder
}) => (
  <div className={cx(css.conditionsRow)}>
    <FormInput.Text
      className={css.textContainer}
      placeholder={attributePlaceholder}
      name={`${fieldId}.${[index]}.key`}
      label=""
    />
    <FormInput.Select
      className={css.operatorContainer}
      placeholder={operatorPlaceholder}
      items={mockOperators}
      name={`${fieldId}.${[index]}.operator`}
      label=""
    />
    <FormInput.Text
      className={css.textContainer}
      name={`${fieldId}.${[index]}.value`}
      label=""
      placeholder={valuePlaceholder}
    />
  </div>
)
 
export const AddConditionsSection: React.FC<AddConditionsSectionPropsInterface> = ({
  title,
  fieldId,
  attributePlaceholder = '',
  formikValues,
  setFieldValue,
  errors
}) => {
  const { getString } = useStrings()
  const addConditions = formikValues?.[fieldId] || []
  return (
    <section data-name={fieldId}>
      <Text className={css.sectionHeader} data-tooltip-id={fieldId}>
        {title}
        <HarnessDocTooltip tooltipId={fieldId} useStandAlone={true} />
      </Text>
      <FieldArray
        name={fieldId}
        render={() => (
          <>
            {addConditions?.length ? <ConditionsRowHeaders getString={getString} /> : null}
            {addConditions?.map((_addCondition: AddConditionInterface, index: number) => (
              <Container key={index} className={css.rowContainer}>
                <AddConditionRow
                  index={index}
                  fieldId={fieldId}
                  attributePlaceholder={attributePlaceholder}
                  operatorPlaceholder={getString('pipeline.operatorPlaceholder')}
                  valuePlaceholder={
                    inNotInArr.includes(formikValues?.[fieldId]?.[index]?.operator)
                      ? inNotInPlaceholder
                      : getString('triggers.conditionsPanel.matchesValuePlaceholder')
                  }
                />
                <Icon
                  className={css.rowTrashIcon}
                  data-name="main-delete"
                  size={14}
                  color={Color.GREY_500}
                  name="main-trash"
                  onClick={() => {
                    const newAddConditions = [...addConditions]
                    newAddConditions.splice(index, 1)
                    setFieldValue(fieldId, newAddConditions)
                  }}
                />
              </Container>
            ))}
            {(addConditions?.length && errors[fieldId] && (
              <Text color={Color.RED_500} style={{ marginBottom: 'var(--spacing-medium)' }}>
                {errors[fieldId]}
              </Text>
            )) ||
              null}
          </>
        )}
      />
      <Text
        style={{ cursor: 'pointer', marginTop: 'var(--spacing-small)' }}
        width={43}
        color={Color.PRIMARY_7}
        data-name="plusAdd"
        onClick={() => {
          const emptyRow = { key: '', operator: '', value: '' }
          if (!addConditions) {
            setFieldValue(fieldId, [emptyRow])
          } else {
            setFieldValue(fieldId, [...addConditions, emptyRow])
          }
        }}
      >
        {getString('plusAdd')}
      </Text>
      {/* <Button
        variation={ButtonVariation.LINK}
        data-name="plusAdd"
        style={{ padding: 0, fontWeight: 'normal' }}
        onClick={() => {
          const emptyRow = { key: '', operator: '', value: '' }
          if (!addConditions) setFieldValue(fieldId, [emptyRow])
          else setFieldValue(fieldId, [...addConditions, emptyRow])
        }}
        text={getString('plusAdd')}
      /> */}
    </section>
  )
}
 
export default AddConditionsSection