All files / modules/85-cv/pages/ChangeSource/ChangeSourceDrawer/components/PagerDutyChangeSource PagerDutyChangeSource.tsx

92.31% Statements 24/26
76.19% Branches 80/105
100% Functions 4/4
92.31% Lines 24/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 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              18x 18x 18x 18x   18x 18x 18x 18x   18x   18x   18x             13x 13x 13x             13x       13x 6x 5x                       13x         13x   6x 4x       4x         13x                                                                                              
/*
 * 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, { useEffect, useMemo } from 'react'
import { Container, FormInput, Layout, SelectOption, Utils, Text } from '@wings-software/uicore'
import { useParams } from 'react-router-dom'
import { Color } from '@harness/design-system'
import type { FormikProps } from 'formik'
import { useStrings } from 'framework/strings'
import { useGetServicesFromPagerDuty } from 'services/cv'
import { useToaster } from '@common/exports'
import { getErrorMessage } from '@cv/utils/CommonUtils'
import type { ProjectPathProps } from '@common/interfaces/RouteInterfaces'
import { FormConnectorReferenceField } from '@connectors/components/ConnectorReferenceField/FormConnectorReferenceField'
import type { UpdatedChangeSourceDTO } from '../../ChangeSourceDrawer.types'
import style from './PagerDutyChangeSource.module.scss'
 
export default function PageDutyChangeSource({
  formik,
  isEdit
}: {
  formik: FormikProps<UpdatedChangeSourceDTO>
  isEdit?: boolean
}): JSX.Element {
  const { getString } = useStrings()
  const { showError, clear } = useToaster()
  const { orgIdentifier, projectIdentifier, accountId } = useParams<ProjectPathProps & { identifier: string }>()
 
  const {
    data: pagerdutyServices,
    error: pagerdutyServicesError,
    refetch: fetchPagerDutyServices,
    loading: loadingPagerdutyServices
  } = useGetServicesFromPagerDuty({
    lazy: true
  })
 
  useEffect(() => {
    if (formik?.values?.spec?.connectorRef) {
      fetchPagerDutyServices({
        queryParams: {
          orgIdentifier,
          projectIdentifier,
          accountId,
          connectorIdentifier: formik?.values?.spec?.connectorRef,
          requestGuid: Utils.randomId()
        }
      })
    }
  }, [formik?.values?.spec?.connectorRef])
 
  Iif (pagerdutyServicesError) {
    clear()
    showError(getErrorMessage(pagerdutyServicesError))
  }
 
  const pagerDutyServiceOptions = useMemo(
    () =>
      pagerdutyServices?.resource?.map(item => {
        const service: SelectOption = {
          label: item.name || '',
          value: item.id || ''
        }
        return service
      }) || [],
    [pagerdutyServices?.resource]
  )
 
  return (
    <Layout.Horizontal spacing={'xxlarge'}>
      <Container margin={{ bottom: 'large' }} width={'400px'}>
        <div className={style.connectorField}>
          <FormConnectorReferenceField
            width={400}
            formik={formik}
            disabled={isEdit}
            type={formik?.values?.type as any}
            name={'spec.connectorRef'}
            accountIdentifier={accountId}
            projectIdentifier={projectIdentifier}
            orgIdentifier={orgIdentifier}
            placeholder={getString('cv.healthSource.connectors.selectConnector', {
              sourceType: formik?.values?.type
            })}
            label={getString('connectors.selectConnector')}
            tooltipProps={{ dataTooltipId: 'selectPageDutyConnector' }}
          />
        </div>
      </Container>
      {formik?.values?.spec?.connectorRef && (
        <Container margin={{ bottom: 'large' }} width={'400px'}>
          <FormInput.Select
            disabled={isEdit}
            name="spec.pagerDutyServiceId"
            label={getString('cv.changeSource.PageDuty.pagerDutyService')}
            placeholder={
              loadingPagerdutyServices
                ? getString('loading')
                : getString('cv.changeSource.PageDuty.selectPagerDutyService')
            }
            tooltipProps={{ dataTooltipId: 'pagerDutyService' }}
            items={pagerDutyServiceOptions}
          />
          {!pagerDutyServiceOptions.length && !loadingPagerdutyServices && (
            <Text font={'xsmall'} color={Color.ERROR}>
              {getString('cv.changeSource.PageDuty.pagerDutyEmptyService', {
                connector: formik?.values?.spec?.connectorRef
              })}
            </Text>
          )}
        </Container>
      )}
    </Layout.Horizontal>
  )
}