All files / modules/70-pipeline/components/InputSetSelector RenderValue.tsx

86.49% Statements 32/37
50% Branches 9/18
100% Functions 9/9
85.71% Lines 30/35

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              38x 38x 38x   38x 38x 38x 38x 38x   38x                         194x   1x 1x   1x 1x 1x 1x 1x 2x 1x                   1x         194x 194x     204x           1x         1x               2x 4x 2x 2x                                                                         12x                    
/*
 * 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, { Dispatch, SetStateAction } from 'react'
import { clone } from 'lodash-es'
import cx from 'classnames'
 
import { Button, ButtonVariation, Layout, Text } from '@harness/uicore'
import { Color } from '@harness/design-system'
import { useStrings } from 'framework/strings'
import { getIconByType, InputSetValue, onDragEnd, onDragLeave, onDragOver, onDragStart } from './utils'
import css from './InputSetSelector.module.scss'
 
export function RenderValue({
  value,
  onChange,
  setSelectedInputSets,
  setOpenInputSetsList,
  selectedValueClass
}: {
  value: InputSetValue[]
  onChange?: (value?: InputSetValue[]) => void
  setSelectedInputSets: Dispatch<SetStateAction<InputSetValue[]>>
  setOpenInputSetsList: Dispatch<SetStateAction<boolean>>
  selectedValueClass?: string
}): JSX.Element {
  const onDrop = React.useCallback(
    (event: React.DragEvent<HTMLLIElement>, droppedLocation: InputSetValue) => {
      Eif (event.preventDefault) {
        event.preventDefault()
      }
      const data = event.dataTransfer.getData('data')
      Eif (data) {
        try {
          const dropInputSet: InputSetValue = JSON.parse(data)
          const selected = clone(value)
          const droppedItem = selected.filter(item => item.value === dropInputSet.value)[0]
          Iif (droppedItem) {
            const droppedItemIndex = selected.indexOf(droppedItem)
            const droppedLocationIndex = selected.indexOf(droppedLocation)
            selected.splice(droppedItemIndex, 1)
            selected.splice(droppedLocationIndex, 0, droppedItem)
            onChange?.(selected)
          }
          // eslint-disable-next-line no-empty
        } catch {}
      }
      event.currentTarget.classList.remove(css.dragOver)
    },
    [value]
  )
 
  const { getString } = useStrings()
  return (
    <div className={cx(css.renderSelectedValue, selectedValueClass)}>
      {value?.map((item, index) => (
        <li
          key={index + item.label}
          data-testid={item.value}
          className={css.selectedInputSetLi}
          draggable={true}
          onDragStart={event => {
            onDragStart(event, item)
          }}
          onDragEnd={onDragEnd}
          onDragOver={onDragOver}
          onDragLeave={onDragLeave}
          onDrop={event => onDrop(event, item)}
        >
          <Button
            data-testid={`button-${item.label}`}
            round={true}
            rightIcon="cross"
            iconProps={{
              onClick: event => {
                event.stopPropagation()
                const valuesAfterRemoval = value.filter(inputset => inputset.value !== item.value)
                setSelectedInputSets(valuesAfterRemoval)
                onChange?.(valuesAfterRemoval)
              },
              style: {
                cursor: 'pointer'
              }
            }}
            text={
              <Layout.Horizontal flex={{ alignItems: 'center' }} spacing="small">
                <Button
                  round={true}
                  withoutBoxShadow={true}
                  font={{ weight: 'semi-bold' }}
                  width={20}
                  height={20}
                  className={css.selectedInputSetOrder}
                >
                  {index + 1}
                </Button>
                <Text
                  color={Color.PRIMARY_8}
                  icon={getIconByType(item.type)}
                  className={css.selectedInputSetLabel}
                  iconProps={{ className: css.selectedInputSetTypeIcon }}
                >
                  {item.label}
                </Text>
              </Layout.Horizontal>
            }
            margin={{ top: 'small', bottom: 'small', left: 0, right: 'small' }}
            className={css.selectedInputSetCard}
            color={Color.PRIMARY_7}
          />
        </li>
      ))}
      <Button
        icon="small-plus"
        className={css.addInputSetButton}
        onClick={() => setOpenInputSetsList(true)}
        color={Color.PRIMARY_7}
        minimal
        variation={ButtonVariation.LINK}
      >
        {getString('pipeline.inputSets.selectPlaceholder')}
      </Button>
    </div>
  )
}