All files / modules/75-ce/components/BusinessMappingList BusinessMappingList.tsx

96.97% Statements 32/33
68.75% Branches 11/16
100% Functions 9/9
96.97% Lines 32/33

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              2x 2x   2x   2x 2x 2x               2x         6x   6x 12x     6x 6x                       6x 6x 6x   6x     6x         6x 6x     6x 6x 6x 6x   6x 1x     6x 1x     6x     6x 5x                                                                                           6x                     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, { useMemo } from 'react'
import { Container, FontVariation, TableV2, Text, Layout } from '@harness/uicore'
import type { CellProps, Column, Renderer } from 'react-table'
import moment from 'moment'
import type { BusinessMapping } from 'services/ce'
import { useStrings } from 'framework/strings'
import MenuCell from './MenuCell'
import css from './BusinessMappingList.module.scss'
 
interface BusinessMappingListProps {
  data: BusinessMapping[]
  handleDelete: (id: string, name: string) => void
  onEdit: (data: BusinessMapping) => void
}
 
const BusinessMappingList: (props: BusinessMappingListProps) => React.ReactElement = ({
  data,
  handleDelete,
  onEdit
}) => {
  const { getString } = useStrings()
 
  const CostBucketCell: Renderer<CellProps<BusinessMapping>> = cell => {
    return <Text font={{ variation: FontVariation.BODY2 }}>{cell.value?.length || 0}</Text>
  }
 
  const NameCell: Renderer<CellProps<BusinessMapping>> = cell => {
    return (
      <Layout.Horizontal spacing="small" className={css.nameContainer}>
        <Container className={css.iconBox} />
        <Text font={{ variation: FontVariation.BODY2 }}>{cell.value}</Text>
      </Layout.Horizontal>
    )
  }
 
  // const UserCell: Renderer<CellProps<BusinessMapping>> = cell => {
  //   return <Text>{cell.value?.name || ''}</Text>
  // }
 
  const UnallocatedCost: Renderer<CellProps<BusinessMapping>> = cell => {
    const label = cell.value?.label
    const strategy = cell.value?.strategy
 
    Iif (strategy === 'HIDE') {
      return <Text font={{ variation: FontVariation.BODY }}>{getString('ce.businessMapping.shownAsHidden')}</Text>
    }
    return (
      <Text font={{ variation: FontVariation.BODY }}>{getString('ce.businessMapping.shownAs', { value: label })}</Text>
    )
  }
 
  const LastEditCell: Renderer<CellProps<BusinessMapping>> = cell => {
    return <Text font={{ variation: FontVariation.BODY }}>{moment.utc(cell.value).format('D MMM, HH:mm')}</Text>
  }
 
  const MenuOptionsCell: Renderer<CellProps<BusinessMapping>> = ({ row }) => {
    const bmId = row.original.uuid as string
    const bmName = row.original.name as string
    const bmData = row.original
 
    const handleEdit: () => void = () => {
      onEdit(bmData)
    }
 
    const onDelete: () => void = () => {
      handleDelete(bmId, bmName)
    }
 
    return <MenuCell id={bmId} name={bmName} handleDelete={onDelete} handleEdit={handleEdit} />
  }
 
  const columns: Column<BusinessMapping>[] = useMemo(
    () => [
      {
        accessor: 'name',
        Header: getString('name'),
        Cell: NameCell,
        width: '25%'
      },
      {
        accessor: 'costTargets',
        Header: getString('ce.businessMapping.tableHeadings.costBuckets'),
        Cell: CostBucketCell,
        width: '15%'
      },
      {
        accessor: 'sharedCosts',
        Header: getString('ce.businessMapping.tableHeadings.sharedCosts'),
        Cell: CostBucketCell,
        width: '15%'
      },
      {
        accessor: 'unallocatedCost',
        Header: getString('ce.businessMapping.tableHeadings.unallocated'),
        Cell: UnallocatedCost,
        width: '25%'
      },
      // {
      //   accessor: 'createdBy',
      //   Header: getString('ce.businessMapping.tableHeadings.createdBy'),
      //   width: '25%',
      //   Cell: UserCell
      // },
      {
        accessor: 'lastUpdatedAt',
        Header: getString('ce.businessMapping.tableHeadings.lastEdit'),
        Cell: LastEditCell,
        width: '15%'
      },
      {
        Header: ' ',
        width: '5%',
        Cell: MenuOptionsCell
      }
    ],
    []
  )
 
  return (
    <Container
      margin={{
        top: 'medium'
      }}
    >
      <TableV2<BusinessMapping> columns={columns} data={data} />
    </Container>
  )
}
 
export default BusinessMappingList