All files / modules/20-rbac/modals/LinkToSSOProviderModal/views UnlinkSSOProviderForm.tsx

90.63% Statements 29/32
43.75% Branches 7/16
80% Functions 4/5
90.63% Lines 29/32

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              11x 11x                         11x 11x 11x 11x   11x 11x 11x                     11x 4x 4x 4x 4x 4x 4x 4x   4x                   4x 1x 1x 1x 1x 1x 1x                 4x                   1x       6x                                                                                   11x  
/*
 * Copyright 2021 Harness Inc. All rights reserved.
 * Use of this source code is governed by the PolyForm Free Trial 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/05/PolyForm-Free-Trial-1.0.0.txt.
 */
 
import React, { useState } from 'react'
import {
  Button,
  Container,
  Formik,
  FormikForm as Form,
  Layout,
  ModalErrorHandler,
  ModalErrorHandlerBinding,
  Text,
  FormInput,
  ButtonVariation,
  shouldShowError
} from '@wings-software/uicore'
import { Color } from '@harness/design-system'
import { useParams } from 'react-router-dom'
import useRBACError from '@rbac/utils/useRBACError/useRBACError'
import { useToaster } from '@common/components'
import type { ProjectPathProps } from '@common/interfaces/RouteInterfaces'
import { useStrings } from 'framework/strings'
import { UserGroupDTO, useUnlinkSsoGroup } from 'services/cd-ng'
import css from '../useLinkToSSOProviderModal.module.scss'
 
interface UnlinkSSOProviderModalData {
  userGroupData: UserGroupDTO
  onSubmit?: () => void
}
 
export interface UnlinkSSOProviderFormData {
  retainMembers: boolean
}
 
const UnlinkSSOProviderForm: React.FC<UnlinkSSOProviderModalData> = props => {
  const { onSubmit, userGroupData } = props
  const { getRBACErrorMessage } = useRBACError()
  const { accountId, projectIdentifier, orgIdentifier } = useParams<ProjectPathProps>()
  const { getString } = useStrings()
  const { showSuccess } = useToaster()
  const [modalErrorHandler, setModalErrorHandler] = useState<ModalErrorHandlerBinding>()
  const [retainMembers, setRetainMembers] = useState(false)
 
  const { mutate: unlinkSsoGroup, loading } = useUnlinkSsoGroup({
    userGroupId: userGroupData.identifier,
    queryParams: {
      accountIdentifier: accountId,
      orgIdentifier,
      projectIdentifier,
      retainMembers: retainMembers
    }
  })
 
  const handleOnSubmit = async (): Promise<void> => {
    modalErrorHandler?.hide()
    try {
      const created = await unlinkSsoGroup({ headers: { 'content-type': 'application/json' } } as any)
      Eif (created) {
        showSuccess(getString('rbac.userGroupPage.successMessage', { name: userGroupData.ssoGroupName }))
        onSubmit?.()
      }
    } catch (err) {
      if (shouldShowError(err)) {
        modalErrorHandler?.showDanger(getRBACErrorMessage(err))
      }
    }
  }
 
  return (
    <Layout.Vertical padding="xlarge">
      <Layout.Vertical spacing="large">
        <Text color={Color.BLACK} font="medium">
          {getString('rbac.userDetails.linkToSSOProviderModal.delinkLabel')}
        </Text>
        <Formik<UnlinkSSOProviderFormData>
          formName="UnlinkSSOProviderForm"
          initialValues={{ retainMembers: false }}
          onSubmit={() => {
            handleOnSubmit()
          }}
        >
          {() => {
            return (
              <Form>
                <Container margin={{ bottom: 'medium' }}>
                  <Container padding={{ bottom: 'medium' }}>
                    <ModalErrorHandler bind={setModalErrorHandler} />
                  </Container>
                  <Layout.Vertical spacing="medium">
                    <Text font={{ weight: 'semi-bold' }}>
                      {getString('rbac.userDetails.linkToSSOProviderModal.delinkText', {
                        name: userGroupData.name,
                        ssoName: userGroupData.linkedSsoDisplayName
                      })}
                    </Text>
                    <FormInput.CheckBox
                      disabled={loading}
                      className={css.checkbox}
                      name="retainMembers"
                      label={getString('rbac.userDetails.linkToSSOProviderModal.retainMembersLabel')}
                      onChange={e => {
                        setRetainMembers(e.currentTarget.checked)
                      }}
                    />
                  </Layout.Vertical>
                </Container>
                <Layout.Horizontal>
                  <Button
                    variation={ButtonVariation.PRIMARY}
                    data-testid="submitLinkSSOProvider"
                    text={getString('save')}
                    type="submit"
                    disabled={loading}
                  />
                </Layout.Horizontal>
              </Form>
            )
          }}
        </Formik>
      </Layout.Vertical>
    </Layout.Vertical>
  )
}
 
export default UnlinkSSOProviderForm