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

78.79% Statements 26/33
81.82% Branches 18/22
66.67% Functions 8/12
80% Lines 24/30

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              10x 10x 10x 10x   10x 10x 10x                     10x                 10x   10x   10x 48x 32x   30x       10x 18x   10x                                                             12x   12x 12x                                                                                   36x 36x                     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 { Card, Layout, Text, Checkbox, Radio } from '@wings-software/uicore'
import { Color, FontVariation } from '@harness/design-system'
import RbacFactory from '@rbac/factories/RbacFactory'
import type { ResourceType, ResourceCategory } from '@rbac/interfaces/ResourceType'
import { useStrings } from 'framework/strings'
import { SelectionType } from '@rbac/utils/utils'
import css from './ResourceTypeList.module.scss'
 
interface ResourceTypeListProps {
  selectionType: SelectionType
  resourceCategoryMap?: Map<ResourceType | ResourceCategory, ResourceType[] | undefined>
  onSelectionTypeChange: (type: SelectionType) => void
  onResourceSelectionChange: (resource: ResourceType, isAdd: boolean) => void
  onResourceCategorySelect: (types: ResourceType[], isAdd: boolean) => void
  preSelectedResourceList: ResourceType[]
  disableAddingResources?: boolean
}
const ResourceTypeList: React.FC<ResourceTypeListProps> = props => {
  const {
    selectionType,
    resourceCategoryMap,
    onSelectionTypeChange,
    onResourceSelectionChange,
    onResourceCategorySelect,
    preSelectedResourceList,
    disableAddingResources
  } = props
 
  const { getString } = useStrings()
 
  const getChecked = (resourceCategory: ResourceType | ResourceCategory, resourceTypes?: ResourceType[]): boolean => {
    if (resourceTypes) {
      return Array.from(resourceTypes).every(value => preSelectedResourceList.includes(value))
    } else {
      return preSelectedResourceList.includes(resourceCategory as ResourceType)
    }
  }
 
  const getIntermittent = (resourceTypes: ResourceType[]): boolean =>
    Array.from(resourceTypes).some(value => preSelectedResourceList.includes(value))
 
  return (
    <Layout.Vertical flex spacing="small" className={css.resourceTypeList}>
      <Layout.Horizontal flex className={css.resourcePicker} padding={{ bottom: 'small' }}>
        <Text font={{ variation: FontVariation.H6 }} color={Color.GREY_800}>
          {getString('resources')}
        </Text>
 
        <Layout.Horizontal flex spacing="huge">
          <Radio
            label={getString('rbac.resourceGroup.all')}
            inline={true}
            value={SelectionType.ALL}
            checked={selectionType === SelectionType.ALL}
            onChange={e => {
              onSelectionTypeChange(e.currentTarget.value as SelectionType)
            }}
          />
          <Radio
            label={getString('common.specified')}
            inline={true}
            value={SelectionType.SPECIFIED}
            checked={selectionType === SelectionType.SPECIFIED}
            onChange={e => {
              onSelectionTypeChange(e.currentTarget.value as SelectionType)
            }}
          />
        </Layout.Horizontal>
      </Layout.Horizontal>
      {resourceCategoryMap?.keys() &&
        Array.from(resourceCategoryMap.keys()).map(resourceCategory => {
          const resourceCategoryHandler =
            RbacFactory.getResourceCategoryHandler(resourceCategory as ResourceCategory) ||
            RbacFactory.getResourceTypeHandler(resourceCategory as ResourceType)
          const resourceTypes = resourceCategoryMap.get(resourceCategory)
          return (
            resourceCategoryHandler && (
              <Card
                className={css.resourceTypeCard}
                key={resourceCategory}
                draggable={!disableAddingResources}
                onDragStart={e => {
                  e.dataTransfer.setData('text/plain', resourceCategory)
                  e.dataTransfer.dropEffect = 'copy'
                }}
              >
                <Layout.Vertical>
                  <Layout.Horizontal flex={{ alignItems: 'center', justifyContent: 'flex-start' }}>
                    <Checkbox
                      data-testid={`CHECK-BOX-${resourceCategory}`}
                      key={resourceCategory}
                      disabled={disableAddingResources}
                      checked={getChecked(resourceCategory, resourceTypes)}
                      indeterminate={
                        resourceTypes
                          ? getIntermittent(resourceTypes) && !getChecked(resourceCategory, resourceTypes)
                          : undefined
                      }
                      className={css.checkBox}
                      onChange={e => {
                        if (resourceTypes) onResourceCategorySelect(resourceTypes, e.currentTarget.checked)
                        else onResourceSelectionChange(resourceCategory as ResourceType, e.currentTarget.checked)
                      }}
                      value={resourceCategory}
                    />
 
                    <Text
                      font={{ size: 'small', weight: 'semi-bold' }}
                      color={Color.GREY_600}
                      icon={resourceCategoryHandler.icon}
                      iconProps={{ padding: { right: 'small' } }}
                    >
                      {getString(resourceCategoryHandler.label).toUpperCase()}
                    </Text>
                  </Layout.Horizontal>
                  {resourceTypes &&
                    Array.from(resourceTypes).map(resource => {
                      const resourceHandler = RbacFactory.getResourceTypeHandler(resource)
                      return (
                        resourceHandler && (
                          <Layout.Horizontal
                            key={resource}
                            flex={{ alignItems: 'center', justifyContent: 'flex-start' }}
                            className={css.resourceSubList}
                          >
                            <Checkbox
                              data-testid={`CHECK-BOX-${resource}`}
                              disabled={disableAddingResources}
                              onChange={e => {
                                onResourceSelectionChange(resource, e.currentTarget.checked)
                              }}
                              checked={getChecked(resource)}
                            />
                            <Text font="small" color={Color.GREY_800}>
                              {getString(resourceHandler.label)}
                            </Text>
                          </Layout.Horizontal>
                        )
                      )
                    })}
                </Layout.Vertical>
              </Card>
            )
          )
        })}
    </Layout.Vertical>
  )
}
export default ResourceTypeList