All files / modules/33-auth-settings/pages/Configuration/RestrictEmailDomains RestrictEmailDomains.tsx

95% Statements 38/40
62.5% Branches 10/16
100% Functions 6/6
95% Lines 38/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              2x 2x 2x 2x 2x 2x   2x 2x 2x 2x 2x                 2x 6x 6x 6x 6x 6x   6x           6x 2x     6x 1x     6x   6x 1x 1x   1x 1x 1x             6x           1x 1x         6x 1x   1x 1x           6x                                                                               2x  
/*
 * 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, { Dispatch, SetStateAction } from 'react'
import { useParams } from 'react-router-dom'
import { Container, Card, Switch, Text, Button, ButtonVariation, useConfirmationDialog } from '@wings-software/uicore'
import { Color } from '@harness/design-system'
import { TagInput } from '@blueprintjs/core'
import { useStrings } from 'framework/strings'
import type { AccountPathProps } from '@common/interfaces/RouteInterfaces'
import { useToaster } from '@common/components'
import { useUpdateWhitelistedDomains } from 'services/cd-ng'
import { useRestrictEmailDomains } from '@auth-settings/modals/RestrictEmailDomains/useRestrictEmailDomains'
import useRBACError from '@rbac/utils/useRBACError/useRBACError'
import css from './RestrictEmailDomains.module.scss'
 
interface Props {
  whitelistedDomains: string[]
  refetchAuthSettings: () => void
  canEdit: boolean
  setUpdating: Dispatch<SetStateAction<boolean>>
}
 
const RestrictEmailDomains: React.FC<Props> = ({ whitelistedDomains, refetchAuthSettings, canEdit, setUpdating }) => {
  const { getRBACErrorMessage } = useRBACError()
  const { getString } = useStrings()
  const { showSuccess, showError } = useToaster()
  const { accountId } = useParams<AccountPathProps>()
  const emailRestrictionsEnabled = !!whitelistedDomains.length
 
  const { mutate: updateWhitelistedDomains, loading: updatingWhitelistedDomains } = useUpdateWhitelistedDomains({
    queryParams: {
      accountIdentifier: accountId
    }
  })
 
  React.useEffect(() => {
    setUpdating(updatingWhitelistedDomains)
  }, [updatingWhitelistedDomains, setUpdating])
 
  const onSuccess = (): void => {
    refetchAuthSettings()
  }
 
  const { openRestrictEmailDomainsModal } = useRestrictEmailDomains({ onSuccess, whitelistedDomains })
 
  const disableWhitelistedDomains = async (): Promise<void> => {
    try {
      const disabled = await updateWhitelistedDomains([])
 
      /* istanbul ignore else */ if (disabled) {
        refetchAuthSettings()
        showSuccess(getString('authSettings.whitelistedDomainsDisabled'), 5000)
      }
    } catch (e) {
      /* istanbul ignore next */ showError(getRBACErrorMessage(e), 5000)
    }
  }
 
  const { openDialog: confirmWhitelistedDomainsDisable } = useConfirmationDialog({
    titleText: getString('authSettings.disableWhitelistedDomains'),
    contentText: getString('authSettings.confirmDisableWhitelistedDomains'),
    confirmButtonText: getString('confirm'),
    cancelButtonText: getString('cancel'),
    onCloseDialog: isConfirmed => {
      /* istanbul ignore else */ if (isConfirmed) {
        disableWhitelistedDomains()
      }
    }
  })
 
  const onChangeWhitelistedDomains = (e: React.FormEvent<HTMLInputElement>): void => {
    const enable = e.currentTarget.checked
 
    Eif (emailRestrictionsEnabled && !enable) {
      confirmWhitelistedDomainsDisable()
    } else if (!emailRestrictionsEnabled && enable) {
      openRestrictEmailDomainsModal()
    }
  }
 
  return (
    <Container margin="xlarge">
      <Card className={css.card}>
        <Switch
          labelElement={
            <Text inline color={Color.BLACK} font={{ weight: 'bold', size: 'normal' }}>
              {getString(
                emailRestrictionsEnabled
                  ? 'authSettings.allowUsersWithEmails'
                  : 'authSettings.restrictUsersToEmailDomains'
              )}
            </Text>
          }
          checked={emailRestrictionsEnabled}
          onChange={onChangeWhitelistedDomains}
          disabled={!canEdit || updatingWhitelistedDomains}
          data-testid="toggle-restrict-email-domains"
        />
        {emailRestrictionsEnabled && (
          <TagInput
            disabled
            values={whitelistedDomains}
            rightElement={
              <Button
                variation={ButtonVariation.ICON}
                icon="Edit"
                onClick={openRestrictEmailDomainsModal}
                disabled={!canEdit}
                data-testid="update-restrict-email-domains"
              />
            }
            className={css.input}
            tagProps={{ minimal: true }}
          />
        )}
      </Card>
    </Container>
  )
}
 
export default RestrictEmailDomains