All files / modules/20-rbac/modals/ServiceAccountModal/views ServiceAccountForm.tsx

89.19% Statements 33/37
62.5% Branches 15/24
100% Functions 4/4
89.19% Lines 33/37

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              10x 10x                           10x 10x 10x 10x 10x 10x   10x 10x 10x                 10x 2x 2x 2x 2x 2x 2x 2x               2x                 2x 1x 1x 1x             1x 1x 1x 1x               2x                                     1x 1x       5x                                                           10x  
/*
 * 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, { useState } from 'react'
import {
  Button,
  Container,
  Formik,
  FormikForm as Form,
  FormInput,
  Layout,
  ModalErrorHandler,
  ModalErrorHandlerBinding,
  Text,
  TextInput,
  ButtonVariation,
  Label
} from '@wings-software/uicore'
import * as Yup from 'yup'
import { useParams } from 'react-router-dom'
import useRBACError from '@rbac/utils/useRBACError/useRBACError'
import { useToaster } from '@common/components'
import { DescriptionTags } from '@common/components/NameIdDescriptionTags/NameIdDescriptionTags'
import { useStrings } from 'framework/strings'
import type { ProjectPathProps } from '@common/interfaces/RouteInterfaces'
import { NameSchema, IdentifierSchema } from '@common/utils/Validation'
import { ServiceAccountDTO, useCreateServiceAccount, useUpdateServiceAccount } from 'services/cd-ng'
import css from '@rbac/modals/ServiceAccountModal/useServiceAccountModal.module.scss'
 
interface ServiceAccountModalData {
  data?: ServiceAccountDTO
  isEdit?: boolean
  onSubmit?: (serviceAccount: ServiceAccountDTO) => void
  onClose?: () => void
}
 
const ServiceAccountForm: React.FC<ServiceAccountModalData> = props => {
  const { data: serviceAccountData, onSubmit, isEdit, onClose } = props
  const { accountId, orgIdentifier, projectIdentifier } = useParams<ProjectPathProps>()
  const { getRBACErrorMessage } = useRBACError()
  const { getString } = useStrings()
  const { showSuccess } = useToaster()
  const [modalErrorHandler, setModalErrorHandler] = useState<ModalErrorHandlerBinding>()
  const { mutate: createServiceAccount, loading: saving } = useCreateServiceAccount({
    queryParams: {
      accountIdentifier: accountId,
      orgIdentifier,
      projectIdentifier
    }
  })
 
  const { mutate: editServiceAccount, loading: updating } = useUpdateServiceAccount({
    identifier: serviceAccountData?.identifier || '',
    queryParams: {
      accountIdentifier: accountId,
      orgIdentifier,
      projectIdentifier
    }
  })
 
  const handleSubmit = async (values: ServiceAccountDTO): Promise<void> => {
    const dataToSubmit = { ...values, email: values['identifier'].concat('@service.harness.io').toLowerCase() }
    try {
      Iif (isEdit) {
        const updated = await editServiceAccount(dataToSubmit)
        /* istanbul ignore else */ Iif (updated) {
          showSuccess(getString('rbac.serviceAccounts.form.editSuccess', { name: values.name }))
          onSubmit?.(values)
        }
      } else {
        const created = await createServiceAccount(dataToSubmit)
        /* istanbul ignore else */ if (created) {
          showSuccess(getString('rbac.serviceAccounts.form.createSuccess', { name: values.name }))
          onSubmit?.(values)
        }
      }
    } catch (e) {
      /* istanbul ignore next */
      modalErrorHandler?.showDanger(getRBACErrorMessage(e))
    }
  }
  return (
    <Formik
      initialValues={{
        identifier: '',
        name: '',
        description: '',
        email: '',
        tags: {},
        accountIdentifier: accountId,
        orgIdentifier,
        projectIdentifier,
        ...serviceAccountData
      }}
      formName="serviceAccountForm"
      validationSchema={Yup.object().shape({
        name: NameSchema(),
        identifier: IdentifierSchema()
      })}
      onSubmit={values => {
        modalErrorHandler?.hide()
        handleSubmit(values)
      }}
    >
      {formikProps => {
        return (
          <Form>
            <Container className={css.form}>
              <ModalErrorHandler bind={setModalErrorHandler} />
              <FormInput.InputWithIdentifier isIdentifierEditable={!isEdit} />
              <Layout.Horizontal flex={{ alignItems: 'center', justifyContent: 'flex-start' }} spacing="small">
                <Layout.Vertical>
                  <Label>{getString('email')}</Label>
                  <TextInput disabled value={formikProps.values.identifier.toLowerCase()} />
                </Layout.Vertical>
                <Text margin={{ top: 'xsmall' }}>{getString('rbac.serviceAccounts.email')}</Text>
              </Layout.Horizontal>
              <DescriptionTags formikProps={formikProps} />
            </Container>
            <Layout.Horizontal spacing="small">
              <Button
                variation={ButtonVariation.PRIMARY}
                text={getString('save')}
                type="submit"
                disabled={saving || updating}
              />
              <Button text={getString('cancel')} onClick={onClose} variation={ButtonVariation.TERTIARY} />
            </Layout.Horizontal>
          </Form>
        )
      }}
    </Formik>
  )
}
 
export default ServiceAccountForm