All files / modules/20-rbac/modals/ResourceGroupModal/views ResourceGroupModalForm.tsx

100% Statements 37/37
82.14% Branches 23/28
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 133 134 135 136 137 138              10x 10x                     10x 10x 10x 10x           10x 10x   10x 10x 10x               10x 8x 8x 8x 8x 8x 8x 8x             8x               8x 2x     2x 2x 1x 1x 1x 1x     1x 1x 1x 1x               8x                                           2x 2x       14x                                             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,
  ButtonVariation
} from '@wings-software/uicore'
import * as Yup from 'yup'
import { useParams } from 'react-router-dom'
import { NameIdDescriptionTags, useToaster } from '@common/components'
import {
  ResourceGroupV2,
  useCreateResourceGroupV2,
  useUpdateResourceGroupV2,
  ResourceGroupV2Request
} from 'services/resourcegroups'
import { useStrings } from 'framework/strings'
import useRBACError from '@rbac/utils/useRBACError/useRBACError'
import type { ProjectPathProps } from '@common/interfaces/RouteInterfaces'
import { DEFAULT_COLOR } from '@common/constants/Utils'
import { IdentifierSchema, NameSchema } from '@common/utils/Validation'
import css from './ResourceGroupModal.module.scss'
interface ResourceGroupModalData {
  data?: ResourceGroupV2
  editMode: boolean
  onSubmit?: (resourceGroup: ResourceGroupV2) => void
  onCancel?: () => void
}
 
const ResourceGroupForm: React.FC<ResourceGroupModalData> = props => {
  const { onSubmit, data, editMode, onCancel } = props
  const { accountId, orgIdentifier, projectIdentifier } = useParams<ProjectPathProps>()
  const { getRBACErrorMessage } = useRBACError()
  const { getString } = useStrings()
  const { showSuccess } = useToaster()
  const [modalErrorHandler, setModalErrorHandler] = useState<ModalErrorHandlerBinding>()
  const { mutate: createResourceGroup, loading: saving } = useCreateResourceGroupV2({
    queryParams: {
      accountIdentifier: accountId,
      orgIdentifier,
      projectIdentifier
    }
  })
  const { mutate: updateResourceGroup, loading: updating } = useUpdateResourceGroupV2({
    identifier: data?.identifier || '',
    queryParams: {
      accountIdentifier: accountId,
      orgIdentifier,
      projectIdentifier
    }
  })
  const handleSubmit = async (values: ResourceGroupV2): Promise<void> => {
    const dataToSubmit: ResourceGroupV2Request = {
      resourceGroup: values
    }
    try {
      if (!editMode) {
        const created = await createResourceGroup(dataToSubmit)
        Eif (created) {
          showSuccess(getString('rbac.resourceGroup.createSuccess'))
          onSubmit?.(dataToSubmit.resourceGroup)
        }
      } else {
        const updated = await updateResourceGroup(dataToSubmit)
        Eif (updated) {
          showSuccess(getString('rbac.resourceGroup.updateSuccess'))
          onSubmit?.(dataToSubmit.resourceGroup)
        }
      }
    } catch (e) {
      /* istanbul ignore next */
      modalErrorHandler?.showDanger(getRBACErrorMessage(e))
    }
  }
  return (
    <Formik<ResourceGroupV2>
      initialValues={{
        identifier: '',
        name: '',
        description: '',
        tags: {},
        color: DEFAULT_COLOR,
        accountIdentifier: accountId,
        orgIdentifier,
        projectIdentifier,
        includedScopes: [],
        resourceFilter: {},
        ...(editMode && data)
      }}
      formName="resourceGroupModalForm"
      validationSchema={Yup.object().shape({
        name: NameSchema(),
        identifier: IdentifierSchema(),
        color: Yup.string().trim().required()
      })}
      onSubmit={values => {
        modalErrorHandler?.hide()
        handleSubmit(values)
      }}
    >
      {formikProps => {
        return (
          <Form>
            <Container className={css.modal}>
              <ModalErrorHandler bind={setModalErrorHandler} />
              <NameIdDescriptionTags formikProps={formikProps} identifierProps={{ isIdentifierEditable: !editMode }} />
              <FormInput.ColorPicker label={getString('rbac.resourceGroup.color')} name="color" />
            </Container>
            <Layout.Horizontal spacing="small">
              <Button
                variation={ButtonVariation.PRIMARY}
                text={getString('save')}
                type="submit"
                disabled={saving || updating}
              />
              <Button text={getString('cancel')} variation={ButtonVariation.TERTIARY} onClick={onCancel} />
            </Layout.Horizontal>
          </Form>
        )
      }}
    </Formik>
  )
}
 
export default ResourceGroupForm