All files / modules/75-cf/pages/pipeline-studio/views/FeatureFlagStage/FeatureAddStageView FeatureAddStageView.tsx

32.43% Statements 12/37
0% Branches 0/60
0% Functions 0/8
32.35% Lines 11/34

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              2x 2x 2x   2x   2x 2x 2x 2x 2x 2x                           2x                                                                                                                                                                                                
/*
 * 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, { useCallback } from 'react'
import { Text, Container, Formik, FormikForm, Button } from '@wings-software/uicore'
import * as Yup from 'yup'
import type { FormikErrors } from 'formik'
import { debounce } from 'lodash-es'
import type { FeatureFlagStageElementConfig, StageElementWrapper } from '@pipeline/utils/pipelineTypes'
import { usePipelineContext } from '@pipeline/components/PipelineStudio/PipelineContext/PipelineContext'
import { useStrings } from 'framework/strings'
import { NameIdDescription } from '@common/components/NameIdDescriptionTags/NameIdDescriptionTags'
import { isDuplicateStageId } from '@pipeline/components/PipelineStudio/StageBuilder/StageBuilderUtil'
import { illegalIdentifiers, regexIdentifier } from '@common/utils/StringUtils'
import css from './FeatureAddStageView.module.scss'
 
export interface FeatureAddEditStageViewProps {
  data?: StageElementWrapper<FeatureFlagStageElementConfig>
  onSubmit?: (values: StageElementWrapper<FeatureFlagStageElementConfig>, identifier: string) => void
  onChange?: (values: Values) => void
}
 
interface Values {
  identifier: string
  name: string
  description?: string
}
 
export const FeatureAddEditStageView: React.FC<FeatureAddEditStageViewProps> = ({
  data,
  onSubmit,
  onChange
}): JSX.Element => {
  const { getString } = useStrings()
 
  const {
    state: { pipeline }
  } = usePipelineContext()
 
  const initialValues: Values = {
    identifier: data?.stage?.identifier || '',
    name: data?.stage?.name || '',
    description: data?.stage?.description
  }
 
  const validationSchema = () =>
    Yup.lazy((_values: Values): any =>
      Yup.object().shape({
        name: Yup.string()
          .trim()
          .required(getString('fieldRequired', { field: getString('stageNameLabel') })),
        identifier: Yup.string().when('name', {
          is: val => val?.length,
          then: Yup.string()
            .required(getString('validation.identifierRequired'))
            .matches(regexIdentifier, getString('validation.validIdRegex'))
            .notOneOf(illegalIdentifiers)
        })
      })
    )
 
  const handleValidate = (values: Values): FormikErrors<Values> => {
    const errors: { name?: string } = {}
    if (isDuplicateStageId(values.identifier, pipeline?.stages || [])) {
      // This always occurs
      // errors.name = getString('validation.identifierDuplicate')
    }
    if (data) {
      onChange?.(values)
    }
    return errors
  }
 
  // eslint-disable-next-line react-hooks/exhaustive-deps
  const handleSubmit = useCallback(
    debounce((values: Values): void => {
      if (data?.stage) {
        data.stage.identifier = values.identifier
        data.stage.name = values.name
        if (values.description) data.stage.description = values.description
        if (!data.stage.spec) data.stage.spec = {}
 
        onSubmit?.(data, values.identifier)
      }
    }, 300),
    [data, onSubmit]
  )
 
  return (
    <div className={css.stageCreate}>
      <Container padding="medium">
        <Formik
          enableReinitialize
          initialValues={initialValues}
          formName="featureAddStage"
          validationSchema={validationSchema}
          validate={handleValidate}
          onSubmit={handleSubmit}
        >
          {formikProps => (
            <FormikForm>
              <Text font={{ weight: 'bold' }} icon="cf-main" iconProps={{ size: 16 }} margin={{ bottom: 'medium' }}>
                {getString('pipelineSteps.build.create.aboutYourStage')}
              </Text>
              <NameIdDescription
                formikProps={formikProps}
                identifierProps={{
                  inputLabel: getString('stageNameLabel')
                }}
              />
              <Button
                type="submit"
                intent="primary"
                text={getString('pipelineSteps.build.create.setupStage')}
                onClick={() => formikProps.submitForm()}
                margin={{ top: 'small' }}
              />
            </FormikForm>
          )}
        </Formik>
      </Container>
    </div>
  )
}