All files / modules/70-pipeline/components/CommonPipelineStages/ApprovalStage ApprovalStageMinimalMode.tsx

97.62% Statements 41/42
80.99% Branches 98/121
100% Functions 6/6
97.56% Lines 40/41

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              1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x   1x 1x   4x               1x 4x 4x         4x   4x 22x 22x     22x 22x   22x     4x 4x 4x 1x   3x 3x 3x 3x 3x 2x   3x         4x                         4x   28x                                                                                                                            
/*
 * 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 * as Yup from 'yup'
import { Formik } from 'formik'
import { Button, Container, FormikForm, Text } from '@wings-software/uicore'
import { Color, Intent } from '@harness/design-system'
import { usePipelineContext } from '@pipeline/components/PipelineStudio/PipelineContext/PipelineContext'
import { isDuplicateStageId } from '@pipeline/components/PipelineStudio/StageBuilder/StageBuilderUtil'
import { useStrings } from 'framework/strings'
import { NameIdDescriptionTags } from '@common/components'
import type { ApprovalStageElementConfig, StageElementWrapper } from '@pipeline/utils/pipelineTypes'
import { getNameAndIdentifierSchema } from '@pipeline/utils/tempates'
import { createTemplate, getTemplateNameWithLabel } from '@pipeline/utils/templateUtils'
import { NameId } from '@common/components/NameIdDescriptionTags/NameIdDescriptionTags'
import { isContextTypeNotStageTemplate } from '@pipeline/components/PipelineStudio/PipelineUtils'
import type { ApprovalStageMinimalModeProps, ApprovalStageMinimalValues } from './types'
import { ApprovalTypeCards } from './ApprovalTypeCards'
import css from './ApprovalStageMinimalMode.module.scss'
 
const getInitialValues = (data?: StageElementWrapper<ApprovalStageElementConfig>): ApprovalStageMinimalValues => ({
  identifier: data?.stage?.identifier || '',
  name: data?.stage?.name || '',
  description: data?.stage?.description,
  tags: data?.stage?.tags || {},
  approvalType: (data?.stage?.spec as any)?.approvalType
})
 
export function ApprovalStageMinimalMode(props: ApprovalStageMinimalModeProps): React.ReactElement {
  const { getString } = useStrings()
  const { onChange, onSubmit, data, template } = props
 
  const {
    state: { pipeline },
    contextType
  } = usePipelineContext()
 
  const handleValidate = (values: ApprovalStageMinimalValues): Record<string, string | undefined> | undefined => {
    const errors: { name?: string } = {}
    Iif (isDuplicateStageId(values.identifier, pipeline?.stages || [])) {
      errors.name = getString('validation.identifierDuplicate')
    }
    Eif (data) {
      onChange?.(values)
    }
    return errors
  }
 
  const handleSubmit = (values: ApprovalStageMinimalValues): void => {
    Eif (data?.stage) {
      if (template) {
        onSubmit?.({ stage: createTemplate(values, template) }, values.identifier)
      } else {
        data.stage.identifier = values.identifier
        data.stage.name = values.name
        data.stage.description = values.description
        data.stage.tags = values.tags
        if (!data.stage.spec?.execution) {
          ;(data.stage as any).approvalType = values.approvalType
        }
        onSubmit?.(data, values.identifier)
      }
    }
  }
 
  return (
    <Container padding="medium" className={css.approvalStageMinimalWrapper}>
      <Formik
        enableReinitialize
        initialValues={getInitialValues(data)}
        validationSchema={Yup.object().shape({
          ...getNameAndIdentifierSchema(getString, contextType),
          ...(!template &&
            !data?.stage?.spec?.execution && {
              approvalType: Yup.string().required(getString('pipeline.approvalTypeRequired'))
            })
        })}
        validate={handleValidate}
        onSubmit={(values: ApprovalStageMinimalValues) => handleSubmit(values)}
      >
        {formikProps => (
          <FormikForm>
            <Text
              icon="pipeline-approval"
              iconProps={{ size: 16, intent: Intent.SUCCESS }}
              margin={{ bottom: 'medium' }}
              className={css.addStageHeading}
            >
              {getString('pipelineSteps.build.create.aboutYourStage')}
            </Text>
 
            {isContextTypeNotStageTemplate(contextType) &&
              (template ? (
                <NameId
                  identifierProps={{
                    inputLabel: getString('stageNameLabel')
                  }}
                />
              ) : (
                <NameIdDescriptionTags
                  formikProps={formikProps}
                  identifierProps={{
                    inputLabel: getString('stageNameLabel')
                  }}
                />
              ))}
 
            {template ? (
              <Text
                icon={'template-library'}
                margin={{ top: 'medium', bottom: 'medium' }}
                font={{ size: 'small' }}
                iconProps={{ size: 12, margin: { right: 'xsmall' } }}
                color={Color.BLACK}
              >
                {`Using Template: ${getTemplateNameWithLabel(template)}`}
              </Text>
            ) : !data?.stage?.spec?.execution ? (
              <>
                <Text
                  color={Color.GREY_700}
                  font={{ size: 'normal', weight: 'semi-bold' }}
                  tooltipProps={{ dataTooltipId: 'approvalTypeHeading' }}
                >
                  {getString('approvalStage.approvalTypeHeading')}
                </Text>
                <ApprovalTypeCards formikProps={formikProps} />
              </>
            ) : null}
 
            <Button
              type="submit"
              intent="primary"
              text={getString('pipelineSteps.build.create.setupStage')}
              margin={{ top: 'small' }}
            />
          </FormikForm>
        )}
      </Formik>
    </Container>
  )
}