All files / modules/72-templates-library/components/TemplateStudio/StepTemplateCanvas/StepTemplateForm StepTemplateForm.tsx

85.19% Statements 23/27
33.33% Branches 8/24
42.86% Functions 3/7
88.46% Lines 23/26

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              13x 13x 13x 13x 13x 13x         13x     13x 13x 13x   13x         2x 2x   2x                       2x 1x 1x 1x 1x       2x 1x     2x                                       13x  
/*
 * 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 { Container, MultiTypeInputType } from '@wings-software/uicore'
import { Color } from '@harness/design-system'
import { debounce, isEmpty, isEqual, set } from 'lodash-es'
import factory from '@pipeline/components/PipelineSteps/PipelineStepFactory'
import {
  StepCommandsWithRef as StepCommands,
  StepFormikRef
} from '@pipeline/components/PipelineStudio/StepCommands/StepCommands'
import type { StepElementConfig } from 'services/cd-ng'
import { StepViewType } from '@pipeline/components/AbstractSteps/Step'
import type { Values } from '@pipeline/components/PipelineStudio/StepCommands/StepCommandTypes'
import type { TemplateFormRef } from '@templates-library/components/TemplateStudio/TemplateStudio'
import { getStepDataFromValues } from '@pipeline/utils/stepUtils'
import { TemplateContext } from '../../TemplateContext/TemplateContext'
import css from './StepTemplateForm.module.scss'
 
const StepTemplateForm = (_props: unknown, formikRef: TemplateFormRef): JSX.Element => {
  const {
    state: { template },
    updateTemplate,
    isReadonly
  } = React.useContext(TemplateContext)
  const stepFormikRef = React.useRef<StepFormikRef | null>(null)
 
  React.useImperativeHandle(formikRef, () => ({
    resetForm() {
      return stepFormikRef.current?.resetForm()
    },
    submitForm() {
      return stepFormikRef.current?.submitForm()
    },
    getErrors() {
      return stepFormikRef.current?.getErrors() || {}
    }
  }))
 
  const onSubmitStep = async (item: Partial<Values>): Promise<void> => {
    const processNode = getStepDataFromValues(item, template.spec as StepElementConfig)
    Eif (!isEqual(template.spec, processNode)) {
      set(template, 'spec', processNode)
      await updateTemplate(template)
    }
  }
 
  const debounceSubmit = debounce((step: Partial<Values>): void => {
    onSubmitStep(step)
  }, 500)
 
  return (
    <Container background={Color.FORM_BG}>
      {template && !isEmpty(template.spec) && !!(template.spec as StepElementConfig)?.type && (
        <StepCommands
          className={css.stepForm}
          step={template.spec as StepElementConfig}
          isReadonly={isReadonly}
          stepsFactory={factory}
          onChange={debounceSubmit}
          onUpdate={debounceSubmit}
          isStepGroup={false}
          stepViewType={StepViewType.Template}
          ref={stepFormikRef}
          allowableTypes={[MultiTypeInputType.FIXED, MultiTypeInputType.RUNTIME, MultiTypeInputType.EXPRESSION]}
        />
      )}
    </Container>
  )
}
 
export const StepTemplateFormWithRef = React.forwardRef(StepTemplateForm)