All files / modules/85-cv/pages/health-source/HealthSourceTable HealthSourceTable.tsx

90.2% Statements 46/51
66.67% Branches 28/42
76.92% Functions 10/13
90% Lines 45/50

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 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199              21x 21x   21x 21x 21x     21x 21x 21x 21x 21x 21x   21x 21x 21x   21x                                 75x 75x 75x   75x   75x                     75x 20x 20x                 1x 1x                                             75x 20x 20x     75x 1x 1x 1x           75x   43x 9x         4x 4x   4x 4x                                                 34x                 75x   75x 32x                   43x                                                       75x    
/*
 * 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 } from 'react'
import { cloneDeep } from 'lodash-es'
import type { CellProps, Renderer } from 'react-table'
import { useParams } from 'react-router-dom'
import { Container, Icon, Layout, Text, NoDataCard, ButtonVariation, TableV2 } from '@wings-software/uicore'
import { useToaster } from '@common/exports'
import type { ProjectPathProps } from '@common/interfaces/RouteInterfaces'
import type { HealthSource, HealthSourceDTO } from 'services/cv'
import { useStrings } from 'framework/strings'
import { ResourceType } from '@rbac/interfaces/ResourceType'
import { PermissionIdentifier } from '@rbac/interfaces/PermissionIdentifier'
import RbacButton from '@rbac/components/Button/Button'
import ContextMenuActions from '@cv/components/ContextMenuActions/ContextMenuActions'
import HealthSources from '@cv/components/PipelineSteps/ContinousVerification/components/HealthSources/HealthSources'
import type { RowData } from '../HealthSourceDrawer/HealthSourceDrawerContent.types'
import { getIconBySourceType, getTypeByFeature } from './HealthSourceTable.utils'
import CardWithOuterTitle from '../common/CardWithOuterTitle/CardWithOuterTitle'
import css from './HealthSourceTable.module.scss'
 
export default function HealthSourceTable({
  value,
  onEdit,
  onDeleteHealthSourceVerifyStep,
  onSuccess,
  onAddNewHealthSource,
  shouldRenderAtVerifyStep,
  isRunTimeInput
}: {
  value: any
  onSuccess: (data: HealthSourceDTO[]) => void
  onDeleteHealthSourceVerifyStep?: (selectedRow: HealthSource) => void
  onEdit: (data: HealthSource) => void
  onAddNewHealthSource: () => void
  shouldRenderAtVerifyStep?: boolean
  isRunTimeInput?: boolean
}): JSX.Element {
  const tableData = cloneDeep(value)
  const { showError } = useToaster()
  const { getString } = useStrings()
 
  const { projectIdentifier } = useParams<ProjectPathProps>()
 
  const onDeleteHealthSource = useCallback(
    async (selectedRow: HealthSource): Promise<void> => {
      const updatedHealthSources = tableData?.filter(
        (healthSource: HealthSource) => healthSource.identifier !== selectedRow.identifier
      )
      onSuccess(updatedHealthSources)
    },
    // eslint-disable-next-line react-hooks/exhaustive-deps
    [tableData]
  )
 
  const renderTypeWithIcon: Renderer<CellProps<RowData>> = ({ row }): JSX.Element => {
    const rowdata = row?.original
    return (
      <Layout.Horizontal flex={{ justifyContent: 'space-between' }}>
        <Icon className={css.sourceTypeIcon} name={getIconBySourceType(rowdata?.type as string)} size={22} />
        <ContextMenuActions
          titleText={getString('cv.healthSource.deleteHealthSource')}
          contentText={getString('cv.healthSource.deleteHealthSourceWarning') + `: ${rowdata.identifier}`}
          onDelete={() => onDeleteHealthSource(rowdata)}
          onEdit={() => {
            const rowFilteredData =
              tableData?.find((healthSource: RowData) => healthSource.identifier === rowdata.identifier) || null
            onEdit(rowFilteredData)
          }}
          RbacPermissions={{
            edit: {
              permission: PermissionIdentifier.EDIT_MONITORED_SERVICE,
              resource: {
                resourceType: ResourceType.MONITOREDSERVICE,
                resourceIdentifier: projectIdentifier
              }
            },
            delete: {
              permission: PermissionIdentifier.DELETE_MONITORED_SERVICE,
              resource: {
                resourceType: ResourceType.MONITOREDSERVICE,
                resourceIdentifier: projectIdentifier
              }
            }
          }}
        />
      </Layout.Horizontal>
    )
  }
 
  const renderTypeByFeature: Renderer<CellProps<RowData>> = ({ row }): JSX.Element => {
    const rowdata = row?.original
    return <Text>{getTypeByFeature(rowdata?.type as string, getString)}</Text>
  }
 
  const editRow = useCallback(rowToEdit => {
    Eif (rowToEdit) {
      onEdit(rowToEdit)
      return
    }
    showError(getString('cv.healthSource.noDataPresentHealthSource'))
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [])
 
  const renderHealthSourceTableInCV = useCallback(
    (healthSourceTableData: RowData[]) => {
      if (healthSourceTableData?.length) {
        return (
          <TableV2
            className={css.healthSourceTableWrapper}
            sortable={true}
            onRowClick={data => {
              const rowFilteredData = healthSourceTableData?.find(
                (healthSource: RowData) => healthSource.identifier === data.identifier
              )
              Eif (rowFilteredData) {
                onEdit(rowFilteredData)
              }
            }}
            columns={[
              {
                Header: getString('name'),
                accessor: 'name',
                width: '30%'
              },
              {
                Header: getString('typeLabel'),
                width: '35%',
                Cell: renderTypeByFeature
              },
              {
                Header: getString('source'),
                accessor: 'type',
                width: '35%',
                Cell: renderTypeWithIcon
              }
            ]}
            data={healthSourceTableData}
          />
        )
      }
      return (
        <Container className={css.noData}>
          <NoDataCard icon={'join-table'} message={getString('cv.healthSource.noData')} />
        </Container>
      )
    },
    [value, onAddNewHealthSource, editRow, onEdit]
  )
 
  const renderHealthSourceTable = useCallback(
    (renderAtVerifyStep: boolean, healthSourceTableData: RowData[]) => {
      if (renderAtVerifyStep) {
        return (
          <HealthSources
            healthSources={healthSourceTableData}
            editHealthSource={editRow}
            deleteHealthSource={onDeleteHealthSourceVerifyStep}
            isRunTimeInput={isRunTimeInput}
            addHealthSource={onAddNewHealthSource}
          />
        )
      }
      return (
        <CardWithOuterTitle>
          <Text tooltipProps={{ dataTooltipId: 'healthSourcesLabel' }} className={css.tableTitle}>
            {getString('connectors.cdng.healthSources.label')}
          </Text>
          {renderHealthSourceTableInCV(healthSourceTableData)}
          <RbacButton
            icon="plus"
            text={getString('cv.healthSource.addHealthSource')}
            variation={ButtonVariation.LINK}
            onClick={onAddNewHealthSource}
            data-testid="addHealthSource-button"
            margin={{ top: 'small' }}
            permission={{
              permission: PermissionIdentifier.EDIT_MONITORED_SERVICE,
              resource: {
                resourceType: ResourceType.MONITOREDSERVICE,
                resourceIdentifier: projectIdentifier
              }
            }}
          />
        </CardWithOuterTitle>
      )
    },
    // eslint-disable-next-line react-hooks/exhaustive-deps
    [isRunTimeInput, value, onAddNewHealthSource, editRow, onEdit]
  )
 
  return renderHealthSourceTable(!!shouldRenderAtVerifyStep, tableData)
}