All files / modules/45-projects-orgs/pages/projects ProjectsPage.tsx

87.69% Statements 57/65
54.64% Branches 53/97
66.67% Functions 14/21
87.69% Lines 57/65

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 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230              1x 1x 1x                         1x 1x   1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x 1x   1x 1x     1x 20x 19x 19x 19x 19x 19x 19x 19x 19x   19x 7x               19x         19x   19x   7x               19x 7x     14x               19x 7x     19x                   19x 4x     19x     2x 2x       19x 1x     19x   19x 1x     19x                           2x                           18x                                         36x                                                                                                   1x  
/*
 * 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, useMemo, useEffect } from 'react'
import { useHistory, useParams } from 'react-router-dom'
import {
  Layout,
  SelectOption,
  ExpandingSearchInput,
  Container,
  GridListToggle,
  Views,
  ButtonVariation,
  DropDown,
  Page,
  ButtonSize
} from '@wings-software/uicore'
 
import { useQueryParams } from '@common/hooks'
import { useGetOrganizationList, useGetProjectAggregateDTOList } from 'services/cd-ng'
import type { Project } from 'services/cd-ng'
import { useProjectModal } from '@projects-orgs/modals/ProjectModal/useProjectModal'
import { useCollaboratorModal } from '@projects-orgs/modals/ProjectModal/useCollaboratorModal'
import routes from '@common/RouteDefinitions'
import { useStrings } from 'framework/strings'
import { useToaster } from '@common/components'
import { useDocumentTitle } from '@common/hooks/useDocumentTitle'
import type { AccountPathProps, OrgPathProps } from '@common/interfaces/RouteInterfaces'
import { NGBreadcrumbs } from '@common/components/NGBreadcrumbs/NGBreadcrumbs'
import RbacButton from '@rbac/components/Button/Button'
import { FeatureIdentifier } from 'framework/featureStore/FeatureIdentifier'
import ProjectsListView from './views/ProjectListView/ProjectListView'
import ProjectsGridView from './views/ProjectGridView/ProjectGridView'
import ProjectsEmptyState from './projects-empty-state.png'
import css from './ProjectsPage.module.scss'
 
enum OrgFilter {
  ALL = '$$ALL$$'
}
 
const ProjectsListPage: React.FC = () => {
  const { accountId } = useParams<AccountPathProps>()
  const { orgIdentifier } = useQueryParams<OrgPathProps>()
  const { verify } = useQueryParams<{ verify?: boolean }>()
  const { getString } = useStrings()
  useDocumentTitle(getString('projectsText'))
  const [view, setView] = useState(Views.GRID)
  const [searchParam, setSearchParam] = useState<string>()
  const [page, setPage] = useState(0)
  const history = useHistory()
 
  const allOrgsSelectOption: SelectOption = useMemo(
    () => ({
      label: getString('all'),
      value: OrgFilter.ALL
    }),
    // eslint-disable-next-line react-hooks/exhaustive-deps
    []
  )
 
  const { data: orgsData } = useGetOrganizationList({
    queryParams: {
      accountIdentifier: accountId
    }
  })
  const { showSuccess } = useToaster()
 
  useEffect(
    () => {
      Iif (verify) {
        showSuccess(getString('common.banners.trial.success'))
      }
    },
    // eslint-disable-next-line react-hooks/exhaustive-deps
    [verify]
  )
 
  const organizations: SelectOption[] = useMemo(() => {
    return [
      allOrgsSelectOption,
      ...(orgsData?.data?.content?.map(org => {
        return {
          label: org.organization.name,
          value: org.organization.identifier
        }
      }) || [])
    ]
  }, [orgsData?.data?.content, orgIdentifier, allOrgsSelectOption])
 
  React.useEffect(() => {
    setPage(0)
  }, [searchParam, orgIdentifier])
 
  const { data, loading, refetch, error } = useGetProjectAggregateDTOList({
    queryParams: {
      accountIdentifier: accountId,
      orgIdentifier,
      searchTerm: searchParam,
      pageIndex: page,
      pageSize: 100
    },
    debounce: 300
  })
  const projectCreateSuccessHandler = (): void => {
    refetch()
  }
 
  const { openProjectModal, closeProjectModal } = useProjectModal({
    onSuccess: projectCreateSuccessHandler,
    onWizardComplete: () => {
      closeProjectModal()
      projectCreateSuccessHandler()
    }
  })
 
  const showEditProject = (project: Project): void => {
    openProjectModal(project)
  }
 
  const { openCollaboratorModal } = useCollaboratorModal()
 
  const showCollaborators = (project: Project): void => {
    openCollaboratorModal({ projectIdentifier: project.identifier, orgIdentifier: project.orgIdentifier || 'default' })
  }
 
  return (
    <Container className={css.projectsPage} height="inherit">
      <Page.Header breadcrumbs={<NGBreadcrumbs />} title={getString('projectsText')} />
      {data?.data?.totalItems || searchParam || loading || error || orgIdentifier ? (
        <Layout.Horizontal spacing="large" className={css.header}>
          <RbacButton
            featuresProps={{
              featuresRequest: {
                featureNames: [FeatureIdentifier.MULTIPLE_PROJECTS]
              }
            }}
            variation={ButtonVariation.PRIMARY}
            text={getString('projectsOrgs.newProject')}
            icon="plus"
            onClick={() => openProjectModal()}
          />
          <DropDown
            disabled={loading}
            filterable={false}
            className={css.orgDropdown}
            items={organizations}
            value={orgIdentifier || OrgFilter.ALL}
            onChange={item => {
              history.push({
                pathname: routes.toProjects({ accountId }),
                search: item.value !== OrgFilter.ALL ? `?orgIdentifier=${item.value.toString()}` : undefined
              })
            }}
            getCustomLabel={item => getString('projectsOrgs.tabOrgs', { name: item.label })}
          />
          <div style={{ flex: 1 }}></div>
          <ExpandingSearchInput
            alwaysExpanded
            onChange={text => {
              setSearchParam(text.trim())
            }}
            width={300}
            className={css.expandSearch}
          />
          <GridListToggle initialSelectedView={Views.GRID} onViewToggle={setView} />
        </Layout.Horizontal>
      ) : null}
      <Page.Body
        loading={loading}
        retryOnError={() => refetch()}
        error={(error?.data as Error)?.message || error?.message}
        noData={
          !searchParam && openProjectModal
            ? {
                when: () => !data?.data?.content?.length,
                image: ProjectsEmptyState,
                imageClassName: css.imageClassName,
                messageTitle: getString('projectsOrgs.youHaveNoProjects'),
                message: getString('projectDescription'),
                button: (
                  <RbacButton
                    featuresProps={{
                      featuresRequest: {
                        featureNames: [FeatureIdentifier.MULTIPLE_PROJECTS]
                      }
                    }}
                    size={ButtonSize.LARGE}
                    variation={ButtonVariation.PRIMARY}
                    text={getString('projectsOrgs.createAProject')}
                    onClick={() => openProjectModal?.()}
                  />
                )
              }
            : {
                when: () => !data?.data?.content?.length,
                image: ProjectsEmptyState,
                imageClassName: css.imageClassName,
                messageTitle: getString('noProjects')
              }
        }
      >
        {view === Views.GRID ? (
          <ProjectsGridView
            data={data}
            showEditProject={showEditProject}
            collaborators={showCollaborators}
            reloadPage={refetch}
            gotoPage={(pageNumber: number) => setPage(pageNumber)}
          />
        ) : null}
        {view === Views.LIST ? (
          <ProjectsListView
            data={data}
            showEditProject={showEditProject}
            collaborators={showCollaborators}
            reloadPage={refetch}
            gotoPage={(pageNumber: number) => setPage(pageNumber)}
          />
        ) : null}
      </Page.Body>
    </Container>
  )
}
 
export default ProjectsListPage