All files / modules/75-ce/pages/business-mapping BusinessMapping.tsx

71.05% Statements 27/38
75% Branches 12/16
33.33% Functions 2/6
71.05% Lines 27/38

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              1x 1x                     1x 1x 1x 1x 1x   1x 1x 1x   1x 5x 4x 4x 4x 4x     4x 4x   4x     4x                                 1x           4x 1x                             3x                               3x         3x                                                                                     1x  
/*
 * 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, { useState } from 'react'
import {
  Button,
  Layout,
  PageBody,
  PageHeader,
  Container,
  Text,
  FontVariation,
  useToaster,
  getErrorInfoFromErrorObject
} from '@harness/uicore'
import { useParams } from 'react-router-dom'
import { Drawer, Position } from '@blueprintjs/core'
import { NGBreadcrumbs } from '@common/components/NGBreadcrumbs/NGBreadcrumbs'
import { useStrings } from 'framework/strings'
import { BusinessMapping, useDeleteBusinessMapping, useGetBusinessMappingList } from 'services/ce'
import type { AccountPathProps } from '@common/interfaces/RouteInterfaces'
import BusinessMappingBuilder from '@ce/components/BusinessMappingBuilder/BusinessMappingBuilder'
import BusinessMappingList from '@ce/components/BusinessMappingList/BusinessMappingList'
import EmptyPage from '@ce/common/EmptyPage/EmptyPage'
 
const BusinessMappingPage: () => React.ReactElement = () => {
  const { accountId } = useParams<AccountPathProps>()
  const { getString } = useStrings()
  const [selectedBM, setSelectedBM] = useState<BusinessMapping>({})
  const { data, loading, refetch } = useGetBusinessMappingList({ queryParams: { accountIdentifier: accountId } })
  const { mutate: deleteBusinessMapping, loading: deleteLoading } = useDeleteBusinessMapping({
    queryParams: { accountIdentifier: accountId }
  })
  const [drawerOpen, setDrawerOpen] = useState<boolean>(false)
  const { showError, showSuccess } = useToaster()
 
  const businessMappingData = data?.resource || []
 
  const ToolBar = (
    <Layout.Horizontal
      padding={{
        left: 'large',
        right: 'large',
        top: 'medium',
        bottom: 'medium'
      }}
      background="white"
      border={{
        bottom: true
      }}
    >
      <Button
        icon="plus"
        text={getString('ce.businessMapping.newButton')}
        intent="primary"
        onClick={() => {
          setDrawerOpen(true)
        }}
      />
    </Layout.Horizontal>
  )
 
  if (!loading && !businessMappingData.length) {
    return (
      <>
        <PageHeader breadcrumbs={<NGBreadcrumbs />} title={getString('ce.businessMapping.sideNavText')} />
        <PageBody>
          {ToolBar}
          <EmptyPage
            subtitle={getString('ce.businessMapping.emptySubtitles')}
            buttonText={getString('ce.businessMapping.newButton')}
            buttonAction={() => setDrawerOpen(true)}
          />
        </PageBody>
      </>
    )
  }
 
  const handleDelete: (id: string, name: string) => void = async (id, name) => {
    try {
      const deleted = await deleteBusinessMapping(id, {
        headers: {
          'content-type': 'application/json'
        }
      })
      if (deleted) {
        showSuccess(getString('ce.businessMapping.deleted', { name: name }))
        refetch()
      }
    } catch (err) {
      showError(getErrorInfoFromErrorObject(err))
    }
  }
 
  const onEdit: (data: BusinessMapping) => void = editData => {
    setSelectedBM(editData)
    setDrawerOpen(true)
  }
 
  return (
    <>
      <PageHeader breadcrumbs={<NGBreadcrumbs />} title={getString('ce.businessMapping.sideNavText')} />
      <PageBody loading={loading || deleteLoading}>
        {ToolBar}
        <Container padding="medium">
          {businessMappingData.length ? (
            <Text font={{ variation: FontVariation.H5 }}>
              {getString('ce.common.totalCount', { count: businessMappingData.length })}
            </Text>
          ) : null}
          <BusinessMappingList onEdit={onEdit} handleDelete={handleDelete} data={businessMappingData} />
        </Container>
 
        <Drawer
          autoFocus
          enforceFocus
          hasBackdrop
          usePortal
          canOutsideClickClose
          canEscapeKeyClose
          position={Position.RIGHT}
          isOpen={drawerOpen}
          onClose={
            /* istanbul ignore next */ () => {
              setSelectedBM({})
              setDrawerOpen(false)
            }
          }
        >
          <BusinessMappingBuilder
            selectedBM={selectedBM}
            onSave={() => {
              setDrawerOpen(false)
              refetch()
            }}
          />
        </Drawer>
      </PageBody>
    </>
  )
}
 
export default BusinessMappingPage