All files / modules/35-connectors/components/CreateConnector/CEGcpConnector/steps ChooseRequirements.tsx

87.5% Statements 35/40
63.64% Branches 28/44
75% Functions 9/12
87.18% Lines 34/39

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              219x 219x 219x 219x 219x               219x   219x 2x 2x   2x                 10x                       4x                                                 8x                                     2x   2x 2x 1x     1x           1x 1x 1x   1x     2x       2x 1x 1x 1x 1x     1x     1x       2x                               1x     6x                                     219x  
/*
 * Copyright 2022 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, { useRef } from 'react'
import { defaultTo, times } from 'lodash-es'
import { Button, CardSelect, Heading, Layout, StepProps, Text } from '@harness/uicore'
import { StringKeys, useStrings } from 'framework/strings'
import {
  useSelectCards,
  RequirementCard,
  CardData,
  FeaturesString
} from '@connectors/common/RequirementCard/RequirementCard'
import type { GcpCloudCostConnector } from 'services/cd-ng'
import type { CEGcpConnectorDTO } from './OverviewStep'
import css from '../CreateCeGcpConnector.module.scss'
 
const ChooseRequirements: React.FC<StepProps<CEGcpConnectorDTO>> = ({ prevStepData, nextStep, previousStep }) => {
  const { getString } = useStrings()
  const featuresEnabled = prevStepData?.spec?.featuresEnabled || []
 
  const featureCards = useRef<CardData[]>([
    {
      icon: 'ce-visibility',
      text: getString('connectors.ceAzure.chooseRequirements.visibilityCardDesc'),
      value: 'BILLING',
      heading: getString('connectors.costVisibility'),
      prefix: getString('connectors.ceGcp.gcp').toUpperCase(),
      isDefaultSelected: true,
      features: times(5, i =>
        getString(`connectors.ceGcp.chooseRequirements.cardDetails.billing.feat${i + 1}` as StringKeys)
      ),
      footer: getString('connectors.ceAws.crossAccountRoleStep1.default.footer')
    },
    {
      icon: 'ce-visibility',
      text: getString('connectors.ceAzure.chooseRequirements.visibilityCardDesc'),
      value: 'VISIBILITY',
      heading: getString('connectors.ceAws.crossAccountRoleStep1.visible.heading'),
      prefix: getString('connectors.ceGcp.gcp'),
      isDefaultSelected: false,
      features: times(2, i =>
        getString(`connectors.ceGcp.chooseRequirements.cardDetails.visibility.feat${i + 1}` as StringKeys)
      ),
      footer: (
        <>
          {getString('connectors.ceAzure.chooseRequirements.optimization.footer1')}{' '}
          <a
            href="https://ngdocs.harness.io/article/kxnsritjls-set-up-cost-visibility-for-gcp"
            target="_blank"
            rel="noreferrer"
            onClick={e => e.stopPropagation()}
          >
            {getString('permissions').toLowerCase()}
          </a>{' '}
          {getString('connectors.ceGcp.chooseRequirements.cardDetails.optimization.footer')}
        </>
      )
    },
    {
      icon: 'nav-settings',
      text: getString('connectors.ceAzure.chooseRequirements.optimizationCardDesc'),
      value: 'OPTIMIZATION',
      heading: getString('common.ce.autostopping'),
      prefix: getString('connectors.ceGcp.chooseRequirements.cardDetails.optimization.prefix'),
      isDefaultSelected: false,
      features: times(4, i =>
        getString(`connectors.ceGcp.chooseRequirements.cardDetails.optimization.feat${i + 1}` as StringKeys)
      ),
      footer: (
        <>
          {getString('connectors.ceAzure.chooseRequirements.optimization.footer1')}{' '}
          <a
            href="https://ngdocs.harness.io/article/kxnsritjls-set-up-cost-visibility-for-gcp"
            target="_blank"
            rel="noreferrer"
            onClick={e => e.stopPropagation()}
          >
            {getString('permissions').toLowerCase()}
          </a>{' '}
          {getString('connectors.ceGcp.chooseRequirements.cardDetails.optimization.footer')}
        </>
      )
    }
  ]).current
 
  const { selectedCards, setSelectedCards } = useSelectCards({ featuresEnabled, featureCards })
 
  const handleSubmit = () => {
    const features: FeaturesString[] = selectedCards.map(card => card.value)
    Iif (prevStepData?.includeBilling) {
      features.push('BILLING')
    }
    const newspec: GcpCloudCostConnector = {
      projectId: '',
      ...prevStepData?.spec,
      featuresEnabled: features,
      serviceAccountEmail: defaultTo(prevStepData?.serviceAccount, '')
    }
    const payload = prevStepData
    Eif (payload) {
      payload.spec = newspec
    }
    nextStep?.(payload)
  }
 
  const handleprev = () => {
    previousStep?.({ ...(prevStepData as CEGcpConnectorDTO) })
  }
 
  const handleCardSelection = (item: CardData) => {
    Eif (!item.isDefaultSelected) {
      const sc = [...selectedCards]
      const index = sc.indexOf(item)
      Iif (index > -1) {
        sc.splice(index, 1)
      } else {
        sc.push(item)
      }
 
      setSelectedCards(sc)
    }
  }
 
  return (
    <Layout.Vertical className={css.stepContainer}>
      <Heading level={2} className={css.header}>
        {getString('connectors.ceGcp.chooseRequirements.heading')}
        <span>{getString('connectors.ceGcp.chooseRequirements.choosePermissions')}</span>
      </Heading>
      <Text color="grey800">{getString('connectors.ceGcp.chooseRequirements.description')}</Text>
      <Layout.Vertical spacing={'medium'}>
        <Text font={{ italic: true }}>{getString('connectors.ceGcp.chooseRequirements.info')}</Text>
        <div>
          <CardSelect
            data={featureCards}
            selected={selectedCards}
            multi={true}
            className={css.cardsGrid}
            onChange={item => {
              handleCardSelection(item)
            }}
            cornerSelected={true}
            renderItem={item => <RequirementCard {...item} />}
          />
          <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}
              disabled={!prevStepData?.includeBilling && selectedCards.length === 0}
            />
          </Layout.Horizontal>
        </div>
      </Layout.Vertical>
    </Layout.Vertical>
  )
}
 
export default ChooseRequirements