All files / modules/35-connectors/components/CreateConnector/CENGAwsConnector/steps CostUsageReport.tsx

88.1% Statements 37/42
68.06% Branches 49/72
66.67% Functions 6/9
90% Lines 36/40

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              219x 219x 219x                     219x   219x 219x 219x 219x 219x 219x   219x   219x 3x   3x   3x 3x 3x 3x       3x 1x         1x 1x 1x 1x   1x 1x     3x         3x 3x   3x           3x 1x     3x                                                                                           1x     7x                                                                                                         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, { useContext, useEffect, useState } from 'react'
import * as Yup from 'yup'
import {
  Button,
  Container,
  Formik,
  FormikForm,
  Heading,
  Layout,
  StepProps,
  FormInput,
  Text
} from '@wings-software/uicore'
import { useStrings } from 'framework/strings'
import type { AwsCurAttributes, CEAwsConnector } from 'services/cd-ng'
import { DialogExtensionContext } from '@connectors/common/ConnectorExtention/DialogExtention'
import LabelWithTooltip from '@connectors/common/LabelWithTooltip/LabelWithTooltip'
import { CE_AWS_CONNECTOR_CREATION_EVENTS } from '@connectors/trackingConstants'
import { useStepLoadTelemetry } from '@connectors/common/useTrackStepLoad/useStepLoadTelemetry'
import CostUsageReportExtention from './CostUsageReportExtenstion'
import CostUsageReportExisting from './CostUsageReportExisting'
import type { CEAwsConnectorDTO } from './OverviewStep'
import css from '../CreateCeAwsConnector.module.scss'
 
const CostUsageStep: React.FC<StepProps<CEAwsConnectorDTO>> = props => {
  const { getString } = useStrings()
 
  useStepLoadTelemetry(CE_AWS_CONNECTOR_CREATION_EVENTS.LOAD_CUR_STEP)
 
  const { prevStepData, nextStep, previousStep } = props
  const { triggerExtension, closeExtension } = useContext(DialogExtensionContext)
  const existingCurReports = prevStepData?.existingCurReports || []
  const [isExistingCostUsageReport, setIsExistingCostUsageReport] = useState<boolean>(
    (!prevStepData?.includeBilling || false) && (existingCurReports.length > 0 || false)
  )
 
  const handleSubmit = (formData: AwsCurAttributes) => {
    const newspec: CEAwsConnector = {
      crossAccountAccess: { crossAccountRoleArn: '' },
      ...prevStepData?.spec,
      curAttributes: formData
    }
    const payload = prevStepData
    Eif (payload) {
      payload.spec = newspec
      payload.includeBilling = !isExistingCostUsageReport
    }
    closeExtension()
    nextStep?.(payload)
  }
 
  const handlePrev = () => {
    closeExtension()
    previousStep?.({ ...(prevStepData as CEAwsConnectorDTO) })
  }
 
  const getValidationScheme = () => {
    Iif (isExistingCostUsageReport) return {}
    else
      return Yup.object().shape({
        reportName: Yup.string().required(getString('connectors.ceAws.cur.validation.reportRequired')),
        s3BucketName: Yup.string().required(getString('connectors.ceAws.cur.validation.bucketRequired'))
      })
  }
 
  useEffect(() => {
    Eif (!isExistingCostUsageReport) triggerExtension(<CostUsageReportExtention />)
  }, [isExistingCostUsageReport])
 
  return (
    <Layout.Vertical className={css.stepContainer}>
      <Heading level={2} className={css.header}>
        {getString('connectors.ceAws.cur.heading')}
      </Heading>
      <Text className={css.subHeader}>{getString('connectors.ceAws.cur.subheading')}</Text>
 
      {!isExistingCostUsageReport && (
        <div>
          <Text
            font="small"
            className={css.info}
            color="primary7"
            inline
            icon="info-sign"
            iconProps={{ size: 15, color: 'primary7', margin: { right: 'xsmall' } }}
          >
            {getString('connectors.ceAws.cur.followInstruction')}
          </Text>
          <Container padding={{ bottom: 35 }}>
            <Layout.Vertical style={{ width: '65%' }}>
              <Button
                className={css.launchTemplateBut}
                text={getString('connectors.ceAws.cur.launchTemplate')}
                icon="main-share"
                iconProps={{ size: 12, margin: { right: 'xsmall' } }}
                onClick={() => {
                  window.open('https://console.aws.amazon.com/billing/home?#/reports')
                }}
              />
              <Text font="small" style={{ textAlign: 'center' }}>
                {getString('connectors.ceAws.cur.login')}
              </Text>
            </Layout.Vertical>
          </Container>
        </div>
      )}
 
      <div style={{ flex: 1 }}>
        <Formik<AwsCurAttributes>
          formName="costUsageReportForm"
          initialValues={{
            reportName: prevStepData?.spec?.curAttributes?.reportName || '',
            s3BucketName: prevStepData?.spec?.curAttributes?.s3BucketName || ''
          }}
          validationSchema={getValidationScheme()}
          onSubmit={formData => handleSubmit(formData)}
        >
          {() => (
            <FormikForm>
              {isExistingCostUsageReport && (
                <Layout.Vertical spacing="xlarge">
                  <CostUsageReportExisting existingCurReports={prevStepData?.existingCurReports || []} />
                  <div>
                    <Button
                      className={css.newCurReport}
                      text={getString('connectors.ceAws.cur.createNew')}
                      onClick={() => {
                        setIsExistingCostUsageReport(false)
                      }}
                    />
                  </div>
                </Layout.Vertical>
              )}
 
              {!isExistingCostUsageReport && (
                <div>
                  <FormInput.Text
                    name={'reportName'}
                    label={
                      <LabelWithTooltip
                        label={getString('connectors.ceAws.cur.reportName')}
                        extentionComponent={CostUsageReportExtention}
                      />
                    }
                    className={css.dataFields}
                  />
                  <FormInput.Text
                    name={'s3BucketName'}
                    label={
                      <LabelWithTooltip
                        label={getString('connectors.ceAws.cur.bucketName')}
                        extentionComponent={CostUsageReportExtention}
                      />
                    }
                    className={css.dataFields}
                  />
                </div>
              )}
 
              <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" />
              </Layout.Horizontal>
            </FormikForm>
          )}
        </Formik>
      </div>
    </Layout.Vertical>
  )
}
 
export default CostUsageStep