All files / modules/70-pipeline/components/PipelineSteps/Steps/CustomVariables AddEditCustomVariable.tsx

87.5% Statements 21/24
73.91% Branches 17/23
83.33% Functions 5/6
86.96% Lines 20/23

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              113x 113x 113x   113x     113x   113x                               113x 135x 135x   135x 135x     135x           1x     135x   135x                                                     1x 1x 1x       1x         11x                                                                  
/*
 * 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 * as Yup from 'yup'
import { Button, Formik, FormikForm, FormInput, ButtonVariation, Dialog } from '@wings-software/uicore'
 
import { useStrings } from 'framework/strings'
import type { AllNGVariables } from '@pipeline/utils/types'
 
import { getVaribaleTypeOptions } from './CustomVariableUtils'
 
const MAX_LENGTH = 64
 
export interface VariableState {
  variable: AllNGVariables
  index: number
}
 
export interface AddEditCustomVariableProps {
  selectedVariable: VariableState | null
  setSelectedVariable(variable: VariableState | null): void
  addNewVariable(variable: AllNGVariables): void
  updateVariable(index: number, variable: AllNGVariables): void
  existingVariables?: AllNGVariables[]
  formName?: string
}
 
export default function AddEditCustomVariable(props: AddEditCustomVariableProps): React.ReactElement {
  const { selectedVariable, setSelectedVariable, addNewVariable, updateVariable, existingVariables, formName } = props
  const { getString } = useStrings()
 
  const existingNames: string[] = Array.isArray(existingVariables) ? existingVariables.map(v => v.name || '') : []
  const isEdit = selectedVariable && typeof selectedVariable.index === 'number' && selectedVariable.index > -1
 
  // remove current variable name in case of edit
  Iif (isEdit) {
    // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
    existingNames.splice(selectedVariable!.index, 1)
  }
 
  function closeModal(): void {
    setSelectedVariable(null)
  }
 
  const actualFormName = formName || 'addEditCustomVariableForm'
 
  return (
    <Dialog
      className={'padded-dialog'}
      isOpen={!!selectedVariable}
      enforceFocus={false}
      title={isEdit ? getString('common.editVariable') : getString('common.addVariable')}
      onClose={closeModal}
    >
      <Formik
        formName={actualFormName}
        initialValues={selectedVariable?.variable}
        enableReinitialize
        validationSchema={Yup.object().shape({
          name: Yup.string()
            .trim()
            .required(getString('common.validation.nameIsRequired'))
            .max(
              MAX_LENGTH,
              getString('common.validation.fieldCannotbeLongerThanN', { name: getString('name'), n: MAX_LENGTH })
            )
            .matches(
              /^[a-zA-Z_][0-9a-zA-Z_$]*$/,
              getString('common.validation.fieldMustBeAlphanumeric', { name: getString('name') })
            )
            .notOneOf(existingNames, getString('common.validation.variableAlreadyExists'))
        })}
        onSubmit={data => {
          Eif (data && selectedVariable) {
            Eif (selectedVariable.index === -1) {
              addNewVariable(data)
            } else {
              updateVariable(selectedVariable.index, data)
            }
            closeModal()
          }
        }}
      >
        {({ submitForm }) => (
          <FormikForm data-testid="add-edit-variable">
            <FormInput.Text
              name="name"
              label={getString('variableNameLabel')}
              placeholder={getString('pipeline.variable.variableNamePlaceholder')}
            />
            <FormInput.Select
              name="type"
              items={getVaribaleTypeOptions(getString)}
              label={getString('typeLabel')}
              placeholder={getString('pipeline.variable.typePlaceholder')}
            />
            <div className="buttons-container">
              <Button
                variation={ButtonVariation.PRIMARY}
                text={getString('save')}
                onClick={submitForm}
                data-testid="addVariableSave"
              />{' '}
              &nbsp; &nbsp;
              <Button
                variation={ButtonVariation.TERTIARY}
                text={getString('cancel')}
                onClick={() => closeModal()}
                data-testid="addVariableCancel"
              />
            </div>
          </FormikForm>
        )}
      </Formik>
    </Dialog>
  )
}