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

93.18% Statements 41/44
65.38% Branches 34/52
100% Functions 7/7
93.02% Lines 40/43

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              17x 17x 17x   17x 17x 17x   17x 17x 17x   17x                   17x 17x 17x 17x 17x   17x               8x 8x   8x   2x 1x   2x 2x 2x       8x   8x   26x 26x 12x   14x   14x   10x 10x         4x 4x                                               14x               8x         10x       26x                                                 2x                                                                    
/*
 * 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, { useCallback, useMemo } from 'react'
import { Text, Formik, FormInput, Container, ThumbnailSelect } from '@wings-software/uicore'
import { Color } from '@harness/design-system'
import type { FormikProps } from 'formik'
import { useParams } from 'react-router-dom'
import cx from 'classnames'
import { FormConnectorReferenceField } from '@connectors/components/ConnectorReferenceField/FormConnectorReferenceField'
import type { ProjectPathProps } from '@common/interfaces/RouteInterfaces'
import { useStrings } from 'framework/strings'
import DrawerFooter from '@cv/pages/health-source/common/DrawerFooter/DrawerFooter'
import CardWithOuterTitle from '@cv/pages/health-source/common/CardWithOuterTitle/CardWithOuterTitle'
import type { ConnectorReferenceFieldProps } from '@connectors/components/ConnectorReferenceField/ConnectorReferenceField'
import {
  createCardOptions,
  createChangesourceList,
  validateChangeSource,
  getChangeSourceOptions,
  updateSpecByType,
  buildInitialData,
  preSelectChangeSourceConnectorOnCategoryChange
} from './ChangeSourceDrawer.utils'
import type { ChangeSoureDrawerInterface, UpdatedChangeSourceDTO } from './ChangeSourceDrawer.types'
import PageDutyChangeSource from './components/PagerDutyChangeSource/PagerDutyChangeSource'
import { ChangeSourceFieldNames, ChangeSourceTypes } from './ChangeSourceDrawer.constants'
import HarnessCDCurrentGenChangeSource from './components/HarnessCDCurrentGenChangeSource/HarnessCDCurrentGenChangeSource'
import KubernetesChangeSource from './components/KubernetesChangeSource/KubernetesChangeSource'
import style from './ChangeSourceDrawer.module.scss'
 
export function ChangeSourceDrawer({
  isEdit,
  rowdata,
  tableData,
  onSuccess,
  hideDrawer,
  monitoredServiceType
}: ChangeSoureDrawerInterface): JSX.Element {
  const { getString } = useStrings()
  const { orgIdentifier, projectIdentifier, accountId } = useParams<ProjectPathProps & { identifier: string }>()
 
  const onSuccessWrapper = (data: UpdatedChangeSourceDTO): void => {
    // for PagerDuty
    if (data.type === ChangeSourceTypes.PagerDuty) {
      data.enabled = true
    }
    data['spec'] = updateSpecByType(data)
    const updatedChangeSources = createChangesourceList(tableData, data)
    onSuccess(updatedChangeSources)
  }
 
  // eslint-disable-next-line react-hooks/exhaustive-deps
  const categoryOptions = useMemo(() => getChangeSourceOptions(getString, monitoredServiceType), [monitoredServiceType])
 
  const renderChangeSource = useCallback(
    (formik: FormikProps<any>): React.ReactNode => {
      const changeSourceType = formik.values?.type as string
      if (!changeSourceType || changeSourceType === ChangeSourceTypes.HarnessCDNextGen) {
        return null
      }
      let changeSource = null
 
      switch (changeSourceType) {
        case ChangeSourceTypes.PagerDuty:
          changeSource = <PageDutyChangeSource formik={formik} isEdit={isEdit} />
          break
        case ChangeSourceTypes.HarnessCD:
          changeSource = <HarnessCDCurrentGenChangeSource formik={formik} />
          break
        case ChangeSourceTypes.K8sCluster:
          changeSource = <KubernetesChangeSource formik={formik} isEdit={isEdit} />
          break
        default:
          changeSource = (
            <FormConnectorReferenceField
              width={400}
              formik={formik}
              type={changeSourceType as ConnectorReferenceFieldProps['type']}
              name={'spec.connectorRef'}
              disabled={isEdit}
              accountIdentifier={accountId}
              projectIdentifier={projectIdentifier}
              orgIdentifier={orgIdentifier}
              placeholder={getString('cv.healthSource.connectors.selectConnector', {
                sourceType: formik?.values?.type
              })}
              label={
                <Text color={Color.BLACK} font={'small'} margin={{ bottom: 'small' }}>
                  {getString('connectors.selectConnector')}
                </Text>
              }
            />
          )
      }
 
      return (
        <CardWithOuterTitle title={getString('cv.changeSource.connectChangeSource')}>{changeSource}</CardWithOuterTitle>
      )
    },
    // eslint-disable-next-line react-hooks/exhaustive-deps
    [accountId, orgIdentifier, projectIdentifier]
  )
 
  return (
    <Formik
      formName={'changeSourceaForm'}
      initialValues={rowdata?.category ? rowdata : buildInitialData(categoryOptions)}
      onSubmit={onSuccessWrapper}
      validate={values => validateChangeSource(values, tableData, isEdit, getString)}
      enableReinitialize
    >
      {formik => {
        return (
          <>
            <CardWithOuterTitle title={getString('cv.changeSource.defineChangeSource')} className={style.outerCard}>
              <Text className={style.selectChangeSource}>{getString('cv.changeSource.selectChangeSource')}</Text>
              <Container
                margin={{ bottom: 'large' }}
                className={cx({
                  [style.removeEditButton]: createCardOptions(formik.values?.category, getString).length === 1
                })}
                width="300px"
              >
                <div className={style.alignHorizontal}>
                  <Text
                    color={Color.BLACK}
                    font={'small'}
                    margin={{ bottom: 'small' }}
                    tooltipProps={{ dataTooltipId: 'changeSourceProviderType' }}
                  >
                    {getString('connectors.docker.dockerProvideType')}
                  </Text>
                  <FormInput.Select
                    name={ChangeSourceFieldNames.CATEGORY}
                    disabled={isEdit}
                    items={categoryOptions}
                    onChange={categoryName =>
                      formik.setValues({
                        [ChangeSourceFieldNames.CATEGORY]: categoryName?.value || ('' as string),
                        [ChangeSourceFieldNames.TYPE]: preSelectChangeSourceConnectorOnCategoryChange(
                          categoryName?.value as string
                        ),
                        spec: {}
                      })
                    }
                  />
                </div>
                {formik.values?.category && (
                  <ThumbnailSelect
                    isReadonly={isEdit}
                    name={ChangeSourceFieldNames.TYPE}
                    items={createCardOptions(formik.values?.category, getString)}
                  />
                )}
              </Container>
              <hr className={style.divider} />
              <Container margin={{ bottom: 'large', top: 'large' }} color={Color.BLACK} width="400px">
                <FormInput.InputWithIdentifier
                  inputLabel={getString('cv.changeSource.sourceName')}
                  isIdentifierEditable={!isEdit}
                />
              </Container>
            </CardWithOuterTitle>
            {renderChangeSource(formik)}
            <DrawerFooter isSubmit onPrevious={hideDrawer} onNext={formik.submitForm} />
          </>
        )
      }}
    </Formik>
  )
}