All files / modules/75-cd/components/PipelineSteps/InfraProvisioning ChooseProvisioner.tsx

87.5% Statements 28/32
100% Branches 4/4
75% Functions 6/8
87.1% Lines 27/31

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              99x 99x 99x 99x 99x   99x 99x   99x   99x   99x                                                       99x 9x 9x   9x         9x 1x                         4x       4x 1x   4x                                                                       9x   9x 1x 1x   9x 1x         99x  
/*
 * 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 from 'react'
import { Classes, Dialog } from '@blueprintjs/core'
import cx from 'classnames'
import { Layout, Card, Icon, Text, IconName, Button, ButtonVariation } from '@wings-software/uicore'
import { useModalHook } from '@harness/use-modal'
 
import { merge } from 'lodash-es'
import { useStrings } from 'framework/strings'
 
import { ProvisionerTypes } from '../Common/ProvisionerConstants'
 
import css from './InfraProvisioning.module.scss'
 
const provisionerTypes: { name: string; icon: IconName; iconColor?: string; enabled: boolean }[] = [
  {
    name: ProvisionerTypes.Terraform,
    icon: 'terraform-apply-new',
    iconColor: '#5C4EE5',
    enabled: true
  },
  {
    name: ProvisionerTypes.CloudFormation,
    icon: 'cloudformation',
    enabled: false
  },
  {
    name: ProvisionerTypes.ARM,
    icon: 'arm',
    enabled: false
  },
  {
    name: ProvisionerTypes.Script,
    icon: 'script',
    enabled: false
  }
]
 
interface ChooseProvisionerProps {
  onSubmit: any
  onClose: any
}
const useChooseProvisioner = (props: ChooseProvisionerProps) => {
  const { getString } = useStrings()
  const [provData, setProvData] = React.useState()
 
  const modalProps = {
    isOpen: true,
    canEscapeKeyClose: true,
    canOutsideClickClose: true
  }
  const ProvDialog = (): JSX.Element => (
    <Dialog
      onClose={() => {
        props.onClose()
        hideModal()
      }}
      enforceFocus={false}
      className={cx(Classes.DIALOG, css.chooseProvisionerDialog)}
      {...modalProps}
    >
      <Layout.Vertical spacing="large">
        <div className={css.provisionerText}>{getString('cd.chooseProvisionerText')}</div>
        <Layout.Horizontal height={120}>
          {provisionerTypes.map((type: { name: string; icon: IconName; enabled: boolean; iconColor?: string }) => {
            const iconProps = {
              name: type.icon as IconName,
              size: 26
            }
            if (type.iconColor) {
              merge(iconProps, { color: type.iconColor })
            }
            return (
              <div key={type.name} className={css.squareCardContainer}>
                <Card
                  disabled={!type.enabled}
                  interactive={true}
                  selected={type.name === ProvisionerTypes.Terraform}
                  cornerSelected={type.name === ProvisionerTypes.Terraform}
                  className={cx({ [css.disabled]: !type.enabled }, css.squareCard)}
                >
                  <Icon {...iconProps} />
                </Card>
                <Text
                  style={{
                    fontSize: '12px',
                    color: type.enabled ? 'var(--grey-900)' : 'var(--grey-350)',
                    textAlign: 'center'
                  }}
                >
                  {type.name}
                </Text>
              </div>
            )
          })}
        </Layout.Horizontal>
        <Button
          variation={ButtonVariation.PRIMARY}
          text={getString('cd.setUpProvisionerBtnText')}
          className={css.provisionerBtnText}
          onClick={() => {
            props.onSubmit(provData)
            hideModal()
          }}
        />
      </Layout.Vertical>
    </Dialog>
  )
  const [showModal, hideModal] = useModalHook(() => <ProvDialog />, [provData])
 
  const open = (data?: any) => {
    setProvData(data)
    showModal()
  }
  return {
    showModal: (data: any) => open(data),
    hideModal
  }
}
 
export default useChooseProvisioner