All files / modules/85-cv/components/HarnessServiceAndEnvironment HarnessServiceAndEnvironment.tsx

75.36% Statements 52/69
44.19% Branches 38/86
53.85% Functions 7/13
75.36% Lines 52/69

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 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182              13x 13x   13x 13x   13x 13x           13x 13x       13x       13x       13x   13x 31x 31x 31x 31x 31x               31x 7x   7x       7x 7x 7x 7x 7x           7x       31x     13x 31x 31x 31x 31x 31x               31x 7x   7x       7x 7x 7x 7x 7x           7x       31x     13x       13x       13x         36x   36x           36x       3x 3x               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, { useState, useEffect } from 'react'
import { FormInput, SelectOption } from '@wings-software/uicore'
import type { CustomRenderProps } from '@wings-software/uicore/dist/components/FormikForm/FormikForm'
import { useParams } from 'react-router-dom'
import cx from 'classnames'
import type { ProjectPathProps } from '@common/interfaces/RouteInterfaces'
import { useStrings } from 'framework/strings'
import {
  useGetServiceListForProject,
  GetServiceListForProjectQueryParams,
  useGetEnvironmentListForProject,
  GetEnvironmentListForProjectQueryParams
} from 'services/cd-ng'
import { useToaster } from '@common/exports'
import {
  ServiceSelectOrCreate,
  ServiceSelectOrCreateProps
} from './components/ServiceSelectOrCreate/ServiceSelectOrCreate'
import {
  EnvironmentSelectOrCreate,
  EnvironmentSelectOrCreateProps
} from './components/EnvironmentSelectOrCreate/EnvironmentSelectOrCreate'
import {
  EnvironmentMultiSelectOrCreate,
  EnvironmentMultiSelectOrCreateProps
} from './components/EnvironmentMultiSelectAndEnv/EnvironmentMultiSelectAndEnv'
import css from './HarnessServiceAndEnvironment.module.scss'
 
export function useGetHarnessServices() {
  const { accountId, projectIdentifier, orgIdentifier } = useParams<ProjectPathProps>()
  const { getString } = useStrings()
  const { showError, clear } = useToaster()
  const [serviceOptions, setServiceOptions] = useState<SelectOption[]>([])
  const { error, loading, data } = useGetServiceListForProject({
    queryParams: {
      accountId,
      orgIdentifier,
      projectIdentifier
    } as GetServiceListForProjectQueryParams
  })
 
  useEffect(() => {
    Iif (loading) {
      setServiceOptions([{ label: getString('loading'), value: '' }])
    } else Iif (error) {
      clear()
      showError(error?.message, 7000)
      setServiceOptions([])
    } else Eif (data?.data?.content) {
      const options = []
      for (const service of data.data.content) {
        Eif (service?.identifier && service.name) {
          options.push({
            label: service.name,
            value: service.identifier
          })
        }
      }
      setServiceOptions(options)
    }
  }, [loading, error, data])
 
  return { serviceOptions, setServiceOptions }
}
 
export function useGetHarnessEnvironments() {
  const { accountId, projectIdentifier, orgIdentifier } = useParams<ProjectPathProps>()
  const { getString } = useStrings()
  const { showError, clear } = useToaster()
  const [environmentOptions, setEnvironmentOptions] = useState<SelectOption[]>([])
  const { error, loading, data } = useGetEnvironmentListForProject({
    queryParams: {
      accountId,
      orgIdentifier,
      projectIdentifier
    } as GetEnvironmentListForProjectQueryParams
  })
 
  useEffect(() => {
    Iif (loading) {
      setEnvironmentOptions([{ label: getString('loading'), value: '' }])
    } else Iif (error) {
      clear()
      showError(error?.message, 7000)
      setEnvironmentOptions([])
    } else Eif (data?.data?.content) {
      const options = []
      for (const service of data.data.content) {
        Eif (service?.identifier && service.name) {
          options.push({
            label: service.name,
            value: service.identifier
          })
        }
      }
      setEnvironmentOptions(options)
    }
  }, [loading, error, data])
 
  return { environmentOptions, setEnvironmentOptions }
}
 
export function HarnessService(props: ServiceSelectOrCreateProps): JSX.Element {
  return <ServiceSelectOrCreate {...props} className={cx(css.serviceEnvDropdown, props.className)} />
}
 
export function HarnessEnvironment(props: EnvironmentSelectOrCreateProps): JSX.Element {
  return <EnvironmentSelectOrCreate {...props} className={cx(css.serviceEnvDropdown, props.className)} />
}
 
export function HarnessServiceAsFormField(props: {
  customRenderProps: Omit<CustomRenderProps, 'render'>
  serviceProps: ServiceSelectOrCreateProps
  customLoading?: boolean
}): JSX.Element {
  const { customRenderProps, serviceProps, customLoading } = props
 
  return (
    <FormInput.CustomRender
      {...customRenderProps}
      tooltipProps={{ dataTooltipId: 'serviceSelectOrCreate' }}
      key={typeof serviceProps.item === 'string' ? serviceProps.item : (serviceProps.item?.value as string)}
      render={formikProps => (
        <ServiceSelectOrCreate
          {...serviceProps}
          customLoading={customLoading}
          onSelect={selectedOption => {
            formikProps.setFieldValue(customRenderProps.name, selectedOption)
            serviceProps.onSelect?.(selectedOption)
          }}
        />
      )}
    />
  )
}
 
export function HarnessEnvironmentAsFormField(props: {
  customRenderProps: Omit<CustomRenderProps, 'render'>
  environmentProps: EnvironmentSelectOrCreateProps | EnvironmentMultiSelectOrCreateProps
  isMultiSelectField?: boolean
}): JSX.Element {
  const { customRenderProps, environmentProps, isMultiSelectField } = props
 
  return (
    <FormInput.CustomRender
      {...customRenderProps}
      tooltipProps={{ dataTooltipId: 'environmentSelectOrCreate' }}
      key={`${
        Array.isArray(environmentProps.item)
          ? (environmentProps.item?.[0]?.value as string)
          : (environmentProps.item?.value as string)
      }`}
      render={formikProps =>
        isMultiSelectField ? (
          <EnvironmentMultiSelectOrCreate
            {...(environmentProps as EnvironmentMultiSelectOrCreateProps)}
            onSelect={selectedOption => {
              formikProps.setFieldValue(customRenderProps.name, selectedOption)
              ;(environmentProps as EnvironmentMultiSelectOrCreateProps).onSelect?.(selectedOption)
            }}
          />
        ) : (
          <EnvironmentSelectOrCreate
            {...(environmentProps as EnvironmentSelectOrCreateProps)}
            onSelect={selectedOption => {
              formikProps.setFieldValue(customRenderProps.name, selectedOption)
              ;(environmentProps as EnvironmentSelectOrCreateProps).onSelect?.(selectedOption)
            }}
          />
        )
      }
    />
  )
}