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

100% Statements 19/19
92.86% Branches 13/14
100% Functions 4/4
100% Lines 19/19

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              5x 5x   5x   5x 5x                 5x 13x   13x 13x 13x 13x 8x 8x 6x       13x   13x                 13x 13x           5x  
/*
 * 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 { debounce } from 'lodash-es'
import type { StageElementWrapperConfig } from 'services/cd-ng'
import DelegateSelectorPanel from '@pipeline/components/PipelineSteps/AdvancedSteps/DelegateSelectorPanel/DelegateSelectorPanel'
import { StageErrorContext } from '@pipeline/context/StageErrorContext'
export interface DelegateSelectorProps {
  selectedStage?: StageElementWrapperConfig
  isReadonly: boolean
  onUpdate(data: { delegateSelectors: string[] | string }): void
  tabName?: string
}
 
// wrapping delgate selector panel usingformik as MultuTypedelagate selector uses formik
export function DelegateSelector(props: DelegateSelectorProps): React.ReactElement {
  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])
 
  const selectedDelegateSelectors: string[] | string = selectedStage?.stage?.delegateSelectors || []
 
  return (
    <Formik
      initialValues={{
        delegateSelectors: selectedDelegateSelectors
      }}
      onSubmit={onUpdate}
      validate={debouncedUpdate}
    >
      {formik => {
        formikRef.current = formik
        return <DelegateSelectorPanel isReadonly={isReadonly} />
      }}
    </Formik>
  )
}
 
export const DelegateSelectorWithRef = React.forwardRef(DelegateSelector)