All files / modules/75-cd/components/Services/ServicesListPage ServicesListPage.tsx

85.42% Statements 41/48
58.33% Branches 7/12
50% Functions 6/12
85.11% Lines 40/47

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              2x 2x 2x                 2x 2x 2x 2x 2x 2x   2x 2x 2x   2x 2x   2x 2x   2x   2x 8x 7x 7x 7x 7x 7x 7x   7x   7x   2x 1x                   1x             7x   1x                                                     7x                       7x       7x 7x     7x                                       1x 1x                                                 2x                
/*
 * Copyright 2022 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, { useCallback, useEffect, useState } from 'react'
import cx from 'classnames'
import {
  Dialog,
  Layout,
  Views,
  VisualYamlSelectedView as SelectedView,
  Container,
  GridListToggle,
  useToaster
} from '@harness/uicore'
import { useHistory, useParams } from 'react-router-dom'
import { useModalHook } from '@harness/use-modal'
import { useServiceStore } from '@cd/components/Services/common'
import { useStrings } from 'framework/strings'
import { PermissionIdentifier } from '@rbac/interfaces/PermissionIdentifier'
import { ResourceType } from '@rbac/interfaces/ResourceType'
 
import { Page } from '@common/exports'
import RbacButton from '@rbac/components/Button/Button'
import { GetServiceListQueryParams, ServiceResponseDTO, useGetServiceList } from 'services/cd-ng'
import type { ModulePathParams, ProjectPathProps } from '@common/interfaces/RouteInterfaces'
import routes from '@common/RouteDefinitions'
import { NewEditServiceModal } from '@cd/components/PipelineSteps/DeployServiceStep/DeployServiceStep'
 
import ServicesGridView from '../ServicesGridView/ServicesGridView'
import ServicesListView from '../ServicesListView/ServicesListView'
 
import css from './ServicesListPage.module.scss'
 
export const ServicesListPage: React.FC = () => {
  const { accountId, orgIdentifier, projectIdentifier, module } = useParams<ProjectPathProps & ModulePathParams>()
  const [view, setView] = useState(Views.LIST)
  const [page, setPage] = useState(0)
  const { getString } = useStrings()
  const { fetchDeploymentList } = useServiceStore()
  const [mode, setMode] = useState<SelectedView>(SelectedView.VISUAL)
  const { showError } = useToaster()
 
  const history = useHistory()
 
  const goToServiceDetails = useCallback(
    (selectedService: ServiceResponseDTO): void => {
      if (selectedService?.identifier) {
        history.push(
          routes.toServiceDetails({
            accountId,
            orgIdentifier,
            projectIdentifier,
            serviceId: selectedService.identifier,
            module
          })
        )
      } else {
        showError(getString('cd.serviceList.noIdentifier'))
      }
    },
    // eslint-disable-next-line react-hooks/exhaustive-deps
    [accountId, orgIdentifier, projectIdentifier, module]
  )
 
  const [showModal, hideModal] = useModalHook(
    () => (
      <Dialog
        isOpen={true}
        enforceFocus={false}
        canEscapeKeyClose
        canOutsideClickClose
        onClose={hideModal}
        title={getString('cd.addService')}
        isCloseButtonShown
        className={cx('padded-dialog', css.dialogStyles)}
      >
        <Container>
          <NewEditServiceModal
            data={{ name: '', identifier: '', orgIdentifier, projectIdentifier }}
            isEdit={false}
            isService
            onCreateOrUpdate={() => {
              ;(fetchDeploymentList.current as () => void)?.()
              hideModal()
            }}
            closeModal={hideModal}
          />
        </Container>
      </Dialog>
    ),
    [fetchDeploymentList, orgIdentifier, projectIdentifier, mode]
  )
 
  const queryParams: GetServiceListQueryParams = {
    accountIdentifier: accountId,
    orgIdentifier,
    projectIdentifier,
    size: 10,
    page: page
  }
 
  const {
    loading,
    data: serviceList,
    refetch
  } = useGetServiceList({
    queryParams
  })
 
  useEffect(() => {
    fetchDeploymentList.current = refetch
  }, [fetchDeploymentList, refetch])
 
  return (
    <Page.Body className={css.pageBody}>
      <>
        <Layout.Horizontal
          padding={{ left: 'xlarge', right: 'xlarge', top: 'medium' }}
          flex={{ distribution: 'space-between' }}
        >
          <RbacButton
            intent="primary"
            data-testid="add-service"
            icon="plus"
            iconProps={{ size: 10 }}
            text={getString('newService')}
            permission={{
              permission: PermissionIdentifier.EDIT_SERVICE,
              resource: {
                resourceType: ResourceType.SERVICE
              }
            }}
            onClick={() => {
              showModal()
              setMode(SelectedView.VISUAL)
            }}
          />
 
          <GridListToggle initialSelectedView={Views.LIST} onViewToggle={setView} />
        </Layout.Horizontal>
 
        <Layout.Vertical
          margin={{ left: 'xlarge', right: 'xlarge', top: 'large', bottom: 'large' }}
          className={css.container}
        >
          {view === Views.GRID ? (
            <ServicesGridView
              data={serviceList}
              loading={loading}
              onRefresh={() => refetch()}
              gotoPage={(pageNumber: number) => setPage(pageNumber)}
              onServiceSelect={async service => goToServiceDetails(service)}
            />
          ) : (
            <ServicesListView
              data={serviceList}
              loading={loading}
              onRefresh={() => refetch()}
              gotoPage={(pageNumber: number) => setPage(pageNumber)}
              onServiceSelect={async service => goToServiceDetails(service)}
            />
          )}
        </Layout.Vertical>
      </>
    </Page.Body>
  )
}