All files / modules/45-projects-orgs/modals/OrganizationModal useOrganizationModal.tsx

93.75% Statements 30/32
90% Branches 9/10
85.71% Functions 6/7
93.55% Lines 29/31

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              1x 1x 1x 1x 1x   1x 1x 1x 1x 1x                     1x   1x 10x 10x 10x 10x   2x                                           1x 1x                   1x 1x               10x   2x 1x 1x 1x 2x         10x   2x          
/*
 * 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 { Classes, Dialog } from '@blueprintjs/core'
import { Button, StepWizard } from '@wings-software/uicore'
import { useModalHook } from '@harness/use-modal'
import React, { useState, useCallback } from 'react'
import cx from 'classnames'
import type { Organization } from 'services/cd-ng'
import { useStrings } from 'framework/strings'
import StepAboutOrganization from './views/StepAboutOrganization'
import EditOrganization from './views/EditOrganization'
import { OrgCollaboratorsStep } from '../ProjectModal/views/Collaborators'
import css from './useOrganizationModal.module.scss'
 
export interface UseOrganizationModalArgs {
  onSuccess: () => void
}
 
export interface UseOrganizationModalResult {
  openOrganizationModal: (org?: Organization) => void
  closeOrganizationModal: () => void
}
 
const Views = { CREATE: 1, EDIT: 2 }
 
export const useOrganizationModal = ({ onSuccess }: UseOrganizationModalArgs): UseOrganizationModalResult => {
  const [view, setView] = useState(Views.CREATE)
  const { getString } = useStrings()
  const [orgData, setOrgData] = useState<Organization>()
  const [openModal, hideModal] = useModalHook(
    () => (
      <Dialog
        isOpen={true}
        enforceFocus={false}
        onClose={() => {
          setView(Views.CREATE)
          hideModal()
        }}
        className={cx(css.dialog, Classes.DIALOG, {
          [css.create]: view === Views.CREATE,
          [css.edit]: view === Views.EDIT
        })}
      >
        {view === Views.CREATE ? (
          <StepWizard<Organization> stepClassName={css.stepClass} onCompleteWizard={hideModal}>
            <StepAboutOrganization name={getString('projectsOrgs.aboutTitle')} onSuccess={onSuccess} />
            <OrgCollaboratorsStep name={getString('projectsOrgs.collaboratorsTitle')} />
          </StepWizard>
        ) : null}
        {view === Views.EDIT ? (
          <EditOrganization
            name={getString('projectsOrgs.aboutTitle')}
            onSuccess={() => {
              hideModal()
              onSuccess()
            }}
            identifier={orgData?.identifier}
          />
        ) : null}
        <Button
          minimal
          icon="cross"
          iconProps={{ size: 18 }}
          onClick={() => {
            setView(Views.CREATE)
            hideModal()
          }}
          className={css.crossIcon}
        />
      </Dialog>
    ),
    [view, orgData]
  )
  const open = useCallback(
    (_org?: Organization) => {
      if (_org) {
        setOrgData(_org)
        setView(Views.EDIT)
      } else setView(Views.CREATE)
      openModal()
    },
    [openModal]
  )
 
  return {
    openOrganizationModal: (org?: Organization) => {
      open(org)
    },
    closeOrganizationModal: hideModal
  }
}