All files / modules/33-auth-settings/modals/RestrictEmailDomains/views RestrictEmailDomainsForm.tsx

100% Statements 27/27
80% Branches 4/5
100% Functions 5/5
100% Lines 27/27

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              2x 2x 2x 2x                     2x 2x   2x 2x 2x                       2x 2x 2x 2x 2x 2x   2x           2x 1x 1x   1x 1x 1x             2x                         1x         1x       4x                                           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 from 'react'
import * as yup from 'yup'
import { useParams } from 'react-router-dom'
import {
  Layout,
  Text,
  Formik,
  FormikForm,
  FormInput,
  Button,
  ModalErrorHandler,
  ModalErrorHandlerBinding,
  ButtonVariation
} from '@wings-software/uicore'
import { Color } from '@harness/design-system'
import { useToaster } from '@common/components'
import type { AccountPathProps } from '@common/interfaces/RouteInterfaces'
import { useUpdateWhitelistedDomains } from 'services/cd-ng'
import { useStrings } from 'framework/strings'
import useRBACError from '@rbac/utils/useRBACError/useRBACError'
 
interface Props {
  onSubmit?: () => void
  onCancel: () => void
  whitelistedDomains: string[]
}
 
interface FormValues {
  domains: string[]
}
 
const RestrictEmailDomainsForm: React.FC<Props> = ({ onSubmit, onCancel, whitelistedDomains }) => {
  const { getRBACErrorMessage } = useRBACError()
  const { getString } = useStrings()
  const { showSuccess } = useToaster()
  const { accountId } = useParams<AccountPathProps>()
  const [modalErrorHandler, setModalErrorHandler] = React.useState<ModalErrorHandlerBinding>()
 
  const { mutate: updateWhitelistedDomains, loading: updatingWhitelistedDomains } = useUpdateWhitelistedDomains({
    queryParams: {
      accountIdentifier: accountId
    }
  })
 
  const handleSubmit = async (values: FormValues): Promise<void> => {
    try {
      const response = await updateWhitelistedDomains(values.domains)
 
      /* istanbul ignore else */ if (response) {
        showSuccess(getString('authSettings.WhitelistedDomainsUpdated'), 5000)
        onSubmit?.()
      }
    } catch (e) {
      /* istanbul ignore next */ modalErrorHandler?.showDanger(getRBACErrorMessage(e))
    }
  }
 
  return (
    <Layout.Vertical padding={{ left: 'huge', right: 'huge' }}>
      <ModalErrorHandler bind={setModalErrorHandler} />
      <Text color={Color.GREY_900} font={{ size: 'medium', weight: 'semi-bold' }} margin={{ bottom: 'xxlarge' }}>
        {getString('authSettings.allowLoginFromTheseDomains')}
      </Text>
      <Formik
        formName="restrictEmailDomainsForm"
        initialValues={{
          domains: whitelistedDomains
        }}
        validationSchema={yup.object().shape({
          domains: yup.array().test({
            test: arr => arr.length !== 0,
            message: getString('authSettings.domainNameRequired')
          })
        })}
        onSubmit={values => {
          handleSubmit(values)
        }}
      >
        {() => (
          <FormikForm>
            <FormInput.MultiInput
              name="domains"
              tagsProps={{ placeholder: getString('authSettings.typeAndPressEnterToAddADomain') }}
            />
            <Layout.Horizontal margin={{ top: 'xxxlarge', bottom: 'xlarge' }}>
              <Button
                text={getString('save')}
                variation={ButtonVariation.PRIMARY}
                type="submit"
                margin={{ right: 'small' }}
                disabled={updatingWhitelistedDomains}
              />
              <Button text={getString('cancel')} onClick={onCancel} variation={ButtonVariation.TERTIARY} />
            </Layout.Horizontal>
          </FormikForm>
        )}
      </Formik>
    </Layout.Vertical>
  )
}
 
export default RestrictEmailDomainsForm