All files / modules/35-user-profile/components/UserSummary SourceCodeManagerList.tsx

92.31% Statements 48/52
43.75% Branches 14/32
88.89% Functions 8/9
92.31% Lines 48/52

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              1x 1x 1x   1x 1x 1x 1x 1x 1x     1x 44x 44x                           1x 44x   44x         44x         44x                   1x 46x 46x 46x 46x 46x   46x           1x 1x 1x     1x 1x         1x                             46x 1x 1x     46x         1x 15x 15x 15x   15x   15x 15x                                                       15x 15x 9x   6x 6x                           15x                   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, { useMemo } from 'react'
import { useParams } from 'react-router-dom'
import { Text, Layout, Button, Icon, ButtonVariation, useConfirmationDialog } from '@wings-software/uicore'
import type { CellProps, Column, Renderer } from 'react-table'
import { Color } from '@harness/design-system'
import { useSourceCodeModal } from '@user-profile/modals/SourceCodeManager/useSourceCodeManager'
import { useStrings } from 'framework/strings'
import { SourceCodeManagerDTO, useDeleteSourceCodeManagers, useGetSourceCodeManagers } from 'services/cd-ng'
import { Table, useToaster } from '@common/components'
import { getIconBySCM, SourceCodeTypes } from '@user-profile/utils/utils'
import type { AccountPathProps } from '@common/interfaces/RouteInterfaces'
 
const RenderColumnName: Renderer<CellProps<SourceCodeManagerDTO>> = ({ row }) => {
  const data = row.original
  return (
    <Layout.Horizontal
      padding={{ left: 'small' }}
      spacing="medium"
      flex={{ alignItems: 'center', justifyContent: 'flex-start' }}
    >
      <Icon name={getIconBySCM(data.type as SourceCodeTypes)} size={25} />
      <Text color={Color.BLACK} lineClamp={1}>
        {data.name}
      </Text>
    </Layout.Horizontal>
  )
}
 
const RenderColumnEdit: Renderer<CellProps<SourceCodeManagerDTO>> = ({ row, column }) => {
  const sourceCodeManagerData = row.original
 
  const { openSourceCodeModal } = useSourceCodeModal({
    initialValues: sourceCodeManagerData,
    onSuccess: (column as any).reload
  })
 
  const handleEdit = (e: React.MouseEvent<Element, MouseEvent>): void => {
    e.stopPropagation()
    openSourceCodeModal()
  }
 
  return (
    <Button
      icon="Edit"
      data-testid={`${sourceCodeManagerData.name}-edit`}
      variation={ButtonVariation.ICON}
      onClick={handleEdit}
    />
  )
}
 
const RenderColumnDelete: Renderer<CellProps<SourceCodeManagerDTO>> = ({ row, column }) => {
  const data = row.original
  const { showSuccess, showError } = useToaster()
  const { getString } = useStrings()
  const { accountId } = useParams<AccountPathProps>()
  const { mutate: deleteSCM } = useDeleteSourceCodeManagers({ queryParams: { accountIdentifier: accountId } })
 
  const { openDialog } = useConfirmationDialog({
    contentText: `${getString('userProfile.confirmDelete', { name: data.name })}`,
    titleText: getString('userProfile.confirmDeleteTitle'),
    confirmButtonText: getString('delete'),
    cancelButtonText: getString('cancel'),
    onCloseDialog: async (isConfirmed: boolean) => {
      /* istanbul ignore else */ if (isConfirmed) {
        try {
          const deleted = await deleteSCM(data.name, {
            headers: { 'content-type': 'application/json' }
          })
          /* istanbul ignore else */ if (deleted) {
            showSuccess(
              getString('userProfile.scmDeleteSuccess', {
                name: data.name
              })
            )
            ;(column as any).reload?.()
          } /* istanbul ignore next */ else {
            showError(
              getString('userProfile.scmDeleteFailure', {
                name: data.name
              })
            )
          }
        } /* istanbul ignore next */ catch (err) {
          showError(err?.data?.message || err?.message)
        }
      }
    }
  })
 
  const handleDelete = (e: React.MouseEvent<Element, MouseEvent>): void => {
    e.stopPropagation()
    openDialog()
  }
 
  return (
    <Button icon="trash" data-testid={`${data.name}-delete`} variation={ButtonVariation.ICON} onClick={handleDelete} />
  )
}
 
const SourceCodeManagerList: React.FC = () => {
  const { getString } = useStrings()
  const { accountId } = useParams<AccountPathProps>()
  const { data, loading, refetch } = useGetSourceCodeManagers({ queryParams: { accountIdentifier: accountId } })
 
  const { openSourceCodeModal } = useSourceCodeModal({ onSuccess: refetch })
 
  const columns: Column<SourceCodeManagerDTO>[] = useMemo(
    () => [
      {
        Header: '',
        id: 'name',
        accessor: 'name',
        width: '90%',
        Cell: RenderColumnName
      },
      {
        Header: '',
        id: 'edit',
        accessor: 'type',
        width: '5%',
        Cell: RenderColumnEdit,
        reload: refetch
      },
      {
        Header: '',
        id: 'delete',
        accessor: 'type',
        width: '5%',
        Cell: RenderColumnDelete,
        reload: refetch
      }
    ],
    [refetch]
  )
 
  const getContent = (): React.ReactElement => {
    if (data?.data?.length) {
      return <Table<SourceCodeManagerDTO> data={data.data} columns={columns} hideHeaders={true} />
    }
    Eif (!loading) {
      return (
        <Layout.Horizontal padding={{ top: 'large' }}>
          <Button
            text={getString('userProfile.plusSCM')}
            data-test="userProfileAddSCM"
            variation={ButtonVariation.LINK}
            onClick={openSourceCodeModal}
          />
        </Layout.Horizontal>
      )
    }
    return <></>
  }
 
  return (
    <Layout.Vertical spacing="large">
      <Text font={{ size: 'medium', weight: 'semi-bold' }} color={Color.BLACK}>
        {getString('userProfile.mysourceCodeManagers')}
      </Text>
      {getContent()}
    </Layout.Vertical>
  )
}
 
export default SourceCodeManagerList