All files / modules/85-cv/pages/ChangeSource/ChangeSourceTable ChangeSourceTable.tsx

84% Statements 21/25
61.11% Branches 11/18
57.14% Functions 4/7
87.5% Lines 21/24

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              18x 18x   18x 18x 18x   18x 18x   18x   18x 18x   18x 38x 38x   38x   38x                                       38x 32x 32x                         1x                                             38x                   4x                                                          
/*
 * 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, { useCallback } from 'react'
import { cloneDeep } from 'lodash-es'
import type { Renderer, CellProps } from 'react-table'
import { useParams } from 'react-router-dom'
import { Container, Icon, Layout, Text, NoDataCard, TableV2 } from '@wings-software/uicore'
import { useStrings } from 'framework/strings'
import type { ChangeSourceDTO } from 'services/cv'
import { ResourceType } from '@rbac/interfaces/ResourceType'
import { PermissionIdentifier } from '@rbac/interfaces/PermissionIdentifier'
import type { ProjectPathProps } from '@common/interfaces/RouteInterfaces'
import ContextMenuActions from '@cv/components/ContextMenuActions/ContextMenuActions'
import type { ChangeSourceTableInterface } from './ChangeSourceTable.types'
import { getIconBySource } from '../ChangeSource.utils'
import css from './ChangeSourceTable.module.scss'
 
export default function ChangeSourceTable({ value, onSuccess, onEdit }: ChangeSourceTableInterface): JSX.Element {
  const tableData = cloneDeep(value)
  const { getString } = useStrings()
 
  const { projectIdentifier } = useParams<ProjectPathProps>()
 
  const deleteChangeSource = useCallback(
    async (selectedRow: ChangeSourceDTO): Promise<void> => {
      const updatedChangeSources = tableData?.filter(changeSource => changeSource.identifier !== selectedRow.identifier)
      await onSuccess(updatedChangeSources as ChangeSourceDTO[])
    },
    [tableData]
  )
 
  // Not Planned For Release
  //
  // const onToggle = async (selectedRow: ChangeSourceDTO): Promise<void> => {
  //   const updatedChangeSources = tableData?.map(changeSource => {
  //     if (changeSource.identifier === selectedRow.identifier) {
  //       changeSource.enabled = !selectedRow.enabled
  //     }
  //     return changeSource
  //   })
  //   await onSuccess(updatedChangeSources as ChangeSourceDTO[])
  // }
 
  const renderEnable: Renderer<CellProps<ChangeSourceDTO>> = ({ row }): JSX.Element => {
    const rowdata = row?.original
    return (
      <Layout.Horizontal flex={{ justifyContent: 'space-between' }}>
        {/* <Switch checked={rowdata?.enabled} onChange={async () => await onToggle(rowdata)} /> */}
        <Icon
          className={css.sourceTypeIcon}
          name={getIconBySource(rowdata?.type as ChangeSourceDTO['type'])}
          size={22}
        />
        <ContextMenuActions
          titleText={getString('cv.admin.activitySources.dialogDeleteTitle')}
          contentText={getString('cv.changeSource.deleteChangeSourceWarning') + `: ${rowdata.identifier}`}
          onDelete={async () => await deleteChangeSource(rowdata)}
          onEdit={() => {
            onEdit({ isEdit: true, tableData, rowdata, onSuccess })
          }}
          RbacPermissions={{
            edit: {
              permission: PermissionIdentifier.EDIT_MONITORED_SERVICE,
              resource: {
                resourceType: ResourceType.MONITOREDSERVICE,
                resourceIdentifier: projectIdentifier
              }
            },
            delete: {
              permission: PermissionIdentifier.DELETE_MONITORED_SERVICE,
              resource: {
                resourceType: ResourceType.MONITOREDSERVICE,
                resourceIdentifier: projectIdentifier
              }
            }
          }}
        />
      </Layout.Horizontal>
    )
  }
 
  return (
    <>
      <Text className={css.tableTitle} tooltipProps={{ dataTooltipId: 'changeSourceTable' }}>
        {getString('cv.navLinks.adminSideNavLinks.activitySources')}
      </Text>
      {tableData?.length ? (
        <TableV2
          className={css.changeSourceTableWrapper}
          sortable={true}
          onRowClick={rowdata => {
            onEdit({ isEdit: true, tableData, rowdata, onSuccess })
          }}
          columns={[
            {
              Header: getString('name'),
              accessor: 'name',
              width: '30%'
            },
            {
              Header: getString('typeLabel'),
              accessor: 'category',
              width: '35%'
            },
            {
              Header: getString('source'),
              width: '35%',
              Cell: renderEnable
            }
          ]}
          data={tableData}
        />
      ) : (
        <Container className={css.noData}>
          <NoDataCard icon={'join-table'} message={getString('cv.changeSource.noChangeSource')} />
        </Container>
      )}
    </>
  )
}