All files / modules/20-rbac/modals/AddResourceModal/views AddResourceModal.tsx

82.61% Statements 19/23
55.56% Branches 10/18
25% Functions 1/4
86.36% Lines 19/22

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              10x 10x 10x 10x 10x 10x 10x   10x 10x                 10x 2x 2x 2x 2x 2x   2x 2x   2x                                                                                                     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, { useState } from 'react'
import { Button, ButtonVariation, Container, ExpandingSearchInput, Layout, Text } from '@wings-software/uicore'
import { Color } from '@harness/design-system'
import { useParams } from 'react-router-dom'
import { ResourceType } from '@rbac/interfaces/ResourceType'
import RbacFactory from '@rbac/factories/RbacFactory'
import { useStrings } from 'framework/strings'
import type { ProjectPathProps } from '@common/interfaces/RouteInterfaces'
import { Page } from '@common/exports'
import css from './AddResourceModal.module.scss'
 
interface RoleModalData {
  resource: ResourceType
  selectedData: string[]
  onSuccess: (resources: string[]) => void
  onClose: () => void
}
 
const AddResourceModal: React.FC<RoleModalData> = ({ resource, onSuccess, onClose, selectedData }) => {
  const resourceHandler = RbacFactory.getResourceTypeHandler(resource)
  const { accountId, orgIdentifier, projectIdentifier } = useParams<ProjectPathProps>()
  const { getString } = useStrings()
  const [searchTerm, setSearchTerm] = useState<string>('')
  const [selectedItems, setSelectedItems] = useState<string[]>(selectedData)
 
  Iif (!resourceHandler) return <Page.Error />
  const label = resource === ResourceType['DASHBOARDS'] ? resourceHandler.labelOverride : resourceHandler.label
 
  return (
    <Layout.Vertical padding="xxxlarge">
      <Layout.Vertical>
        <Text color={Color.BLACK} font="medium">
          {`${getString('add')} ${getString(resourceHandler.label)}`}
        </Text>
        <Layout.Horizontal padding={{ top: 'large' }} flex>
          <ExpandingSearchInput
            alwaysExpanded
            onChange={text => {
              setSearchTerm(text.trim())
            }}
          />
          <Text color={Color.PRIMARY_7}>
            {getString('rbac.addResourceModal.selectedText', {
              name: getString(label || resourceHandler.label),
              number: selectedItems.length
            })}
          </Text>
        </Layout.Horizontal>
        <Container className={css.modal}>
          {resourceHandler?.addResourceModalBody?.({
            searchTerm,
            onSelectChange: items => {
              setSelectedItems(items)
            },
            selectedData: selectedItems,
            resourceScope: {
              accountIdentifier: accountId,
              orgIdentifier,
              projectIdentifier
            }
          })}
        </Container>
        <Layout.Horizontal spacing="small">
          <Button
            variation={ButtonVariation.PRIMARY}
            text={`${getString('add')} ${selectedItems.length} ${
              resource === ResourceType['DASHBOARDS']
                ? getString(resourceHandler.labelOverride || 'dashboards.homePage.folders')
                : getString(resourceHandler.label)
            }`}
            onClick={() => onSuccess(selectedItems)}
          />
          <Button text={getString('cancel')} onClick={onClose} />
        </Layout.Horizontal>
      </Layout.Vertical>
    </Layout.Vertical>
  )
}
 
export default AddResourceModal