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

66.07% Statements 37/56
44.44% Branches 12/27
46.15% Functions 6/13
66.07% Lines 37/56

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              11x 11x 11x 11x             11x 11x 11x 11x   11x 11x 11x 11x   11x 11x                         11x 31x 31x 31x 31x                   31x   31x                       31x                 31x 31x 2x     31x   2x                     31x   2x                                                   31x                                 31x                               31x 2x 2x 2x                     31x     31x                                        
/*
 * 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, useState } from 'react'
import { useParams } from 'react-router-dom'
import { useConfirmationDialog } from '@harness/uicore'
import {
  ChangeSourceDTO,
  HealthSource,
  MonitoredServiceDTO,
  MonitoredServiceResponse,
  useUpdateMonitoredService
} from 'services/cv'
import { useStrings } from 'framework/strings'
import { useDrawer } from '@cv/hooks/useDrawerHook/useDrawerHook'
import { useToaster } from '@common/exports'
import { getErrorMessage } from '@cv/utils/CommonUtils'
import type { ProjectPathProps } from '@common/interfaces/RouteInterfaces'
import { PageSpinner } from '@common/components'
import HealthSourceTable from './HealthSourceTable'
import HealthSourceDrawerHeader from '../HealthSourceDrawer/component/HealthSourceDrawerHeader/HealthSourceDrawerHeader'
import HealthSourceDrawerContent from '../HealthSourceDrawer/HealthSourceDrawerContent'
import type { RowData } from '../HealthSourceDrawer/HealthSourceDrawerContent.types'
import { createHealthsourceList } from './HealthSourceTable.utils'
import { deleteHealthSourceVerifyStep } from './VerifyStepHealthSourceTable.utils'
 
interface VerifyStepHealthSourceTableInterface {
  serviceIdentifier: string
  envIdentifier: string
  healthSourcesList: RowData[]
  monitoredServiceRef: { identifier: string; name: string }
  monitoredServiceData?: MonitoredServiceDTO
  onSuccess: (data: any) => void
  isRunTimeInput: boolean
  changeSourcesList: ChangeSourceDTO[]
}
 
export default function VerifyStepHealthSourceTable(tableProps: VerifyStepHealthSourceTableInterface): JSX.Element {
  const { getString } = useStrings()
  const { accountId } = useParams<ProjectPathProps & { identifier: string }>()
  const { showError, showSuccess } = useToaster()
  const [rowToDelete, setRowToDelete] = useState<HealthSource>()
  const {
    serviceIdentifier,
    envIdentifier,
    healthSourcesList,
    changeSourcesList,
    monitoredServiceRef,
    monitoredServiceData,
    isRunTimeInput,
    onSuccess
  } = tableProps
 
  const { openDialog } = useConfirmationDialog({
    titleText: getString('cv.healthSource.deleteHealthSource'),
    contentText: getString('cv.healthSource.deleteHealthSourceWarning') + `: ${rowToDelete?.identifier}`,
    confirmButtonText: getString('delete'),
    cancelButtonText: getString('cancel'),
    onCloseDialog: function (shouldDelete: boolean) {
      if (shouldDelete) {
        handleOnDeleteHealthSource()
      }
    }
  })
 
  const { mutate: updateMonitoredService, loading } = useUpdateMonitoredService({
    identifier: monitoredServiceRef?.identifier,
    queryParams: { accountId: accountId }
  })
 
  const {
    showDrawer: showHealthSourceDrawer,
    hideDrawer: hideHealthSourceDrawer,
    setDrawerHeaderProps
  } = useDrawer({
    createHeader: props => <HealthSourceDrawerHeader {...props} />,
    createDrawerContent: props => <HealthSourceDrawerContent {...props} />
  })
 
  const healthSourceDrawerHeaderProps = useCallback(
    (isEdit = false) => {
      return {
        isEdit,
        shouldRenderAtVerifyStep: true,
        onClick: () => hideHealthSourceDrawer(),
        breadCrumbRoute: { routeTitle: getString('connectors.cdng.runTimeMonitoredService.backToRunPipeline') }
      }
    },
    // eslint-disable-next-line react-hooks/exhaustive-deps
    [serviceIdentifier, envIdentifier, healthSourcesList, monitoredServiceRef, isRunTimeInput]
  )
 
  const getHealthSourceDrawerProps = useCallback(
    (values?: any) => {
      return {
        isRunTimeInput,
        shouldRenderAtVerifyStep: true,
        serviceRef: serviceIdentifier,
        environmentRef: envIdentifier,
        monitoredServiceRef: monitoredServiceRef,
        rowData: values,
        tableData: values ? createHealthsourceList(healthSourcesList, values) : healthSourcesList,
        changeSources: changeSourcesList,
        onSuccess: (data: MonitoredServiceResponse) => {
          onSuccess(data)
          hideHealthSourceDrawer()
        }
      }
    },
    // eslint-disable-next-line react-hooks/exhaustive-deps
    [
      serviceIdentifier,
      envIdentifier,
      healthSourcesList,
      monitoredServiceRef.name,
      monitoredServiceRef.identifier,
      isRunTimeInput
    ]
  )
 
  const onEdit = useCallback(
    values => {
      const drawerProps = getHealthSourceDrawerProps(values)
      showHealthSourceDrawer(drawerProps)
      setDrawerHeaderProps?.(healthSourceDrawerHeaderProps(true))
    },
    // eslint-disable-next-line react-hooks/exhaustive-deps
    [
      serviceIdentifier,
      envIdentifier,
      healthSourcesList,
      monitoredServiceRef.identifier,
      monitoredServiceRef.name,
      isRunTimeInput
    ]
  )
 
  const handleOnDeleteHealthSource = useCallback(async () => {
    const updatedMonitoredServicePayload = deleteHealthSourceVerifyStep(
      healthSourcesList,
      monitoredServiceData as MonitoredServiceDTO,
      rowToDelete
    )
    try {
      const updatedMonitoredService = await updateMonitoredService(updatedMonitoredServicePayload)
      onSuccess(updatedMonitoredService?.resource)
      showSuccess(getString('cv.monitoredServices.monitoredServiceUpdated'))
    } catch (error) {
      showError(getErrorMessage(error))
    }
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [healthSourcesList, monitoredServiceData, rowToDelete])
 
  const onAddNewHealthSource = useCallback(() => {
    const drawerProps = getHealthSourceDrawerProps()
    showHealthSourceDrawer(drawerProps)
    setDrawerHeaderProps?.(healthSourceDrawerHeaderProps())
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [
    serviceIdentifier,
    envIdentifier,
    healthSourcesList,
    monitoredServiceRef.identifier,
    monitoredServiceRef.name,
    isRunTimeInput
  ])
 
  Iif (loading) {
    return <PageSpinner />
  }
  return (
    <>
      <HealthSourceTable
        onEdit={onEdit}
        onDeleteHealthSourceVerifyStep={selectedRow => {
          setRowToDelete(selectedRow)
          openDialog()
        }}
        isRunTimeInput={isRunTimeInput}
        onAddNewHealthSource={onAddNewHealthSource}
        value={healthSourcesList as HealthSource[]}
        shouldRenderAtVerifyStep
        onSuccess={(data: any) => {
          onSuccess(data)
          hideHealthSourceDrawer()
        }}
      />
    </>
  )
}