All files / modules/33-auth-settings/pages/AccountOverview/views/Settings/connector ConnectorSettings.tsx

96.77% Statements 30/31
88% Branches 22/25
100% Functions 3/3
96.77% Lines 30/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 129 130              3x 3x 3x 3x 3x   3x   3x 3x 3x 3x 3x 3x             3x 9x 7x 7x 7x 7x   7x       7x                   7x             7x 1x 1x                     1x 1x       7x                                                           1x 1x                                               3x  
/*
 * 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, { useState } from 'react'
import { Text, Checkbox, Layout, ButtonVariation, shouldShowError, useToaster, Popover } from '@wings-software/uicore'
import { Color, FontVariation } from '@harness/design-system'
import { PopoverInteractionKind } from '@blueprintjs/core'
import { useParams } from 'react-router-dom'
 
import { useGetConnectorList, useUpdateAccountSetting } from 'services/cd-ng'
import type { ProjectPathProps } from '@common/interfaces/RouteInterfaces'
import { useStrings } from 'framework/strings'
import { PermissionIdentifier } from '@rbac/interfaces/PermissionIdentifier'
import RbacButton from '@rbac/components/Button/Button'
import { ResourceType } from '@rbac/interfaces/ResourceType'
import useRBACError from '@rbac/utils/useRBACError/useRBACError'
import css from '../AccountSettings.module.scss'
 
interface ConnectorSettingProps {
  disableBuiltInSM: boolean
  onChange: (val: boolean) => void
}
 
const ConnectorSettings: React.FC<ConnectorSettingProps> = props => {
  const { getRBACErrorMessage } = useRBACError()
  const { getString } = useStrings()
  const { showError, showSuccess } = useToaster()
  const { accountId, orgIdentifier, projectIdentifier } = useParams<ProjectPathProps>()
  const [disableApply, setDisableApply] = useState<boolean>(true)
 
  const { mutate: updateConnectorSetting, loading } = useUpdateAccountSetting({
    queryParams: { accountIdentifier: accountId }
  })
 
  const { data: secretManagersApiResponse, loading: loadingSecretsManagers } = useGetConnectorList({
    queryParams: {
      accountIdentifier: accountId,
      orgIdentifier,
      projectIdentifier,
      category: 'SECRET_MANAGER'
    }
  })
 
  const disabled =
    loading ||
    loadingSecretsManagers ||
    !!(
      !props.disableBuiltInSM &&
      (secretManagersApiResponse?.data?.content?.length ? secretManagersApiResponse.data.content.length < 2 : true)
    )
 
  const saveConnectorSettings = async () => {
    try {
      await updateConnectorSetting({
        accountIdentifier: accountId,
        orgIdentifier: orgIdentifier,
        projectIdentifier: projectIdentifier,
        type: 'Connector',
        config: {
          builtInSMDisabled: props.disableBuiltInSM
        }
      })
      showSuccess(getString('common.accountSetting.connector.saveSettingSuccess'))
    } catch (e) {
      Eif (shouldShowError(e)) {
        showError(getRBACErrorMessage(e))
      }
    }
  }
  return (
    <Layout.Horizontal flex={{ justifyContent: 'space-between' }} margin={{ top: 'small', bottom: 'small' }}>
      <Popover
        interactionKind={PopoverInteractionKind.HOVER}
        boundary="viewport"
        disabled={!disabled}
        content={
          <Text padding="medium" font={{ variation: FontVariation.SMALL }} color={Color.GREY_800}>
            {loading || loadingSecretsManagers
              ? getString('common.accountSetting.connector.loading')
              : getString('common.accountSetting.connector.disabledState')}
          </Text>
        }
      >
        <Checkbox
          labelElement={
            <div>
              <Text font={{ variation: FontVariation.BODY2 }} color={Color.GREY_900}>
                {getString('common.accountSetting.connector.disableBISMHeading')}
              </Text>
              <Text font={{ variation: FontVariation.SMALL }} color={Color.GREY_600}>
                {getString('common.accountSetting.connector.disableBISMSubHeading')}
              </Text>
            </div>
          }
          data-testid={`checkBox-disable-builtInSM`}
          defaultChecked={props.disableBuiltInSM}
          disabled={disabled}
          checked={props.disableBuiltInSM}
          onChange={(event: React.FormEvent<HTMLInputElement>) => {
            props.onChange(event.currentTarget.checked)
            setDisableApply(false)
          }}
          className={css.checkboxBuiltInSm}
        />
      </Popover>
      <RbacButton
        data-testid={'apply-connector-setting'}
        variation={ButtonVariation.PRIMARY}
        padding="none"
        text={getString('common.apply')}
        onClick={saveConnectorSettings}
        disabled={disableApply}
        loading={loading}
        permission={{
          permission: PermissionIdentifier.EDIT_ACCOUNT,
          resource: {
            resourceType: ResourceType.ACCOUNT
          }
        }}
      />
    </Layout.Horizontal>
  )
}
 
export default ConnectorSettings