All files / modules/45-projects-orgs/modals/ProjectModal/views CreateProject.tsx

100% Statements 40/40
87.88% Branches 29/33
100% Functions 4/4
100% Lines 39/39

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              17x 17x 17x 17x 17x   17x   17x 17x 17x 17x 17x 17x           17x 6x 6x 6x 6x 6x 6x 6x 6x 6x           6x 6x             6x   6x 8x 8x           6x 1x               1x   1x 1x                 1x 1x             6x 3x           6x                                       17x  
/*
 * 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, { useEffect, useState } from 'react'
import { useParams } from 'react-router-dom'
import { StepProps, SelectOption, ModalErrorHandlerBinding, useToaster } from '@wings-software/uicore'
import { pick } from 'lodash-es'
import { useGetOrganizationList, usePostProject } from 'services/cd-ng'
import type { Project } from 'services/cd-ng'
import { useStrings } from 'framework/strings'
import type { OrgPathProps } from '@common/interfaces/RouteInterfaces'
import { PageSpinner } from '@common/components'
import { useQueryParams } from '@common/hooks'
import useRBACError from '@rbac/utils/useRBACError/useRBACError'
import { useTelemetry } from '@common/hooks/useTelemetry'
import { Category, ProjectActions } from '@common/constants/TrackingConstants'
import ProjectForm from './ProjectForm'
 
interface CreateModalData {
  modules?: Project['modules']
}
 
const CreateProject: React.FC<StepProps<Project> & CreateModalData> = props => {
  const { accountId, orgIdentifier: orgIdPathParam } = useParams<OrgPathProps>()
  const { orgIdentifier: orgIdQueryParam } = useQueryParams<OrgPathProps>()
  const { getRBACErrorMessage } = useRBACError()
  const orgIdentifier = orgIdPathParam || orgIdQueryParam
  const { nextStep, modules } = props
  const { getString } = useStrings()
  const { showSuccess } = useToaster()
  const { trackEvent } = useTelemetry()
  const { mutate: createProject, loading: saving } = usePostProject({
    queryParams: {
      accountIdentifier: accountId,
      orgIdentifier: ''
    }
  })
  const [modalErrorHandler, setModalErrorHandler] = useState<ModalErrorHandlerBinding>()
  const { data: orgData } = useGetOrganizationList({
    queryParams: {
      accountIdentifier: accountId,
      pageSize: 200
    }
  })
 
  let defaultOrg = ''
  const organizations: SelectOption[] =
    orgData?.data?.content?.map(org => {
      if (org.harnessManaged) defaultOrg = org.organization.identifier
      return {
        label: org.organization.name,
        value: org.organization.identifier
      }
    }) || []
 
  const onComplete = async (values: Project): Promise<void> => {
    const dataToSubmit: Project = pick<Project, keyof Project>(values, [
      'name',
      'orgIdentifier',
      'color',
      'description',
      'identifier',
      'tags'
    ])
    ;(dataToSubmit as Project)['modules'] = values.modules || []
 
    try {
      await createProject(
        { project: dataToSubmit },
        {
          queryParams: {
            accountIdentifier: accountId,
            orgIdentifier: values?.orgIdentifier || /* istanbul ignore next */ ''
          }
        }
      )
      nextStep?.(dataToSubmit)
      showSuccess(getString('projectsOrgs.projectCreateSuccess'))
    } catch (e) {
      /* istanbul ignore next */
      modalErrorHandler?.showDanger(getRBACErrorMessage(e))
    }
  }
 
  useEffect(() => {
    trackEvent(ProjectActions.OpenCreateProjectModal, {
      category: Category.PROJECT
    })
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [])
 
  return (
    <>
      <ProjectForm
        disableSelect={false}
        enableEdit={true}
        disableSubmit={saving}
        initialOrgIdentifier={orgIdentifier || defaultOrg}
        initialModules={modules}
        organizationItems={organizations}
        title={getString('projectsOrgs.aboutProject')}
        saveTitle={getString('saveAndContinue')}
        saveIcon="chevron-right"
        setModalErrorHandler={setModalErrorHandler}
        onComplete={onComplete}
      />
      {saving ? <PageSpinner message={getString('projectsOrgs.createProjectLoader')} /> : null}
    </>
  )
}
 
export default CreateProject