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

94.64% Statements 53/56
79.17% Branches 19/24
91.67% Functions 22/24
96.23% Lines 51/53

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 196 197 198 199 200 201 202 203 204 205              6x 6x 6x 6x     6x             6x                     6x                             6x 37x   35x 16x 14x 14x 25x 25x     14x       35x 396x     35x 198x     3x       35x 132x       2x       35x                             66x         1x 1x   1x 1x                                       66x       1x                         66x     1x             35x 35x 59x             35x 8x 8x 8x 8x     8x 1x   8x 1x     8x     35x                   42x         8x                 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 { isEmpty as _isEmpty, omit as _omit, debounce as _debounce } from 'lodash-es'
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 { PortConfig } 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 actions: SelectItem[] = [
  {
    label: 'Redirect',
    value: 'redirect'
  },
  {
    label: 'Forward',
    value: 'forward'
  }
]
 
interface CORoutingTableProps {
  routingRecords: PortConfig[]
  setRoutingRecords: (records: PortConfig[]) => void
}
const CORoutingTable: React.FC<CORoutingTableProps> = props => {
  const [forwardConfigRows, setForwardConfigRows] = useState<Record<string, boolean>>({})
 
  useEffect(() => {
    if (!_isEmpty(props.routingRecords) && _isEmpty(forwardConfigRows)) {
      const configRows: Record<string, boolean> = {}
      props.routingRecords.forEach((record, _index) => {
        Eif (record.action === 'forward') {
          configRows[_index] = true
        }
      })
      setForwardConfigRows(configRows)
    }
  }, [props.routingRecords])
 
  const getItembyValue = (items: SelectItem[], value: string): SelectItem => {
    return items.filter(x => x.value == value)[0]
  }
 
  const getTextInput: Field['renderer'] = (value, _rowIndex, handleChange) => (
    <TextInput
      defaultValue={value}
      style={{ border: 'none', marginBottom: 0 }}
      onChange={e => handleChange((e.currentTarget as HTMLInputElement).value)}
    />
  )
 
  const getProtocolSelect: Field['renderer'] = (value, _rowIndex, handleChange) => (
    <Select
      className={css.selectCell}
      value={getItembyValue(protocols, value)}
      items={protocols}
      onChange={item => handleChange(item.value)}
    />
  )
 
  const fields: Field[] = [
    {
      name: 'protocol',
      label: 'PROTOCOL',
      renderer: getProtocolSelect
    },
    {
      name: 'port',
      label: 'PORT',
      renderer: getTextInput
    },
    {
      name: 'action',
      label: 'ACTION',
      renderer: (value, rowIndex, handleChange) => (
        <Select
          className={css.selectCell}
          value={getItembyValue(actions, value)}
          items={actions}
          onChange={item => {
            handleChange(item.value)
            Iif (item.value === 'forward') {
              setForwardConfigRows(prevRecord => ({ ...prevRecord, [rowIndex]: true }))
            } else Eif (forwardConfigRows[rowIndex]) {
              setForwardConfigRows(prevRecord => _omit(prevRecord, rowIndex))
            }
          }}
        />
      )
    },
    {
      name: 'target_protocol',
      label: 'TARGET PROTOCOL',
      renderer: getProtocolSelect
    },
    {
      name: 'target_port',
      label: 'TARGET PORT',
      renderer: getTextInput
    },
    {
      name: 'redirect_url',
      label: 'REDIRECT URL',
      renderer: (value, _rowIndex, handleChange) => (
        <TextInput
          defaultValue={value}
          disabled={forwardConfigRows[_rowIndex]}
          style={{ border: 'none', marginBottom: 0 }}
          onChange={e => handleChange((e.currentTarget as HTMLInputElement).value)}
        />
      )
    },
    {
      name: 'server_name',
      label: 'SERVER NAME',
      renderer: getTextInput
    },
    {
      name: 'routing_rules',
      label: 'PATH MATCH',
      renderer: (value, _rowIndex, handleChange) => (
        <TextInput
          defaultValue={value}
          style={{ border: 'none', marginBottom: 0 }}
          onChange={e => handleChange((e.currentTarget as HTMLInputElement).value)}
          data-testid="routingRules"
        />
      )
    }
  ]
 
  const getInitialData = () => {
    return props.routingRecords.map(_record => {
      return {
        ..._record,
        routing_rules: _isEmpty(_record['routing_rules']) ? '' : _record['routing_rules']?.[0].path_match
      }
    })
  }
 
  const handleFielArrayChange = _debounce(data => {
    const portConfig = [...(data.modifiedRows as Array<any>)]
    portConfig.forEach(config => {
      const routingRules = config['routing_rules']
      config['routing_rules'] = !_isEmpty(routingRules)
        ? [{ path_match: Array.isArray(routingRules) ? routingRules[0].path_match : routingRules }]
        : []
      if (!_isEmpty(config.port)) {
        config.port = Number(config.port)
      }
      if (!_isEmpty(config.target_port)) {
        config.target_port = Number(config.target_port)
      }
    })
    props.setRoutingRecords(portConfig)
  }, 500)
 
  return (
    <div className={css.portConfigTable}>
      <Formik
        initialValues={{ routingTableData: getInitialData() }}
        enableReinitialize={true}
        onSubmit={values => {
          console.log(values) // eslint-disable-line
        }}
      >
        {formikProps => (
          <form onSubmit={formikProps.handleSubmit}>
            <FieldArray
              label={''}
              name={'routingTableData'}
              fields={fields}
              onChange={data => handleFielArrayChange(data)}
            />
          </form>
        )}
      </Formik>
    </div>
  )
}
 
export default CORoutingTable