All files / modules/70-pipeline/components/PipelineStudio/FailureStrategy FailureStrategy.tsx

69.05% Statements 29/42
52.94% Branches 18/34
54.55% Functions 6/11
68.29% Lines 28/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              7x 7x 7x 7x   7x 7x   7x 7x     7x                   7x 5x 5x   5x 5x 5x   5x 5x 5x 4x       5x                                 2x 2x                                         5x   5x                       13x 13x 13x               7x  
/*
 * 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 { Formik, FormikProps } from 'formik'
import * as Yup from 'yup'
import { debounce } from 'lodash-es'
import type { StageElementWrapperConfig } from 'services/cd-ng'
import { useStrings } from 'framework/strings'
import FailureStrategyPanel from '@pipeline/components/PipelineSteps/AdvancedSteps/FailureStrategyPanel/FailureStrategyPanel'
import type { AllFailureStrategyConfig } from '@pipeline/components/PipelineSteps/AdvancedSteps/FailureStrategyPanel/utils'
import { getFailureStrategiesValidationSchema } from '@pipeline/components/PipelineSteps/AdvancedSteps/FailureStrategyPanel/validation'
import { StepMode as Modes } from '@pipeline/utils/stepUtils'
import type { StageType } from '@pipeline/utils/stageHelpers'
 
import { StageErrorContext } from '@pipeline/context/StageErrorContext'
import type { StepCommandsRef } from '../StepCommands/StepCommands'
 
export interface FailureStrategyProps {
  selectedStage?: StageElementWrapperConfig
  isReadonly: boolean
  onUpdate(data: { failureStrategies: AllFailureStrategyConfig[] }): void
  tabName?: string
}
 
export function FailureStrategy(props: FailureStrategyProps, ref: StepCommandsRef): React.ReactElement {
  const { getString } = useStrings()
  const { selectedStage, onUpdate, isReadonly, tabName } = props
  // eslint-disable-next-line react-hooks/exhaustive-deps
  const debouncedUpdate = React.useCallback(debounce(onUpdate, 300), [onUpdate])
  const formikRef = React.useRef<FormikProps<unknown> | null>(null)
  const { subscribeForm, unSubscribeForm } = React.useContext(StageErrorContext)
 
  React.useEffect(() => {
    !!tabName && subscribeForm({ tab: tabName, form: formikRef })
    return () => {
      !!tabName && unSubscribeForm({ tab: tabName, form: formikRef })
    }
  }, [subscribeForm, unSubscribeForm, tabName])
 
  React.useImperativeHandle(ref, () => ({
    setFieldError(key: string, error: string) {
      if (formikRef.current) {
        formikRef.current.setFieldError(key, error)
      }
    },
    isDirty() {
      if (formikRef.current) {
        return formikRef.current.dirty
      }
    },
    submitForm() {
      if (formikRef.current) {
        return formikRef.current.submitForm()
      }
    },
    getErrors() {
      Eif (formikRef.current) {
        return formikRef.current.errors
      }
 
      return {}
    },
    getValues() {
      if (formikRef.current) {
        return formikRef.current.values
      }
 
      return {}
    },
    resetForm() {
      if (formikRef.current) {
        return formikRef.current.resetForm()
      }
 
      return {}
    }
  }))
 
  const stageType = selectedStage?.stage?.type as StageType
 
  return (
    <Formik
      initialValues={{
        failureStrategies: selectedStage?.stage?.failureStrategies as AllFailureStrategyConfig[]
      }}
      validationSchema={Yup.object().shape({
        failureStrategies: getFailureStrategiesValidationSchema(getString)
      })}
      onSubmit={onUpdate}
      validate={debouncedUpdate}
    >
      {formik => {
        !!tabName && window.dispatchEvent(new CustomEvent('UPDATE_ERRORS_STRIP', { detail: tabName }))
        formikRef.current = formik
        return (
          <FailureStrategyPanel isReadonly={isReadonly} mode={Modes.STAGE} stageType={stageType} formikProps={formik} />
        )
      }}
    </Formik>
  )
}
 
export const FailureStrategyWithRef = React.forwardRef(FailureStrategy)