All files / modules/75-cf/components/PipelineSteps/FlagConfigurationStep/FlagChanges/subSections ServeVariationToIndividualTarget.tsx

100% Statements 16/16
62.5% Branches 5/8
100% Functions 3/3
100% Lines 15/15

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              4x 4x 4x     4x 4x 4x                   4x               10x   10x 8x   8x 6x     6x     10x                                         4x  
/*
 * 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, { FC, useMemo } from 'react'
import { get } from 'lodash-es'
import { useStrings } from 'framework/strings'
import type { Target, Variation } from 'services/cf'
import type { FlagConfigurationStepFormDataValues } from '@cf/components/PipelineSteps/FlagConfigurationStep/types'
import SubSection, { SubSectionProps } from '../SubSection'
import ServeVariationToItem from './ServeVariationToItem'
import { CFPipelineInstructionType } from '../../types'
 
export interface ServeVariationToIndividualTargetProps extends SubSectionProps {
  setField: (fieldName: string, value: unknown) => void
  prefix: (fieldName: string) => string
  variations?: Variation[]
  targets?: Target[]
  fieldValues?: FlagConfigurationStepFormDataValues
}
 
const ServeVariationToIndividualTarget: FC<ServeVariationToIndividualTargetProps> = ({
  fieldValues = {},
  variations = [],
  targets = [],
  setField,
  prefix,
  ...props
}) => {
  const { getString } = useStrings()
 
  const selectedTargets = useMemo<Target[]>(() => {
    const selectedTargetIds = get(fieldValues, prefix('spec.targets'))
 
    if (!Array.isArray(targets) || !Array.isArray(selectedTargetIds) || selectedTargetIds.length === 0) {
      return []
    }
 
    return targets.filter(({ identifier }) => selectedTargetIds.includes(identifier))
  }, [targets, fieldValues])
 
  return (
    <SubSection data-testid="flagChanges-serveVariationToIndividualTarget" {...props}>
      <ServeVariationToItem
        dialogTitle={getString('cf.pipeline.flagConfiguration.addEditVariationToSpecificTargets')}
        itemLabel={getString('cf.shared.targets')}
        itemPlaceholder={getString('cf.pipeline.flagConfiguration.enterTarget')}
        itemFieldName="targets"
        serveItemString={getString('cf.featureFlags.toTarget')}
        serveItemsString={getString('cf.pipeline.flagConfiguration.toTargets')}
        setField={setField}
        items={targets}
        selectedItems={selectedTargets}
        variations={variations}
        selectedVariationId={get(fieldValues, prefix('spec.variation'))}
        instructionType={CFPipelineInstructionType.ADD_TARGETS_TO_VARIATION_TARGET_MAP}
        instructionIdentifier="SetVariationForTarget"
      />
    </SubSection>
  )
}
 
export default ServeVariationToIndividualTarget