All files / modules/18-audit-trail/components AuditTrailsFilters.tsx

100% Statements 35/35
80.77% Branches 42/52
100% Functions 6/6
100% Lines 34/34

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              2x   2x 2x 2x 2x   2x 2x 2x 2x           2x 14x 14x 14x   14x 14x 14x 14x 14x 14x 14x   14x               14x   2x 1x   2x 2x     14x 2x               1x 1x 1x           14x           2x                   2x  
/*
 * 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 React, { useState } from 'react'
import type { SelectOption } from '@wings-software/uicore'
import { useModalHook } from '@harness/use-modal'
import { useParams } from 'react-router-dom'
import FilterSelector from '@common/components/Filter/FilterSelector/FilterSelector'
import { AuditFilterProperties, FilterDTO, useGetAuditFilterList } from 'services/audit'
import type { ProjectPathProps } from '@common/interfaces/RouteInterfaces'
import { removeNullAndEmpty } from '@common/components/Filter/utils/FilterUtils'
import { useStrings } from 'framework/strings'
import { formToLabelMap, getFormValuesFromFilterProperties } from '@audit-trail/utils/RequestUtil'
import FilterDrawer from './FilterDrawer/FilterDrawer'
 
interface AuditFiltersProps {
  applyFilters?: (filtersProperties: AuditFilterProperties) => void
}
 
const AuditTrailsFilters: React.FC<AuditFiltersProps> = ({ applyFilters }) => {
  const { accountId, projectIdentifier, orgIdentifier } = useParams<ProjectPathProps>()
  const [selectedFilter, setSelectedFilter] = useState<FilterDTO | undefined>()
  const { getString } = useStrings()
 
  const fieldToLabelMapping = new Map<string, string>()
  fieldToLabelMapping.set('users', getString('common.userLabel'))
  fieldToLabelMapping.set('organizations', getString('orgLabel'))
  fieldToLabelMapping.set('projects', getString('projectLabel'))
  fieldToLabelMapping.set('modules', getString('common.moduleLabel'))
  fieldToLabelMapping.set('resourceType', getString('common.resourceTypeLabel'))
  fieldToLabelMapping.set('actions', getString('action'))
 
  const { data: filterResponse, refetch: refetchFilterList } = useGetAuditFilterList({
    queryParams: {
      accountIdentifier: accountId,
      orgIdentifier,
      projectIdentifier
    }
  })
 
  const onFilterSelect = (identifier?: string): void => {
    let filter
    if (identifier) {
      filter = filterResponse?.data?.content?.find(filtr => filtr.identifier === identifier)
    }
    setSelectedFilter(filter)
    applyFilters?.(filter?.filterProperties || {})
  }
 
  const [openDrawer, closeDrawer] = useModalHook(() => {
    return (
      <FilterDrawer
        filters={filterResponse?.data?.content || []}
        closeDrawer={closeDrawer}
        refetchFilters={refetchFilterList}
        selectedFilter={selectedFilter}
        selectFilter={setSelectedFilter}
        applyFilter={(filter: FilterDTO) => {
          closeDrawer()
          setSelectedFilter(filter)
          applyFilters?.(filter.filterProperties)
        }}
      />
    )
  }, [filterResponse, selectedFilter])
 
  return (
    <FilterSelector<FilterDTO>
      appliedFilter={selectedFilter}
      filters={filterResponse?.data?.content}
      onFilterBtnClick={openDrawer}
      onFilterSelect={(option: SelectOption) => {
        onFilterSelect(option.value as string)
      }}
      fieldToLabelMapping={fieldToLabelMapping}
      filterWithValidFields={formToLabelMap(
        removeNullAndEmpty(getFormValuesFromFilterProperties(selectedFilter?.filterProperties || {}, getString))
      )}
    />
  )
}
 
export default AuditTrailsFilters