All files / modules/75-ce/components/COGatewayConfig COHealthCheckTable.tsx

94.74% Statements 36/38
91.3% Branches 42/46
90% Functions 18/20
94.59% Lines 35/37

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              6x 6x 6x     6x             6x                     6x               6x 11x 11x   11x 11x     11x     1x                           6x 11x   11x 9x       22x     11x 11x     1x       11x 22x     2x       11x         11x       1x                                           11x         1x                         11x                 11x       11x   5x 5x                             6x  
/*
 * 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, useState } from 'react'
import { FieldArray, Select, TextInput } from '@wings-software/uicore'
import { Formik } from 'formik'
import type { Field } from '@wings-software/uicore/dist/components/FieldArray/FieldArray'
import type { HealthCheck } from 'services/lw'
import css from './COGatewayConfig.module.scss'
 
interface SelectItem {
  label: string
  value: string
}
 
const protocols: SelectItem[] = [
  {
    label: 'http',
    value: 'http'
  },
  {
    label: 'https',
    value: 'https'
  }
]
 
const statusRegEx = new RegExp(/^\d{3}-\d{3}$/)
 
interface StatusRangeInputProps {
  status_code_from: number
  status_code_to: number
  onChange: (val: string) => void
}
 
const StatusRangeInput: React.FC<StatusRangeInputProps> = props => {
  const { status_code_from, status_code_to } = props
  const [errorFlag, setErrorFlag] = useState(false)
 
  const getStatusStringFromRange = (from: number | null = null, to: number | null = null) => {
    return `${from}-${to}`
  }
 
  return (
    <TextInput
      defaultValue={getStatusStringFromRange(status_code_from as number, status_code_to)}
      onChange={e => props.onChange((e.target as HTMLInputElement).value)}
      onBlur={e => {
        setErrorFlag(!statusRegEx.test((e.target as HTMLInputElement).value))
      }}
      errorText={'Please enter from & to values'}
      intent={errorFlag ? 'danger' : 'none'}
    />
  )
}
 
interface COHealthCheckTableProps {
  pattern: HealthCheck | null
  updatePattern: (pattern: HealthCheck) => void
}
const COHealthCheckTable: React.FC<COHealthCheckTableProps> = props => {
  const [healthCheckPattern, setHealthCheckPattern] = useState<HealthCheck[]>(props.pattern ? [props.pattern] : [])
 
  useEffect(() => {
    props.updatePattern(healthCheckPattern[0])
  }, [healthCheckPattern])
 
  function getItembyValue(items: SelectItem[], value: string): SelectItem {
    return items.filter(x => x.value == value)[0]
  }
 
  const getTextInputEl: Field['renderer'] = (value, _rowIndex, handleChange) => (
    <TextInput
      defaultValue={value}
      style={{ border: 'none' }}
      onChange={e => handleChange((e.currentTarget as HTMLInputElement).value)}
    />
  )
 
  const getNumericInput: Field['renderer'] = (value, _rowIndex, handleChange) => (
    <TextInput
      defaultValue={value}
      style={{ border: 'none' }}
      onChange={e => handleChange(Number((e.currentTarget as HTMLInputElement).value))}
    />
  )
 
  const fields: Field[] = [
    {
      name: 'protocol',
      label: 'PROTOCOL',
      renderer: (value, _rowIndex, handleChange) => (
        <Select
          className={css.selectCell}
          value={getItembyValue(protocols, value)}
          items={protocols}
          onChange={item => handleChange(item.value)}
        />
      )
    },
    {
      name: 'path',
      label: 'PATH',
      renderer: getTextInputEl
    },
    {
      name: 'port',
      label: 'PORT',
      renderer: getNumericInput
    },
    {
      name: 'timeout',
      label: 'TIMEOUT',
      renderer: getNumericInput
    },
    {
      name: 'status',
      label: 'STATUS (from-to)',
      renderer: (value, _rowIndex, handleChange) => (
        <StatusRangeInput
          status_code_from={value?.split('-').map(Number)?.[0] || 200}
          status_code_to={value?.split('-').map(Number)?.[1] || 299}
          onChange={
            val => handleChange(val)
            // value => {
            // if (statusRegEx.test(value)) {
            //   const [from, to] = value.split('-').map(Number)
            //   setHealthCheckPattern([{ ...healthCheckPattern[0], status_code_from: from, status_code_to: to }])
            // }
            // }
          }
        />
      )
    }
  ]
 
  return (
    <div className={css.healthCheckTable}>
      <Formik
        initialValues={{ healthCheckData: healthCheckPattern }}
        onSubmit={values => {
          console.log(values) // eslint-disable-line
        }}
      >
        {_formikProps => (
          <FieldArray
            label={''}
            name={'healthCheckData'}
            fields={fields}
            isDeleteOfRowAllowed={() => false}
            onChange={data => {
              const _healthCheckData = (data.modifiedRows as any[])[0]
              setHealthCheckPattern([
                {
                  ..._healthCheckData,
                  status_code_from: _healthCheckData.status?.split('-').map(Number)?.[0] || 200,
                  status_code_to: _healthCheckData.status?.split('-').map(Number)?.[1] || 299
                }
              ])
            }}
          />
        )}
      </Formik>
    </div>
  )
}
 
export default COHealthCheckTable