All files / modules/35-connectors/components/CreateConnector/CEK8sConnector FeatureSelectionStep.tsx

92.86% Statements 39/42
68.75% Branches 44/64
90% Functions 9/10
94.74% Lines 36/38

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              219x 219x 219x     219x 219x 219x   219x 219x 219x                                             219x 5x   4x   4x   4x                                 4x     4x 4x         4x 2x     1x   1x       1x 1x 1x     4x       4x   1x 1x 1x 1x     1x   1x     4x                                           1x       8x                                                           219x  
/*
 * 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, { useState } from 'react'
import { Button, Heading, Layout, StepProps, CardSelect, Icon, IconName, Text } from '@wings-software/uicore'
import { useStrings } from 'framework/strings'
import type { ConnectorInfoDTO } from 'services/cd-ng'
 
import { CE_K8S_CONNECTOR_CREATION_EVENTS } from '@connectors/trackingConstants'
import { useStepLoadTelemetry } from '@connectors/common/useTrackStepLoad/useStepLoadTelemetry'
import css from './CEK8sConnector.module.scss'
 
enum Features {
  VISIBILITY,
  OPTIMIZATION
}
 
export type FeaturesString = keyof typeof Features
 
interface CardData {
  icon: IconName
  text: string
  value: FeaturesString
  heading: string
  subheading: string
}
 
interface StepSecretManagerProps extends ConnectorInfoDTO {
  spec: any
}
 
interface FeatureSelectionStepProps {
  name: string
  isEditMode: boolean
  handleOptimizationSelection: (val: boolean) => void
}
 
const FeatureSelectionStep: React.FC<StepProps<StepSecretManagerProps> & FeatureSelectionStepProps> = props => {
  const { getString } = useStrings()
 
  useStepLoadTelemetry(CE_K8S_CONNECTOR_CREATION_EVENTS.LOAD_FEATURE_SELECTION)
 
  const { prevStepData, nextStep, previousStep } = props
 
  const cardData: CardData[] = [
    {
      icon: 'main-main-zoom_in',
      text: getString('connectors.ceK8.chooseRequirements.visibility.description'),
      value: 'VISIBILITY',
      heading: getString('connectors.ceK8.chooseRequirements.visibility.heading'),
      subheading: getString('connectors.ceK8.chooseRequirements.visibility.subheading')
    },
    {
      icon: 'gear',
      text: getString('connectors.ceK8.chooseRequirements.optimization.description'),
      value: 'OPTIMIZATION',
      heading: getString('connectors.ceK8.chooseRequirements.optimization.heading'),
      subheading: getString('connectors.ceK8.chooseRequirements.optimization.subheading')
    }
  ]
 
  const [cardsSelected, setCardsSelected] = useState<CardData[]>(
    prevStepData?.spec.featuresEnabled
      ? prevStepData?.spec?.featuresEnabled.map((feat: FeaturesString) => {
          const num = Features[feat]
          return cardData[num]
        })
      : [cardData[0]]
  )
 
  const handleSubmit = async () => {
    const featuresEnabled: FeaturesString[] = cardsSelected.map(card => card.value)
 
    // enable Secret creation step if OPTIMIZATION is selected
    await props.handleOptimizationSelection(featuresEnabled.indexOf('OPTIMIZATION') > -1)
 
    const newspec = {
      ...prevStepData?.spec,
      featuresEnabled
    }
    const payload = prevStepData
    Eif (payload) payload.spec = newspec
    nextStep?.(payload)
  }
 
  const handleprev = () => {
    previousStep?.({ ...prevStepData } as ConnectorInfoDTO)
  }
 
  const handleCardSelection = (item: CardData) => {
    // return for VISIBLIITY feature
    Iif (prevStepData?.spec?.fixFeatureSelection || item.value === Features[0]) return
    const selectedAr = [...cardsSelected]
    const index = selectedAr.map(i => i.value).indexOf(item.value)
    Iif (index > -1) {
      selectedAr.splice(index, 1)
    } else {
      selectedAr.push(item)
    }
    setCardsSelected(selectedAr)
  }
 
  return (
    <Layout.Vertical className={css.featureSelectionCont}>
      <Heading level={2} className={css.header}>
        {getString('connectors.ceK8.chooseRequirements.heading')}
      </Heading>
      <div style={{ padding: 5, paddingBottom: 20 }}>
        {!prevStepData?.spec?.fixFeatureSelection && (
          <Text> {getString('connectors.ceK8.chooseRequirements.subheading')}</Text>
        )}
        {getString(
          prevStepData?.spec?.fixFeatureSelection
            ? 'connectors.ceK8.chooseRequirements.fixFeaturesDescription'
            : 'connectors.ceK8.chooseRequirements.description'
        )}
      </div>
      <div style={{ flex: 1 }}>
        <CardSelect
          data={cardData}
          selected={cardsSelected}
          multi={true}
          className={css.grid}
          onChange={item => {
            handleCardSelection(item)
          }}
          cornerSelected={true}
          renderItem={item => (
            <div>
              <div style={{ display: 'flex', paddingBottom: 7 }}>
                <Icon name={item.icon} size={32} color="primary5" style={{ paddingRight: 10 }}></Icon>
                <p>
                  <div style={{ fontSize: 8, fontFamily: 'inter' }}>{item.heading}</div>{' '}
                  <div style={{ fontSize: 14, fontFamily: 'inter' }}>{item.subheading}</div>
                </p>
              </div>
              <p>
                <div dangerouslySetInnerHTML={{ __html: item.text }} />
              </p>
            </div>
          )}
        ></CardSelect>
 
        <Layout.Horizontal className={css.buttonPanel} spacing="small">
          <Button text={getString('previous')} icon="chevron-left" onClick={handleprev}></Button>
          <Button
            type="submit"
            intent="primary"
            text={getString('continue')}
            rightIcon="chevron-right"
            onClick={handleSubmit}
          />
        </Layout.Horizontal>
      </div>
    </Layout.Vertical>
  )
}
 
export default FeatureSelectionStep