All files / modules/75-cd/components/PipelineSteps/Common/Terraform/Editview TFVarStore.tsx

100% Statements 37/37
88.14% Branches 52/59
75% Functions 6/8
100% Lines 36/36

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 206 207 208 209 210              103x 103x 103x   103x                           103x 103x 103x   103x 103x 103x 103x   103x                       103x                     19x 19x 19x         19x 19x                 19x     8x 8x 3x       19x       19x                                                 24x 24x 24x 24x       120x           3x 3x 3x 3x                                                                                                 1x 1x   1x                                                          
/*
 * 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 { useParams } from 'react-router-dom'
import * as Yup from 'yup'
 
import {
  Button,
  ButtonVariation,
  Card,
  Formik,
  Heading,
  Icon,
  Layout,
  MultiTypeInputType,
  getMultiTypeFromValue,
  StepProps,
  Text,
  ButtonSize
} from '@wings-software/uicore'
import { Form } from 'formik'
import { Color } from '@harness/design-system'
import { useStrings } from 'framework/strings'
import type { ConnectorSelectedValue } from '@connectors/components/ConnectorReferenceField/ConnectorReferenceField'
import { FormMultiTypeConnectorField } from '@connectors/components/ConnectorReferenceField/FormMultiTypeConnectorField'
import { useVariablesExpression } from '@pipeline/components/PipelineStudio/PiplineHooks/useVariablesExpression'
import { ConnectorRefSchema } from '@common/utils/Validation'
import { ConnectorTypes, AllowedTypes, tfVarIcons, ConnectorMap, ConnectorLabelMap } from './TerraformConfigFormHelper'
 
import css from './TerraformVarfile.module.scss'
 
interface TFVarStoreProps {
  initialValues: any
  isEditMode: boolean
  allowableTypes: MultiTypeInputType[]
  handleConnectorViewChange?: () => void
  isReadonly?: boolean
  setConnectorView?: (val: boolean) => void
  setSelectedConnector: (val: string) => void
}
 
export const TFVarStore: React.FC<StepProps<any> & TFVarStoreProps> = ({
  prevStepData,
  nextStep,
  initialValues,
  isEditMode,
  allowableTypes,
  handleConnectorViewChange,
  isReadonly,
  setConnectorView,
  setSelectedConnector
}) => {
  const [selectedType, setSelectedType] = React.useState('')
  const { getString } = useStrings()
  const { accountId, projectIdentifier, orgIdentifier } = useParams<{
    projectIdentifier: string
    orgIdentifier: string
    accountId: string
  }>()
  const { expressions } = useVariablesExpression()
  const iValues = {
    varFile: {
      store: {
        spec: {
          connectorRef: ''
        }
      }
    }
  }
  React.useEffect(() => {
    /* istanbul ignore next */
    setSelectedType(initialValues?.varFile?.spec?.store?.type)
    setSelectedConnector(initialValues?.varFile?.spec?.store?.type)
    if (setConnectorView) {
      setConnectorView(false)
    }
  }, [])
 
  const newConnectorLabel = `${getString('newLabel')} ${
    !!selectedType && getString(ConnectorLabelMap[selectedType as ConnectorTypes])
  } ${getString('connector')}`
 
  return (
    <Layout.Vertical padding="small" className={css.tfVarStore}>
      <Heading level={2} style={{ color: Color.BLACK, fontSize: 24, fontWeight: 'bold' }} margin={{ bottom: 'large' }}>
        {getString('cd.specifyTfVarStore')}
      </Heading>
 
      <Formik
        formName="tfVarStoreForm"
        initialValues={isEditMode ? initialValues : iValues}
        enableReinitialize={true}
        onSubmit={() => {
          /* istanbul ignore next */
          setSelectedType('')
        }}
        validationSchema={Yup.object().shape({
          varFile: Yup.object().shape({
            store: Yup.object().shape({
              spec: Yup.object().shape({
                connectorRef: ConnectorRefSchema()
              })
            })
          })
        })}
      >
        {formik => {
          const connectorRef = formik.values.varFile?.spec?.store?.spec?.connectorRef
          const isFixedValue = getMultiTypeFromValue(connectorRef) === MultiTypeInputType.FIXED
          const disabled = !selectedType || (isFixedValue && !(connectorRef as ConnectorSelectedValue)?.connector)
          return (
            <>
              <Layout.Horizontal flex={{ justifyContent: 'flex-start', alignItems: 'flex-start' }}>
                {AllowedTypes.map(item => (
                  <div key={item} className={css.squareCardContainer}>
                    <Card
                      className={css.manifestIcon}
                      selected={item === selectedType}
                      data-testid={`varStore-${item}`}
                      onClick={() => {
                        setSelectedConnector(item)
                        setSelectedType(item)
                        Eif (isFixedValue) {
                          formik?.setFieldValue('varFile.spec.store.spec.connectorRef', '')
                        }
                      }}
                    >
                      <Icon name={tfVarIcons[item]} size={26} />
                    </Card>
                    <Text color={Color.BLACK_100}>{item}</Text>
                  </div>
                ))}
              </Layout.Horizontal>
              <Form className={css.formComponent}>
                <div className={css.formContainerStepOne}>
                  {selectedType && (
                    <Layout.Horizontal
                      spacing={'medium'}
                      flex={{ alignItems: 'flex-start', justifyContent: 'flex-start' }}
                    >
                      <FormMultiTypeConnectorField
                        label={
                          <Text style={{ display: 'flex', alignItems: 'center' }}>
                            {selectedType} {getString('connector')}
                            <Button
                              icon="question"
                              minimal
                              tooltip={`${selectedType} ${getString('connector')}`}
                              iconProps={{ size: 14 }}
                            />
                          </Text>
                        }
                        type={ConnectorMap[selectedType]}
                        width={400}
                        name="varFile.spec.store.spec.connectorRef"
                        placeholder={`${getString('select')} ${selectedType} ${getString('connector')}`}
                        accountIdentifier={accountId}
                        projectIdentifier={projectIdentifier}
                        orgIdentifier={orgIdentifier}
                        style={{ marginBottom: 10 }}
                        multiTypeProps={{ expressions, allowableTypes }}
                      />
                      <Button
                        variation={ButtonVariation.LINK}
                        size={ButtonSize.SMALL}
                        disabled={isReadonly}
                        id="new-var-connector"
                        text={newConnectorLabel}
                        className={css.newConnectorButton}
                        icon="plus"
                        iconProps={{ size: 12 }}
                        onClick={() => {
                          Eif (handleConnectorViewChange) {
                            handleConnectorViewChange()
                          }
                          nextStep?.({ ...prevStepData, selectedType })
                        }}
                      />
                    </Layout.Horizontal>
                  )}
                </div>
 
                <Layout.Horizontal spacing="xxlarge">
                  <Button
                    variation={ButtonVariation.PRIMARY}
                    type="submit"
                    text={getString('continue')}
                    rightIcon="chevron-right"
                    /* istanbul ignore next */
                    onClick={() => {
                      /* istanbul ignore next */
                      nextStep?.({ ...formik.values, selectedType })
                    }}
                    disabled={disabled}
                  />
                </Layout.Horizontal>
              </Form>
            </>
          )
        }}
      </Formik>
    </Layout.Vertical>
  )
}