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

86.11% Statements 31/36
75% Branches 15/20
100% Functions 7/7
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 131 132 133 134 135 136 137 138 139              38x 38x 38x   38x 38x 38x 38x 38x   38x 38x 38x                               38x 47x 47x   47x   1x 1x   1x 1x 1x 1x 1x 2x 1x                   1x         47x   45x       3x                           1x         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 { clone, defaultTo } from 'lodash-es'
import cx from 'classnames'
 
import { Checkbox, Container, Icon, Layout, Text } from '@harness/uicore'
import { Color } from '@harness/design-system'
import { isInputSetInvalid } from '@pipeline/utils/inputSetUtils'
import { Badge } from '@pipeline/pages/utils/Badge/Badge'
import { useStrings } from 'framework/strings'
import type { EntityGitDetails, InputSetErrorWrapper, InputSetSummaryResponse } from 'services/pipeline-ng'
import { getIconByType, InputSetValue, onDragEnd, onDragLeave, onDragOver, onDragStart } from './utils'
import { InputSetGitDetails } from './InputSetGitDetails'
import css from './InputSetSelector.module.scss'
 
interface SelectedMultipleListProps {
  selectedInputSets: InputSetValue[]
  setSelectedInputSets: React.Dispatch<React.SetStateAction<InputSetValue[]>>
  onCheckBoxHandler: (
    checked: boolean,
    label: string,
    val: string,
    type: InputSetSummaryResponse['inputSetType'],
    inputSetGitDetails: EntityGitDetails | null,
    inputSetErrorDetails?: InputSetErrorWrapper,
    overlaySetErrorDetails?: { [key: string]: string }
  ) => void
}
 
export default function SelectedMultipleList(props: SelectedMultipleListProps): JSX.Element {
  const { selectedInputSets, setSelectedInputSets, onCheckBoxHandler } = props
  const { getString } = useStrings()
 
  const onDropPreSelect = 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 clonedSelectedInputSets = clone(selectedInputSets)
          const droppedItem = clonedSelectedInputSets.filter(item => item.value === dropInputSet.value)[0]
          Iif (droppedItem) {
            const droppedItemIndex = clonedSelectedInputSets.indexOf(droppedItem)
            const droppedLocationIndex = clonedSelectedInputSets.indexOf(droppedLocation)
            clonedSelectedInputSets.splice(droppedItemIndex, 1)
            clonedSelectedInputSets.splice(droppedLocationIndex, 0, droppedItem)
            setSelectedInputSets(clonedSelectedInputSets)
          }
          // eslint-disable-next-line no-empty
        } catch {}
      }
      event.currentTarget.classList.remove(css.dragOver)
    },
    [selectedInputSets]
  )
 
  return (
    <>
      {selectedInputSets.map((selected, index) => (
        <li
          className={cx(css.item)}
          onClick={() => {
            onCheckBoxHandler(
              false,
              selected.label,
              selected.value as string,
              selected.type,
              defaultTo(selected.gitDetails, {}),
              selected.inputSetErrorDetails,
              selected.overlaySetErrorDetails
            )
          }}
          key={`${index}-${selected.value as string}`}
          data-testid={`${index}-${selected.value as string}`}
          draggable={true}
          onDragStart={event => {
            onDragStart(event, selected)
          }}
          onDragEnd={onDragEnd}
          onDragOver={onDragOver}
          onDragLeave={onDragLeave}
          onDrop={event => onDropPreSelect(event, selected)}
        >
          <Layout.Horizontal flex={{ distribution: 'space-between' }}>
            <Checkbox
              className={css.checkbox}
              labelElement={
                <Layout.Horizontal
                  flex={{ alignItems: 'center', justifyContent: 'flex-start' }}
                  padding={{ left: true }}
                  width="100%"
                >
                  <Icon name={getIconByType(selected.type)}></Icon>
                  <Container margin={{ left: true }} className={css.nameIdContainer}>
                    <Text lineClamp={1} font={{ weight: 'bold' }} color={Color.GREY_800}>
                      {selected.label}
                    </Text>
                    <Text font="small" lineClamp={1} margin={{ top: 'xsmall' }} color={Color.GREY_450}>
                      {getString('idLabel', { id: selected.value })}
                    </Text>
                  </Container>
                </Layout.Horizontal>
              }
              checked={true}
            />
            {selected.gitDetails?.repoIdentifier ? <InputSetGitDetails gitDetails={selected.gitDetails} /> : null}
            {isInputSetInvalid(selected) && (
              <Container padding={{ left: 'large' }}>
                <Badge
                  text={'common.invalid'}
                  iconName="error-outline"
                  showTooltip={true}
                  entityName={selected.name}
                  entityType={selected.inputSetType === 'INPUT_SET' ? 'Input Set' : 'Overlay Input Set'}
                  uuidToErrorResponseMap={selected.inputSetErrorDetails?.uuidToErrorResponseMap}
                  overlaySetErrorDetails={selected.overlaySetErrorDetails}
                />
              </Container>
            )}
          </Layout.Horizontal>
          <span className={css.order}>
            <Text className={css.orderText}>{index + 1}</Text>
            <Icon name="drag-handle-vertical" className={css.drag} size={16} />
          </span>
        </li>
      ))}
    </>
  )
}