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

92.86% Statements 26/28
71.88% Branches 23/32
100% Functions 8/8
92.86% Lines 26/28

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              29x 29x 29x 29x 29x               29x                                   79x 77x   77x 14x 14x 14x     14x   14x   14x       14x                     77x 29x 29x     77x   1x                       77x   1x                         77x                         1x   1x                
/*
 * Copyright 2022 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, useCallback } from 'react'
import { SetupSourceLayout } from '@cv/components/CVSetupSourcesView/SetupSourceLayout/SetupSourceLayout'
import { MultiItemsSideNav } from '@cv/components/MultiItemsSideNav/MultiItemsSideNav'
import { useStrings } from 'framework/strings'
import {
  onRemoveMetric,
  onSelectMetric,
  getGroupedCreatedMetrics,
  updateSelectedMetricsMap
} from './CustomMetric.utils'
import type { CustomMetricInterface } from './CustomMetric.types'
 
export default function CustomMetric(props: CustomMetricInterface): JSX.Element {
  const {
    children,
    formikValues,
    defaultMetricName,
    tooptipMessage,
    addFieldLabel,
    createdMetrics,
    isValidInput,
    mappedMetrics,
    selectedMetric,
    groupedCreatedMetrics,
    setMappedMetrics,
    setCreatedMetrics,
    setGroupedCreatedMetrics,
    initCustomForm,
    isPrimaryMetric,
    shouldBeAbleToDeleteLastMetric
  } = props
  const { getString } = useStrings()
 
  useEffect(() => {
    setMappedMetrics(oldState => {
      const emptyName = formikValues.metricName?.length
      Iif (!emptyName) {
        return { selectedMetric: oldState.selectedMetric, mappedMetrics: mappedMetrics }
      }
      const metricName = formikValues.metricName || ''
      const duplicateName =
        Array.from(mappedMetrics.keys()).indexOf(metricName) > -1 &&
        oldState.selectedMetric !== formikValues?.metricName
      Iif (duplicateName) {
        return { selectedMetric: oldState.selectedMetric, mappedMetrics: mappedMetrics }
      }
 
      return updateSelectedMetricsMap({
        updatedMetric: metricName,
        oldMetric: oldState.selectedMetric,
        mappedMetrics: oldState.mappedMetrics,
        formikValues,
        initCustomForm,
        isPrimaryMetric
      })
    })
  }, [formikValues?.groupName, formikValues?.metricName])
 
  useEffect(() => {
    const updatedGroupedCreatedMetrics = getGroupedCreatedMetrics(mappedMetrics, getString)
    setGroupedCreatedMetrics(updatedGroupedCreatedMetrics)
  }, [formikValues?.groupName, mappedMetrics, selectedMetric])
 
  const removeMetric = useCallback(
    (removedMetric, updatedMetric, updatedList, smIndex) =>
      onRemoveMetric({
        removedMetric,
        updatedMetric,
        updatedList,
        smIndex,
        formikValues,
        setCreatedMetrics,
        setMappedMetrics
      }),
    [formikValues]
  )
 
  const selectMetric = useCallback(
    (newMetric, updatedList, smIndex) =>
      onSelectMetric({
        newMetric,
        updatedList,
        smIndex,
        setCreatedMetrics,
        setMappedMetrics,
        formikValues,
        initCustomForm,
        isPrimaryMetric
      }),
    [formikValues]
  )
 
  return (
    <SetupSourceLayout
      leftPanelContent={
        <MultiItemsSideNav
          defaultMetricName={defaultMetricName}
          tooptipMessage={tooptipMessage}
          addFieldLabel={addFieldLabel}
          createdMetrics={createdMetrics}
          defaultSelectedMetric={selectedMetric}
          renamedMetric={formikValues?.metricName}
          isValidInput={isValidInput}
          groupedCreatedMetrics={groupedCreatedMetrics}
          onRemoveMetric={(removedMetric, updatedMetric, updatedList, smIndex) =>
            removeMetric(removedMetric, updatedMetric, updatedList, smIndex)
          }
          onSelectMetric={(newMetric, updatedList, smIndex) => selectMetric(newMetric, updatedList, smIndex)}
          shouldBeAbleToDeleteLastMetric={shouldBeAbleToDeleteLastMetric}
        />
      }
      content={children}
    />
  )
}