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

77.78% Statements 35/45
42.98% Branches 49/114
70% Functions 7/10
76.19% Lines 32/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 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172              18x 18x 18x   18x 18x   18x             18x                 18x         3x 3x 3x 3x 3x   3x 2x                           3x                   3x                 3x               3x 2x 1x 1x       1x                         1x                 3x 2x 2x 2x       3x 3x 3x   3x                                                                                                
/*
 * 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, useState } from 'react'
import { FormInput, Layout } from '@wings-software/uicore'
import { useParams } from 'react-router-dom'
import type { FormikProps } from 'formik'
import { useStrings } from 'framework/strings'
import { useToaster } from '@common/exports'
import type { ProjectPathProps } from '@common/interfaces/RouteInterfaces'
import {
  GetListApplicationsQueryParams,
  useGetListApplications,
  useGetListEnvironments,
  useGetListServices
} from 'services/portal'
import type { UpdatedChangeSourceDTO } from '../../ChangeSourceDrawer.types'
import {
  getApplicationsData,
  getEnvironmentsData,
  getHarnessCDFieldValue,
  getPlaceHolder,
  getServicesData,
  showToasterError
} from './HarnessCDCurrentGenChangeSource.utils'
 
export default function HarnessCDCurrentGenChangeSource({
  formik: { values: formValues, setFieldValue }
}: {
  formik: FormikProps<UpdatedChangeSourceDTO>
}): JSX.Element {
  const { getString } = useStrings()
  const { showError, clear } = useToaster()
  const { accountId } = useParams<ProjectPathProps & { identifier: string }>()
  const [showEnvAndServices, setShowEnvAndServices] = useState<boolean>(false)
  const [currentApplicationId, setCurrentApplicationId] = useState<string>('')
 
  const servicesAndEnvQueryParams = useMemo(() => {
    return {
      appId: !currentApplicationId
        ? formValues?.spec?.harnessApplicationId
        : formValues?.spec?.harnessApplicationId?.value,
      accountId,
      details: false
    }
  }, [accountId, currentApplicationId, formValues?.spec?.harnessApplicationId])
 
  const {
    data: environmentsData,
    loading: loadingEnvironments,
    error: errorEnvironments,
    refetch: refetchEnvironments
  } = useGetListEnvironments({
    queryParams: servicesAndEnvQueryParams,
    lazy: true
  })
 
  const {
    data: servicesData,
    loading: loadingServices,
    error: errorServices,
    refetch: refetchServices
  } = useGetListServices({
    queryParams: servicesAndEnvQueryParams,
    lazy: true
  })
 
  const {
    data: applicationsData,
    loading: applicationsLoading,
    error: applicationsError
  } = useGetListApplications({
    queryParams: {
      accountId,
      details: false
    } as GetListApplicationsQueryParams
  })
 
  // on selecting appId in the dropdown
  useEffect(() => {
    if (formValues?.spec?.harnessApplicationId) {
      Eif (!showEnvAndServices) {
        setShowEnvAndServices(true)
      }
 
      // if env/service is selected then it has to be reset on appId change from the dropdown
      Iif (currentApplicationId) {
        let updatedSpecs = { ...formValues?.spec }
 
        if (formValues?.spec?.harnessServiceId) {
          updatedSpecs = { ...updatedSpecs, harnessServiceId: '' }
        }
        if (formValues?.spec?.harnessEnvironmentId) {
          updatedSpecs = { ...updatedSpecs, harnessEnvironmentId: '' }
        }
        setFieldValue('spec', updatedSpecs)
      }
 
      // Fetching service and envs for selected appId
      Promise.all([
        refetchEnvironments({ queryParams: servicesAndEnvQueryParams }),
        refetchServices({ queryParams: servicesAndEnvQueryParams })
      ])
    }
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [formValues?.spec?.harnessApplicationId, currentApplicationId])
 
  // showing error in case of any api errors.
  useEffect(() => {
    showToasterError(errorEnvironments, clear, showError)
    showToasterError(errorServices, clear, showError)
    showToasterError(applicationsError, clear, showError)
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [errorEnvironments, errorServices, applicationsError])
 
  const applications = useMemo(() => getApplicationsData(applicationsData), [applicationsData])
  const services = useMemo(() => getServicesData(servicesData), [servicesData])
  const environments = useMemo(() => getEnvironmentsData(environmentsData), [environmentsData])
 
  return (
    <Layout.Horizontal spacing={'xxlarge'}>
      <FormInput.Select
        name="spec.harnessApplicationId"
        label={getString('cv.changeSource.HarnessCDCurrentGen.applicationId')}
        placeholder={getPlaceHolder(
          applicationsLoading,
          getString('cv.changeSource.HarnessCDCurrentGen.selectHarnessAppId'),
          getString('loading')
        )}
        items={applications}
        onChange={application => {
          setFieldValue('spec', { ...formValues?.spec, harnessApplicationId: application })
          setCurrentApplicationId(application.value as string)
        }}
        value={getHarnessCDFieldValue(currentApplicationId, applications, formValues?.spec?.harnessApplicationId)}
      />
      {showEnvAndServices ? (
        <>
          <FormInput.Select
            name="spec.harnessServiceId"
            label={getString('cv.harnessService')}
            placeholder={getPlaceHolder(
              loadingServices,
              getString('cv.changeSource.HarnessCDCurrentGen.selectHarnessService'),
              getString('loading')
            )}
            items={services}
            onChange={service => setFieldValue('spec', { ...formValues?.spec, harnessServiceId: service })}
            value={getHarnessCDFieldValue(currentApplicationId, services, formValues?.spec?.harnessServiceId)}
          />
          <FormInput.Select
            name="spec.harnessEnvironmentId"
            label={getString('cv.harnessEnvironment')}
            placeholder={getPlaceHolder(
              loadingEnvironments,
              getString('cv.changeSource.HarnessCDCurrentGen.selectHarnessEnv'),
              getString('loading')
            )}
            items={environments}
            onChange={environment => setFieldValue('spec', { ...formValues?.spec, harnessEnvironmentId: environment })}
            value={getHarnessCDFieldValue(currentApplicationId, environments, formValues?.spec?.harnessEnvironmentId)}
          />
        </>
      ) : null}
    </Layout.Horizontal>
  )
}