All files / modules/72-triggers/pages/triggers/views WebhookTriggerConfigPanel.tsx

88.37% Statements 38/43
71.67% Branches 43/60
85.71% Functions 6/7
88.1% Lines 37/42

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 149 150              5x 5x 5x 5x 5x 5x 5x 5x 5x           5x             5x       82x           82x       82x 82x 82x 82x 82x   82x 11x 11x         11x 11x   11x 7x         82x 16x 28x 5x       82x 11x         82x 30x     23x   30x                     82x                                                                                                           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, { useState, useEffect } from 'react'
import cx from 'classnames'
import { Layout, FormInput, SelectOption, Text, HarnessDocTooltip, PageSpinner } from '@wings-software/uicore'
import { isEmpty } from 'lodash-es'
import { useGetGitTriggerEventDetails } from 'services/pipeline-ng'
import { NameIdDescriptionTags } from '@common/components'
import { useStrings } from 'framework/strings'
import { GitSourceProviders, getSourceRepoOptions } from '../utils/TriggersListUtils'
import {
  handleSourceRepoChange,
  renderNonCustomEventFields,
  getEventAndActions,
  clearEventsAndActions
} from '../utils/WebhookTriggerConfigPanelUtils'
import css from './WebhookTriggerConfigPanel.module.scss'
 
export interface WebhookTriggerConfigPanelPropsInterface {
  formikProps?: any
  isEdit?: boolean
}
 
const WebhookTriggerConfigPanel: React.FC<WebhookTriggerConfigPanelPropsInterface> = ({
  formikProps,
  isEdit = false
}) => {
  const { sourceRepo, identifier, actions, event, connectorRef } = formikProps.values
  const {
    data: eventDetailsResponse,
    refetch: refetchEventDetails,
    error: eventDetailsError,
    loading: loadingGetGitTriggerEventDetails
  } = useGetGitTriggerEventDetails({
    lazy: true
  })
 
  const [eventOptions, setEventOptions] = useState<SelectOption[]>([])
  const [actionsOptions, setActionsOptions] = useState<SelectOption[]>([])
  const [actionsOptionsMap, setActionsOptionsMap] = useState<{ [key: string]: string[] }>({})
  const { getString } = useStrings()
  const loading = false
 
  useEffect(() => {
    Eif (eventDetailsResponse?.data && !isEmpty(eventDetailsResponse.data) && sourceRepo && !eventDetailsError) {
      const { eventOptions: newEventOptions, actionsOptionsMap: newActionsOptionsMap } = getEventAndActions({
        data: eventDetailsResponse?.data,
        sourceRepo
      })
 
      setEventOptions(newEventOptions)
      setActionsOptionsMap(newActionsOptionsMap)
 
      if (event) {
        clearEventsAndActions({ newActionsOptionsMap, formikProps, setActionsOptions, event })
      }
    }
  }, [eventDetailsResponse?.data, sourceRepo])
 
  useEffect(() => {
    if (!isEmpty(actionsOptionsMap) && event) {
      const newActionsOptions = actionsOptionsMap[event]?.map(val => ({ label: val, value: val }))
      setActionsOptions(newActionsOptions)
    }
  }, [event, actions])
 
  useEffect(() => {
    Iif (sourceRepo !== GitSourceProviders.CUSTOM.value && !eventDetailsResponse && !loadingGetGitTriggerEventDetails) {
      refetchEventDetails()
    }
  }, [sourceRepo])
 
  useEffect(() => {
    if (event && identifier && connectorRef) {
      // handle lack of validating when value present
      // identifier/connector needed otherwise page crash when event clicked first
      formikProps.validateForm()
    }
    Iif (
      Object.keys(formikProps.errors)?.includes('connectorRef') && // check that this does not break for keys of
      !Object.keys(formikProps.touched)?.includes('connectorRef') &&
      formikProps.submitCount === 0
    ) {
      const newErrors = { ...formikProps.errors }
      delete newErrors.connectorRef
      formikProps.setErrors(newErrors)
    }
  }, [event, formikProps.errors])
 
  return (
    <Layout.Vertical className={css.webhookConfigurationContainer} padding="xxlarge">
      {loading && (
        <div style={{ position: 'relative', height: 'calc(100vh - 128px)' }}>
          <PageSpinner />
        </div>
      )}
      <Text className={css.formContentTitle} inline={true} data-tooltip-id="triggerConfigurationLabel">
        {getString('triggers.triggerConfigurationLabel')}
        {!isEdit ? `: ${getString('triggers.onNewWebhookTitle')}` : ''}
        <HarnessDocTooltip tooltipId="triggerConfigurationLabel" useStandAlone={true} />
      </Text>
 
      <div className={css.formContent}>
        <NameIdDescriptionTags
          className={cx(css.nameIdDescriptionTags, css.bottomMarginZero)}
          formikProps={formikProps}
          identifierProps={{
            isIdentifierEditable: !isEdit
          }}
          tooltipProps={{
            dataTooltipId: 'webhookTrigger'
          }}
        />
      </div>
      <Text className={css.formContentTitle} inline={true} data-tooltip-id="listenOnNewWebhook">
        {getString('triggers.triggerConfigurationPanel.listenOnNewWebhook')}
        <HarnessDocTooltip tooltipId="listenOnNewWebhook" useStandAlone={true} />
      </Text>
      <div className={css.formContent}>
        <section style={{ width: '650px' }}>
          <FormInput.Select
            label={getString('triggers.triggerConfigurationPanel.payloadType')}
            name="sourceRepo"
            className={cx(sourceRepo === GitSourceProviders.CUSTOM.value && css.bottomMarginZero)}
            items={getSourceRepoOptions(getString)}
            onChange={e => handleSourceRepoChange({ e, formikProps })}
          />
          {sourceRepo !== GitSourceProviders.CUSTOM.value
            ? renderNonCustomEventFields({
                sourceRepo,
                formikProps,
                event,
                eventOptions,
                getString,
                actionsOptions,
                actions
              })
            : null}
        </section>
      </div>
    </Layout.Vertical>
  )
}
export default WebhookTriggerConfigPanel