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

0% Statements 0/26
0% Branches 0/35
0% Functions 0/7
0% Lines 0/23

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                                                                                                                                                                                     
/*
 * 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 } from 'react'
import type { FormikContext } from 'formik'
import { useParams } from 'react-router-dom'
import { isEmpty } from 'lodash-es'
import { Container, FormInput, Icon, PageError } from '@wings-software/uicore'
import type { ProjectPathProps } from '@common/interfaces/RouteInterfaces'
import { getErrorMessage } from '@cv/utils/CommonUtils'
import { useGetMetricPacks, GetMetricPacksQueryParams, MetricPackDTO } from 'services/cv'
import css from './MonitoredServiceConnector.module.scss'
 
export default function MetricPack({
  formik,
  connector,
  value,
  onChange,
  setSelectedMetricPacks
}: {
  formik: FormikContext<any>
  connector: GetMetricPacksQueryParams['dataSourceType']
  value: MetricPackDTO[] | undefined
  onChange: (data: { [key: string]: boolean }) => void
  setSelectedMetricPacks: React.Dispatch<React.SetStateAction<MetricPackDTO[]>>
}): JSX.Element {
  const { accountId, orgIdentifier, projectIdentifier } = useParams<ProjectPathProps>()
  const {
    data: metricPacks,
    refetch: refetchMetricPacks,
    error: metricPackError,
    loading
  } = useGetMetricPacks({
    queryParams: {
      accountId,
      orgIdentifier,
      projectIdentifier,
      dataSourceType: connector
    }
  })
 
  useEffect(() => {
    if (metricPacks) {
      const metricData: { [key: string]: boolean } = {}
      const metricList: MetricPackDTO[] = value || metricPacks?.resource || []
      metricList.forEach((i: MetricPackDTO) => (metricData[i.identifier as string] = true))
      if (!isEmpty(metricData)) formik.setFieldValue('metricData', metricData)
      if (metricPacks?.resource) setSelectedMetricPacks(metricPacks?.resource)
    }
  }, [metricPacks])
 
  return (
    <FormInput.CustomRender
      name={'metricData'}
      render={() => {
        return loading ? (
          <Icon name="steps-spinner" />
        ) : (
          <>
            <Container className={css.metricPack}>
              {metricPacks?.resource?.map((metricPack: MetricPackDTO) => {
                return (
                  <FormInput.CheckBox
                    name={`metricData.${metricPack.identifier}`}
                    key={metricPack.identifier}
                    label={metricPack.identifier || ''}
                    onChange={async val => {
                      const metricValue: { [key: string]: boolean } = {
                        ...formik.values.metricData,
                        [metricPack.identifier as string]: val.currentTarget.checked
                      }
                      onChange(metricValue)
                    }}
                  />
                )
              })}
            </Container>
            {metricPackError && (
              <PageError message={getErrorMessage(metricPackError)} onClick={() => refetchMetricPacks()} />
            )}
          </>
        )
      }}
    />
  )
}