All files / modules/70-pipeline/components/ManifestSelection DragnDropPaths.tsx

48.98% Statements 24/49
65.52% Branches 19/29
41.18% Functions 7/17
48.98% Lines 24/49

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              102x                   102x 102x 102x   102x 102x 102x   102x 102x                   102x                 22x 22x         22x       22x       22x                 22x                                 22x                           22x     66x                           22x     21x 21x                                                                   63x                                                                   102x  
/*
 * 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 {
  Layout,
  FormInput,
  MultiTypeInputType,
  ButtonVariation,
  Text,
  Button,
  Icon,
  ButtonSize
} from '@wings-software/uicore'
import { FieldArray, FieldArrayRenderProps } from 'formik'
import React, { useCallback } from 'react'
import { v4 as nameSpace, v5 as uuid } from 'uuid'
 
import { DragDropContext, Draggable, Droppable, DropResult } from 'react-beautiful-dnd'
import { useStrings } from 'framework/strings'
import MultiTypeFieldSelector from '@common/components/MultiTypeFieldSelector/MultiTypeFieldSelector'
 
import { ManifestDataType } from './Manifesthelper'
import css from './ManifestWizardSteps/ManifestDetails/ManifestDetails.module.scss'
 
export interface DragnDropPathsProps {
  formik: any
  expressions: any
  allowableTypes: MultiTypeInputType[]
  selectedManifest?: string | null
  pathLabel?: string | null
}
 
const defaultValueToReset = [{ path: '', uuid: uuid('', nameSpace()) }]
 
function DragnDropPaths({
  formik,
  selectedManifest,
  expressions,
  allowableTypes,
  pathLabel
}: DragnDropPathsProps): React.ReactElement {
  const { getString } = useStrings()
  const onDragStart = useCallback((event: React.DragEvent<HTMLDivElement>, index: number) => {
    event.dataTransfer.setData('data', index.toString())
    event.currentTarget.classList.add(css.dragging)
  }, [])
 
  const onDragEnd = useCallback((event: React.DragEvent<HTMLDivElement>) => {
    event.currentTarget.classList.remove(css.dragging)
  }, [])
 
  const onDragLeave = useCallback((event: React.DragEvent<HTMLDivElement>) => {
    event.currentTarget.classList.remove(css.dragOver)
  }, [])
 
  const onDragOver = useCallback((event: React.DragEvent<HTMLDivElement>) => {
    /* istanbul ignore else */
    Iif (event.preventDefault) {
      event.preventDefault()
    }
    event.currentTarget.classList.add(css.dragOver)
    event.dataTransfer.dropEffect = 'move'
  }, [])
 
  const onDrop = useCallback(
    (event: React.DragEvent<HTMLDivElement>, arrayHelpers: FieldArrayRenderProps, droppedIndex: number) => {
      /* istanbul ignore else */
      Iif (event.preventDefault) {
        event.preventDefault()
      }
      const data = event.dataTransfer.getData('data')
      /* istanbul ignore else */
      Iif (data) {
        const index = parseInt(data, 10)
        arrayHelpers.swap(index, droppedIndex)
      }
      event.currentTarget.classList.remove(css.dragOver)
    },
    []
  )
 
  return (
    <DragDropContext
      onDragEnd={(result: DropResult) => {
        if (!result.destination) {
          return
        }
        const res = Array.from(formik.values.paths)
        const [removed] = res.splice(result.source.index, 1)
        res.splice(result.destination.index, 0, removed)
        formik.setFieldValue('paths', res as any)
      }}
    >
      <Droppable droppableId="droppable">
        {(provided, _snapshot) => (
          <div className={css.halfWidth} {...provided.droppableProps} ref={provided.innerRef}>
            <MultiTypeFieldSelector
              defaultValueToReset={defaultValueToReset}
              allowedTypes={allowableTypes.filter(allowedType => allowedType !== MultiTypeInputType.EXPRESSION)}
              name={'paths'}
              label={
                <Text>
                  {pathLabel
                    ? pathLabel
                    : selectedManifest === ManifestDataType.K8sManifest
                    ? getString('fileFolderPathText')
                    : getString('common.git.filePath')}
                </Text>
              }
            >
              <FieldArray
                name="paths"
                render={arrayHelpers => (
                  <Layout.Vertical>
                    {formik.values?.paths?.map((draggablepath: { path: string; uuid: string }, index: number) => (
                      <Draggable key={draggablepath.uuid} draggableId={draggablepath.uuid} index={index}>
                        {providedDrag => (
                          <Layout.Horizontal
                            key={draggablepath.uuid}
                            flex={{ distribution: 'space-between', alignItems: 'flex-start' }}
                            ref={providedDrag.innerRef}
                            {...providedDrag.draggableProps}
                            {...providedDrag.dragHandleProps}
                          >
                            <Layout.Horizontal
                              spacing="medium"
                              style={{ alignItems: 'baseline' }}
                              draggable={true}
                              onDragStart={event => {
                                onDragStart(event, index)
                              }}
                              onDragEnd={onDragEnd}
                              onDragOver={onDragOver}
                              onDragLeave={onDragLeave}
                              onDrop={event => onDrop(event, arrayHelpers, index)}
                            >
                              <Icon name="drag-handle-vertical" className={css.drag} />
                              <Text width={12}>{`${index + 1}.`}</Text>
                              <FormInput.MultiTextInput
                                label={''}
                                placeholder={
                                  selectedManifest === ManifestDataType.K8sManifest
                                    ? getString('pipeline.manifestType.manifestPathPlaceholder')
                                    : getString('pipeline.manifestType.pathPlaceholder')
                                }
                                name={`paths[${index}].path`}
                                style={{ width: 275 }}
                                multiTextInputProps={{
                                  expressions,
                                  allowableTypes: allowableTypes.filter(
                                    allowedType => allowedType !== MultiTypeInputType.RUNTIME
                                  )
                                }}
                              />
                            </Layout.Horizontal>
                            {formik.values?.paths?.length > 1 && (
                              <Button minimal icon="main-trash" onClick={() => arrayHelpers.remove(index)} />
                            )}
                          </Layout.Horizontal>
                        )}
                      </Draggable>
                    ))}
                    {provided.placeholder}
                    <span>
                      <Button
                        text={getString('addFileText')}
                        icon="plus"
                        size={ButtonSize.SMALL}
                        variation={ButtonVariation.LINK}
                        className={css.addFileButton}
                        onClick={() => arrayHelpers.push({ path: '', uuid: uuid('', nameSpace()) })}
                      />
                    </span>
                  </Layout.Vertical>
                )}
              />
            </MultiTypeFieldSelector>
          </div>
        )}
      </Droppable>
    </DragDropContext>
  )
}
 
export default DragnDropPaths