All files / modules/18-audit-trail/components/FilterDrawer AuditTrailFilterForm.tsx

84.21% Statements 32/38
72.86% Branches 51/70
60% Functions 6/10
86.11% Lines 31/36

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              4x 4x 4x   4x 4x   4x 4x 4x 4x 4x             4x 18x 18x 18x 18x 18x   18x               18x           18x 6x           18x   18x 18x       18x     18x                         18x 306x           18x   18x                       6x                                                                                                       4x  
/*
 * 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 { FormInput, MultiSelectOption } from '@wings-software/uicore'
import React, { useState } from 'react'
import { useParams } from 'react-router-dom'
import type { FormikProps } from 'formik'
import { useMutateAsGet } from '@common/hooks'
import { StringKeys, useStrings } from 'framework/strings'
import type { ProjectPathProps } from '@common/interfaces/RouteInterfaces'
import { useGetUsers, useGetOrganizationAggregateDTOList, useGetProjectListWithMultiOrgFilter } from 'services/cd-ng'
import { actionToLabelMap, getOrgDropdownList, getProjectDropdownList } from '@audit-trail/utils/RequestUtil'
import UserItemRenderer from '@audit-trail/components/UserItemRenderer/UserItemRenderer'
import UserTagRenderer from '@audit-trail/components/UserTagRenderer/UserTagRenderer'
import AuditTrailFactory from '@audit-trail/factories/AuditTrailFactory'
import type { AuditTrailFormType } from './FilterDrawer'
 
interface AuditTrailFormProps {
  formikProps?: FormikProps<AuditTrailFormType>
}
 
const AuditTrailFilterForm: React.FC<AuditTrailFormProps> = props => {
  const { accountId, projectIdentifier, orgIdentifier } = useParams<ProjectPathProps>()
  const [userQuery, setUserQuery] = useState<string>()
  const [orgQuery, setOrgQuery] = useState<string>()
  const [projectsQuery, setProjectsQuery] = useState<string>()
  const { getString } = useStrings()
 
  const { data: userData } = useMutateAsGet(useGetUsers, {
    queryParams: { accountIdentifier: accountId, orgIdentifier, projectIdentifier },
    body: {
      searchTerm: userQuery
    },
    debounce: 300
  })
 
  const { data } = useGetOrganizationAggregateDTOList({
    queryParams: { accountIdentifier: accountId, searchTerm: orgQuery },
    debounce: 300
  })
 
  const users =
    userData?.data?.content?.map(user => {
      return {
        label: user.name || user.email,
        value: user.email
      }
    }) || []
 
  const organizations = data?.data?.content ? getOrgDropdownList(data?.data?.content) : []
 
  const getOrgs = (): string[] => {
    Iif (orgIdentifier) {
      return [orgIdentifier]
    }
 
    return props.formikProps?.values.organizations?.map(org => org.value as string) || []
  }
 
  const { data: projectData, refetch: refetchProjectList } = useGetProjectListWithMultiOrgFilter({
    queryParams: {
      accountIdentifier: accountId,
      searchTerm: projectsQuery,
      orgIdentifiers: getOrgs(),
      pageSize: 10
    },
    queryParamStringifyOptions: {
      arrayFormat: 'repeat'
    },
    debounce: 300
  })
 
  const getOptionsForMultiSelect = (map: Record<any, StringKeys>): MultiSelectOption[] => {
    return Object.keys(map).map(key => ({
      label: getString(map[key]),
      value: key
    }))
  }
 
  const projects = projectData?.data?.content ? getProjectDropdownList(projectData?.data?.content) : []
 
  return (
    <>
      <FormInput.MultiSelect
        name="users"
        key="users"
        items={users}
        label={getString('common.userLabel')}
        multiSelectProps={{
          allowCreatingNewItems: true,
          onQueryChange: setUserQuery,
          tagRenderer: (item: MultiSelectOption) => <UserTagRenderer key={item.value.toString()} item={item} />,
          itemRender: (item, { handleClick }) => (
            <UserItemRenderer key={item.value.toString()} item={item} handleClick={handleClick} />
          )
        }}
      />
      {!orgIdentifier && (
        <FormInput.MultiSelect
          name="organizations"
          key="organizations"
          items={organizations}
          label={getString('orgLabel')}
          multiSelectProps={{
            onQueryChange: setOrgQuery
          }}
          onChange={() => {
            props.formikProps?.setFieldValue('projects', [])
          }}
        />
      )}
      <FormInput.MultiSelect
        name="projects"
        key="projects"
        label={getString('projectLabel')}
        items={projects}
        multiSelectProps={{
          onQueryChange: query => {
            setProjectsQuery(query)
            refetchProjectList()
          }
        }}
      />
      {/* <FormInput.MultiSelect
        items={getOptionsForMultiSelect(moduleToLabelMap)}
        name="modules"
        label={getString('common.moduleLabel')}
        key="modules"
      /> */}
      <FormInput.MultiSelect
        items={getOptionsForMultiSelect(AuditTrailFactory.getResourceTypeTolabelMap())}
        name="resourceType"
        label={getString('common.resourceTypeLabel')}
        key="resourceType"
      />
      <FormInput.MultiSelect
        items={getOptionsForMultiSelect(actionToLabelMap)}
        name="actions"
        label={getString('action')}
        key="actions"
      />
    </>
  )
}
 
export default AuditTrailFilterForm