All files / modules/75-cf/pages/target-group-detail/components/EditTargetGroupCriteria TargetSelect.tsx

100% Statements 17/17
100% Branches 10/10
100% Functions 3/3
100% Lines 17/17

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              4x 4x 4x 4x 4x 4x   4x               4x 65x 65x   65x                 65x         65x 17x       65x                 8x           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 React, { FC, ReactNode, useMemo } from 'react'
import { useParams } from 'react-router-dom'
import { FormInput, SelectOption } from '@wings-software/uicore'
import { useStrings } from 'framework/strings'
import { useGetAllTargets, UseGetAllTargetsProps } from 'services/cf'
import targetToSelectOption from '@cf/utils/targetToSelectOption'
 
import css from './TargetSelect.module.scss'
 
export interface TargetSelectProps {
  environmentIdentifier: string
  fieldName: string
  label: ReactNode
}
 
const TargetSelect: FC<TargetSelectProps> = ({ environmentIdentifier, fieldName, label }) => {
  const { getString } = useStrings()
  const { accountId: accountIdentifier, orgIdentifier, projectIdentifier } = useParams<Record<string, string>>()
 
  const queryParams: UseGetAllTargetsProps['queryParams'] = {
    environmentIdentifier,
    accountIdentifier,
    orgIdentifier,
    projectIdentifier,
    pageSize: 100,
    sortByField: 'name'
  }
 
  const { data: targets, refetch: refetchTargets } = useGetAllTargets({
    queryParams,
    debounce: 300
  })
 
  const targetOptions = useMemo<SelectOption[]>(
    () => (targets?.targets || []).map(targetToSelectOption),
    [targets?.targets]
  )
 
  return (
    <FormInput.MultiSelect
      name={fieldName}
      label={label}
      className={css.input}
      items={targetOptions}
      multiSelectProps={{
        allowCreatingNewItems: false,
        placeholder: getString('cf.segmentDetail.searchTarget'),
        onQueryChange: async query => await refetchTargets({ queryParams: { ...queryParams, targetName: query } })
      }}
    />
  )
}
 
export default TargetSelect