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

86.67% Statements 52/60
63.64% Branches 42/66
90.91% Functions 10/11
86.44% Lines 51/59

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 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157              16x 16x 16x 16x     16x 16x 16x 16x 16x 16x 16x 16x 16x 16x                           16x         83x 83x 83x 83x   83x                     83x 83x 1x 1x   83x 1x     1x 1x         83x 83x   5x                                   3x 1x                                   2x 2x                   2x 1x 1x   2x 2x 2x 2x                 83x   4x 4x 4x 2x 2x 4x         83x 4x        
/*
 * Copyright 2022 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, { useCallback, useState } from 'react'
import { StepWizard, Button } from '@wings-software/uicore'
import { useModalHook } from '@harness/use-modal'
import { Dialog, IDialogProps } from '@blueprintjs/core'
import type { ModuleName } from 'framework/types/ModuleName'
import type { Project } from 'services/cd-ng'
import { useStrings } from 'framework/strings'
import { useModuleInfo } from '@common/hooks/useModuleInfo'
import { useTelemetry } from '@common/hooks/useTelemetry'
import { ExitModalActions, Category } from '@common/constants/TrackingConstants'
import { useModuleSelectModal } from '../ModuleSelect/useModuleSelect'
import { Views } from './Constants'
import { ProjectCollaboratorsStep } from './views/Collaborators'
import StepAboutProject from './views/StepAboutProject'
import EditProject from './views/EditProject'
import css from './useProjectModal.module.scss'
 
export interface UseProjectModalProps {
  onSuccess?: (projectData?: Project) => void
  onCloseModal?: () => void
  onWizardComplete?: (projectData?: Project) => void
  module?: ModuleName
}
 
export interface UseProjectModalReturn {
  openProjectModal: (project?: Project) => void
  closeProjectModal: () => void
}
 
export const useProjectModal = ({
  onSuccess,
  onCloseModal,
  onWizardComplete
}: UseProjectModalProps): UseProjectModalReturn => {
  const [view, setView] = useState(Views.CREATE)
  const [projectData, setProjectData] = useState<Project>()
  const [refreshProjects, setRefreshProjects] = useState(false)
  const { getString } = useStrings()
 
  const modalProps: IDialogProps = {
    isOpen: true,
    enforceFocus: false,
    style: {
      width: view === Views.CREATE ? 1100 : 500,
      borderLeft: 0,
      paddingBottom: 0,
      position: 'relative',
      overflow: 'auto'
    }
  }
  const { module } = useModuleInfo()
  const { openModuleSelectModal } = useModuleSelectModal({
    onSuccess: () => onWizardComplete?.(projectData),
    onCloseModal: () => onWizardComplete?.(projectData)
  })
  const wizardCompleteHandler = async (wizardData: Project | undefined): Promise<void> => {
    Iif (!wizardData) {
      setProjectData(wizardData)
    }
    Eif (wizardData && !module) {
      openModuleSelectModal(wizardData)
    } else {
      onWizardComplete?.(wizardData)
    }
  }
  const { trackEvent } = useTelemetry()
  const [showModal, hideModal] = useModalHook(
    () => (
      <Dialog
        onClose={() => {
          if (refreshProjects) {
            onSuccess?.(projectData)
            setRefreshProjects(false)
          }
          onCloseModal?.()
          setView(Views.CREATE)
          hideModal()
        }}
        {...modalProps}
      >
        {view === Views.CREATE ? (
          <StepWizard<Project>
            icon="projects-wizard"
            iconProps={{ size: 30, padding: { bottom: 'xxlarge' } }}
            onCompleteWizard={wizardCompleteHandler}
            onStepChange={() => {
              if (!refreshProjects) {
                setRefreshProjects(true)
              }
            }}
            stepClassName={css.stepClass}
          >
            <StepAboutProject name={getString('projectsOrgs.aboutProject')} modules={projectData?.modules} />
            <ProjectCollaboratorsStep
              name={getString('projectsOrgs.invite')}
              subTitle={getString('projectsOrgs.optional')}
            />
          </StepWizard>
        ) : null}
 
        {view === Views.EDIT ? (
          <EditProject
            identifier={projectData?.identifier}
            orgIdentifier={projectData?.orgIdentifier}
            closeModal={() => {
              hideModal()
              onSuccess?.(projectData)
            }}
          />
        ) : null}
 
        <Button
          minimal
          icon="cross"
          iconProps={{ size: 18 }}
          onClick={() => {
            if (refreshProjects) {
              onSuccess?.(projectData)
              setRefreshProjects(false)
            }
            onCloseModal?.()
            setView(Views.CREATE)
            trackEvent(ExitModalActions.ExitByClose, { category: Category.PROJECT })
            hideModal()
          }}
          className={css.crossIcon}
        />
      </Dialog>
    ),
    [view, projectData, refreshProjects]
  )
 
  const open = useCallback(
    (_project?: Project) => {
      setProjectData(_project)
      const isNew = !_project || !_project.identifier
      if (!isNew) {
        setView(Views.EDIT)
      } else setView(Views.CREATE)
      showModal()
    },
    [showModal]
  )
 
  return {
    openProjectModal: (project?: Project) => open(project),
    closeProjectModal: hideModal
  }
}