All files / modules/20-rbac/components/ResourcesCard ResourcesCard.tsx

80% Statements 20/25
66.67% Branches 10/15
42.86% Functions 3/7
83.33% Lines 20/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              10x 10x 10x 10x 10x 10x   10x   10x 10x                   10x             9x 9x 9x           9x 9x 9x 9x                                                       1x               1x                                                                           10x  
/*
 * 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 from 'react'
import { Layout, Text, Card, Button, Radio, Container, ButtonVariation } from '@wings-software/uicore'
import { Color } from '@harness/design-system'
import { useParams } from 'react-router-dom'
import RbacFactory from '@rbac/factories/RbacFactory'
import { useStrings } from 'framework/strings'
import type { ResourceType } from '@rbac/interfaces/ResourceType'
import useAddResourceModal from '@rbac/modals/AddResourceModal/useAddResourceModal'
import type { ProjectPathProps } from '@common/interfaces/RouteInterfaces'
import { isDynamicResourceSelector } from '@rbac/utils/utils'
import css from './ResourcesCard.module.scss'
 
interface ResourcesCardProps {
  resourceType: ResourceType
  resourceValues: string | string[]
  onResourceSelectionChange: (resourceType: ResourceType, isAdd: boolean, identifiers?: string[] | undefined) => void
  disableAddingResources?: boolean
  disableSelection?: boolean
}
 
const ResourcesCard: React.FC<ResourcesCardProps> = ({
  resourceType,
  resourceValues,
  onResourceSelectionChange,
  disableAddingResources,
  disableSelection
}) => {
  const { accountId, orgIdentifier, projectIdentifier } = useParams<ProjectPathProps>()
  const { getString } = useStrings()
  const { openAddResourceModal } = useAddResourceModal({
    onSuccess: resources => {
      onResourceSelectionChange(resourceType, true, resources)
    }
  })
 
  const resourceDetails = RbacFactory.getResourceTypeHandler(resourceType)
  Iif (!resourceDetails) return null
  const { label, icon, addResourceModalBody, staticResourceRenderer } = resourceDetails
  return (
    <Card className={css.selectedResourceGroupCardDetails} key={resourceType}>
      <Layout.Vertical>
        <Layout.Horizontal flex={{ justifyContent: 'space-between', alignItems: 'center' }}>
          <Container>
            <Text
              color={Color.BLACK}
              font={{ weight: 'semi-bold' }}
              icon={icon}
              iconProps={{ size: 30, padding: { right: 'medium' } }}
            >
              {getString(label)}
            </Text>
          </Container>
          {!disableSelection && (
            <Layout.Horizontal flex>
              <Radio
                label={getString('rbac.resourceGroup.all')}
                data-testid={`dynamic-${resourceType}`}
                checked={isDynamicResourceSelector(resourceValues)}
                onChange={e => onResourceSelectionChange(resourceType, e.currentTarget.checked)}
              />
              {addResourceModalBody && (
                <Layout.Horizontal spacing="small" flex padding={{ left: 'huge' }} className={css.radioBtn}>
                  <Radio
                    label={getString('common.specified')}
                    data-testid={`static-${resourceType}`}
                    checked={!isDynamicResourceSelector(resourceValues)}
                    onChange={e => onResourceSelectionChange(resourceType, e.currentTarget.checked, [])}
                  />
                  <Button
                    variation={ButtonVariation.LINK}
                    data-testid={`addResources-${resourceType}`}
                    disabled={disableAddingResources || isDynamicResourceSelector(resourceValues)}
                    className={css.addResourceBtn}
                    onClick={() => {
                      openAddResourceModal(resourceType, Array.isArray(resourceValues) ? resourceValues : [])
                    }}
                  >
                    {getString('rbac.resourceGroup.add')}
                  </Button>
                </Layout.Horizontal>
              )}
            </Layout.Horizontal>
          )}
        </Layout.Horizontal>
 
        {Array.isArray(resourceValues) && resourceValues.length > 0 && (
          <Layout.Vertical padding={{ top: 'large' }}>
            {staticResourceRenderer
              ? staticResourceRenderer({
                  identifiers: resourceValues,
                  resourceScope: { accountIdentifier: accountId, orgIdentifier, projectIdentifier },
                  onResourceSelectionChange,
                  resourceType
                })
              : resourceValues.map(resource => (
                  <Layout.Horizontal padding="large" className={css.staticResource} key={resource} flex>
                    <Text>{resource}</Text>
                    <Button
                      icon="trash"
                      minimal
                      onClick={() => {
                        onResourceSelectionChange(resourceType, false, [resource])
                      }}
                    />
                  </Layout.Horizontal>
                ))}
          </Layout.Vertical>
        )}
      </Layout.Vertical>
    </Card>
  )
}
export default ResourcesCard