All files / modules/85-cv/pages/health-source/connectors/GCOMetricsHealthSource/components/ManualInputQueryModal ManualInputQueryModal.tsx

95% Statements 19/20
83.33% Branches 10/12
83.33% Functions 5/6
94.74% Lines 18/19

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              43x 43x 43x 43x 43x 43x   43x                 43x   43x         43x                       9x           20x                       43x 9x 9x 9x                 3x 3x                                                      
/*
 * 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 from 'react'
import { Dialog, IDialogProps } from '@blueprintjs/core'
import { object as yupObject, string as yupString } from 'yup'
import { Button, Container, Formik, FormikForm } from '@wings-software/uicore'
import { NameId } from '@common/components/NameIdDescriptionTags/NameIdDescriptionTags'
import { StringKeys, useStrings } from 'framework/strings'
import type { UseStringsReturn } from 'framework/strings'
import css from './ManualInputQueryModal.module.scss'
 
interface UseManualInputQueryModalProps {
  title?: StringKeys
  onSubmit: (values: { metricName: string; identifier: string }) => void
  manuallyInputQueries?: string[]
  closeModal: () => void
}
 
export const MANUAL_INPUT_QUERY = 'ManualInputQuery'
 
export const FieldNames = {
  METRIC_NAME: 'metricName',
  IDENTIFIER: 'identifier'
}
 
const DialogOptions: IDialogProps = {
  isOpen: true,
  usePortal: true,
  autoFocus: true,
  canEscapeKeyClose: true,
  canOutsideClickClose: true,
  enforceFocus: false,
  style: { width: 500 },
  className: 'ManualInputModal'
}
 
function getValidatitionSchema(getString: UseStringsReturn['getString'], manuallyInputQueries?: string[]) {
  return yupObject().shape({
    [FieldNames.METRIC_NAME]: yupString()
      .required(getString('cv.monitoringSources.metricNameLabel'))
      .test({
        name: 'Ensure that input query is unique',
        test: function (value: string) {
          return manuallyInputQueries?.find(metricName => metricName.toLocaleLowerCase() === value?.toLocaleLowerCase())
            ? this.createError({
                message: getString('cv.monitoringSources.gco.manualInputQueryModal.validation.uniqueMetricName', {
                  metricName: value
                })
              })
            : true
        }
      })
  })
}
 
export function ManualInputQueryModal(props: UseManualInputQueryModalProps): JSX.Element {
  const { title, onSubmit, manuallyInputQueries, closeModal } = props
  const { getString } = useStrings()
  return (
    <Dialog
      {...DialogOptions}
      onClose={closeModal}
      title={getString(title || 'cv.monitoringSources.gco.manualInputQueryModal.modalTitle')}
    >
      <Container className={css.main}>
        <Formik
          onSubmit={values => {
            onSubmit(values)
            closeModal()
          }}
          formName="manualInputQuery"
          initialValues={{ metricName: '', identifier: '' }}
          validationSchema={getValidatitionSchema(getString, manuallyInputQueries)}
        >
          <FormikForm className={css.form}>
            <NameId
              nameLabel={getString('cv.monitoringSources.metricNameLabel')}
              identifierProps={{
                inputName: FieldNames.METRIC_NAME,
                idName: FieldNames.IDENTIFIER
              }}
            />
 
            <Container className={css.buttonContainer}>
              <Button onClick={() => closeModal()}>{getString('cancel')}</Button>
              <Button type="submit" intent="primary">
                {getString('submit')}
              </Button>
            </Container>
          </FormikForm>
        </Formik>
      </Container>
    </Dialog>
  )
}