All files / modules/85-cv/pages/slos/components/SLODashbordFilters SLODashbordFilters.tsx

73.91% Statements 17/23
81.25% Branches 39/48
33.33% Functions 3/9
72.73% Lines 16/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 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              11x 11x 11x 11x   11x                 11x   11x           20x   20x       20x         20x   20x 20x         20x   20x                                                                                                                                                                         11x  
/*
 * 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, { useCallback, useMemo } from 'react'
import { Button, ButtonVariation, Layout, Select } from '@wings-software/uicore'
import { defaultTo } from 'lodash-es'
import { useStrings } from 'framework/strings'
import type { SLODashbordFiltersProps } from './SLODashboardFilters.types'
import {
  getIsClearFilterDisabled,
  getIsMonitoresServicePageClearFilterDisabled,
  getMonitoredServicesOptionsForFilter,
  getPeriodTypeOptionsForFilter,
  getSliTypeOptionsForFilter,
  getUserJourneyOptionsForFilter,
  SLODashboardFilterActions
} from '../../CVSLOListingPage.utils'
import css from '../../CVSLOsListingPage.module.scss'
 
const SLODashbordFilters: React.FC<SLODashbordFiltersProps> = ({
  filterState,
  dispatch,
  filterItemsData,
  hideMonitoresServicesFilter
}) => {
  const { getString } = useStrings()
 
  const resetFilters = useCallback(() => {
    dispatch(SLODashboardFilterActions.resetFilters())
  }, [])
 
  const resetFiltersInMonitoredServicePage = useCallback(() => {
    dispatch(SLODashboardFilterActions.resetFiltersInMonitoredServicePageAction())
  }, [])
 
  // eslint-disable-next-line react-hooks/exhaustive-deps
  const hideResetFilterButton = useMemo(() => getIsClearFilterDisabled(filterState, getString), [filterState])
 
  const hideMonitoredServiceResetButton = useMemo(
    () => getIsMonitoresServicePageClearFilterDisabled(filterState, getString),
    // eslint-disable-next-line react-hooks/exhaustive-deps
    [filterState]
  )
 
  const { updateUserJourney, updateMonitoredServices, updateTargetType, updateSliType } = SLODashboardFilterActions
 
  return (
    <Layout.Horizontal className={css.sloFilters}>
      <Layout.Vertical width="240px" margin={{ right: 'small' }} data-testid="userJourney-filter">
        <Select
          value={{
            label: `${getString('cv.slos.userJourney')}: ${defaultTo(
              filterState.userJourney?.label,
              getString('all')
            )}`,
            value: defaultTo(filterState.userJourney?.value, getString('all'))
          }}
          items={getUserJourneyOptionsForFilter(filterItemsData.userJourney?.data?.content, getString)}
          onChange={item => {
            dispatch(updateUserJourney({ userJourney: item }))
          }}
        />
      </Layout.Vertical>
      {!hideMonitoresServicesFilter && (
        <Layout.Vertical width="240px" margin={{ right: 'small' }} data-testid="monitoredServices-filter">
          <Select
            value={{
              label: `${getString('cv.monitoredServices.title')}: ${defaultTo(
                filterState.monitoredService?.label,
                getString('all')
              )}`,
              value: defaultTo(filterState.monitoredService?.value, getString('all'))
            }}
            items={getMonitoredServicesOptionsForFilter(filterItemsData.monitoredServices, getString)}
            onChange={item => {
              dispatch(updateMonitoredServices({ monitoredService: item }))
            }}
          />
        </Layout.Vertical>
      )}
      <Layout.Vertical width="240px" margin={{ right: 'small' }} data-testid="sloTargetAndBudget-filter">
        <Select
          value={{
            label: `${getString('cv.slos.sloTargetAndBudget.periodType')}: ${defaultTo(
              filterState.targetTypes?.label,
              getString('all')
            )}`,
            value: defaultTo(filterState.targetTypes?.value, getString('all'))
          }}
          items={getPeriodTypeOptionsForFilter(getString)}
          onChange={item => {
            dispatch(updateTargetType({ targetTypes: item }))
          }}
        />
      </Layout.Vertical>
      <Layout.Vertical width="240px" margin={{ right: 'small' }} data-testid="sliType-filter">
        <Select
          value={{
            label: `${getString('cv.slos.sliType')}: ${defaultTo(filterState.sliTypes?.label, getString('all'))}`,
            value: defaultTo(filterState.sliTypes?.value, getString('all'))
          }}
          items={getSliTypeOptionsForFilter(getString)}
          onChange={item => {
            dispatch(updateSliType({ sliTypes: item }))
          }}
        />
      </Layout.Vertical>
      {!hideMonitoresServicesFilter && !hideResetFilterButton && (
        <Button
          className={css.clearButton}
          variation={ButtonVariation.LINK}
          onClick={resetFilters}
          data-testid="filter-reset"
        >
          {getString('common.filters.clearFilters')}
        </Button>
      )}
      {hideMonitoresServicesFilter && !hideMonitoredServiceResetButton && (
        <Button
          className={css.clearButton}
          variation={ButtonVariation.LINK}
          onClick={resetFiltersInMonitoredServicePage}
          data-testid="filter-reset-monitored-services"
        >
          {getString('common.filters.clearFilters')}
        </Button>
      )}
    </Layout.Horizontal>
  )
}
 
export default SLODashbordFilters