All files / modules/20-rbac/modals/ApiKeyModal/views ApiKeyForm.tsx

100% Statements 37/37
89.66% Branches 26/29
100% Functions 4/4
100% Lines 37/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              11x 11x 11x                   11x 11x 11x 11x 11x 11x   11x 11x 11x 11x                     11x 4x 4x 4x 4x     4x 4x 4x       4x 2x 2x 1x 1x 1x 1x     1x 1x 1x 1x               4x                                                 2x 2x       10x                                                     11x  
/*
 * 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 * as Yup from 'yup'
import {
  Layout,
  Formik,
  Text,
  Button,
  Container,
  ModalErrorHandler,
  ModalErrorHandlerBinding,
  ButtonVariation
} from '@wings-software/uicore'
import { Color } from '@harness/design-system'
import { Form } from 'formik'
import { useParams } from 'react-router-dom'
import useRBACError from '@rbac/utils/useRBACError/useRBACError'
import { useStrings } from 'framework/strings'
import { ApiKeyDTO, TokenDTO, useCreateApiKey, useUpdateApiKey } from 'services/cd-ng'
import type { ProjectPathProps, ServiceAccountPathProps } from '@common/interfaces/RouteInterfaces'
import { IdentifierSchema, NameSchema } from '@common/utils/Validation'
import { NameIdDescriptionTags } from '@common/components/NameIdDescriptionTags/NameIdDescriptionTags'
import { useToaster } from '@common/exports'
import css from '../useApiKeyModal.module.scss'
 
interface ApiKeyModalData {
  data?: ApiKeyDTO
  isEdit?: boolean
  apiKeyType?: TokenDTO['apiKeyType']
  parentIdentifier?: string
  onSubmit?: (data: ApiKeyDTO) => void
  onClose?: () => void
}
 
const ApiKeyForm: React.FC<ApiKeyModalData> = ({ data, isEdit, onSubmit, apiKeyType, parentIdentifier, onClose }) => {
  const { getString } = useStrings()
  const { getRBACErrorMessage } = useRBACError()
  const [modalErrorHandler, setModalErrorHandler] = useState<ModalErrorHandlerBinding>()
  const { accountId, projectIdentifier, orgIdentifier, serviceAccountIdentifier } = useParams<
    ProjectPathProps & ServiceAccountPathProps
  >()
  const { showSuccess } = useToaster()
  const { mutate: createApiKey, loading: saving } = useCreateApiKey({ queryParams: { accountIdentifier: accountId } })
  const { mutate: editApiKey, loading: updating } = useUpdateApiKey({
    identifier: data?.identifier || /* istanbul ignore next */ ''
  })
 
  const handleSubmit = async (values: ApiKeyDTO): Promise<void> => {
    try {
      if (isEdit) {
        const updated = await editApiKey({ ...values, accountIdentifier: accountId })
        /* istanbul ignore else */ if (updated) {
          showSuccess(getString('rbac.apiKey.form.editSuccess', { name: values.name }))
          onSubmit?.(values)
        }
      } else {
        const created = await createApiKey({ ...values })
        /* istanbul ignore else */ if (created) {
          showSuccess(getString('rbac.apiKey.form.createSuccess', { name: values.name }))
          onSubmit?.(values)
        }
      }
    } catch (e) {
      /* istanbul ignore next */
      modalErrorHandler?.showDanger(getRBACErrorMessage(e))
    }
  }
  return (
    <Layout.Vertical padding={{ bottom: 'xxxlarge', right: 'xxxlarge', left: 'xxxlarge' }}>
      <Layout.Vertical spacing="large">
        <Text color={Color.GREY_900} font={{ size: 'medium', weight: 'semi-bold' }}>
          {isEdit ? getString('rbac.apiKey.editLabel') : getString('rbac.apiKey.createLabel')}
        </Text>
        <Formik<ApiKeyDTO>
          initialValues={{
            identifier: '',
            name: '',
            description: '',
            tags: {},
            accountIdentifier: accountId,
            orgIdentifier,
            projectIdentifier,
            parentIdentifier: parentIdentifier || serviceAccountIdentifier,
            apiKeyType: apiKeyType || 'SERVICE_ACCOUNT',
            ...data
          }}
          formName="apiKeyForm"
          validationSchema={Yup.object().shape({
            name: NameSchema(),
            identifier: IdentifierSchema()
          })}
          onSubmit={values => {
            modalErrorHandler?.hide()
            handleSubmit(values)
          }}
        >
          {formikProps => {
            return (
              <Form>
                <Container className={css.form}>
                  <ModalErrorHandler bind={setModalErrorHandler} />
                  <NameIdDescriptionTags
                    formikProps={formikProps}
                    identifierProps={{ isIdentifierEditable: !isEdit }}
                  />
                </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>
      </Layout.Vertical>
    </Layout.Vertical>
  )
}
 
export default ApiKeyForm