All files / modules/75-cf/components/PipelineSteps/FlagConfigurationStep/FlagChanges/subSections ServeVariationToTargetGroup.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               8x   8x 7x   7x 5x     6x     8x                                         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 { Segment, 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 ServeVariationToTargetGroupProps extends SubSectionProps {
  setField: (fieldName: string, value: unknown) => void
  prefix: (fieldName: string) => string
  variations?: Variation[]
  targetGroups?: Segment[]
  fieldValues?: FlagConfigurationStepFormDataValues
}
 
const ServeVariationToTargetGroup: FC<ServeVariationToTargetGroupProps> = ({
  fieldValues = {},
  variations = [],
  targetGroups = [],
  setField,
  prefix,
  ...props
}) => {
  const { getString } = useStrings()
 
  const selectedTargetGroups = useMemo<Segment[]>(() => {
    const selectedTargetGroupIds = get(fieldValues, prefix('spec.segments'))
 
    if (!Array.isArray(targetGroups) || !Array.isArray(selectedTargetGroupIds) || selectedTargetGroupIds.length === 0) {
      return []
    }
 
    return targetGroups.filter(({ identifier }) => selectedTargetGroupIds.includes(identifier))
  }, [targetGroups, fieldValues])
 
  return (
    <SubSection data-testid="flagChanges-serveVariationToTargetGroup" {...props}>
      <ServeVariationToItem
        dialogTitle={getString('cf.pipeline.flagConfiguration.addEditVariationToTargetGroups')}
        itemLabel={getString('cf.shared.segments')}
        itemPlaceholder={getString('cf.pipeline.flagConfiguration.enterTargetGroup')}
        itemFieldName="segments"
        serveItemString={getString('cf.pipeline.flagConfiguration.toTargetGroup')}
        serveItemsString={getString('cf.pipeline.flagConfiguration.toTargetGroups')}
        setField={setField}
        items={targetGroups}
        selectedItems={selectedTargetGroups}
        variations={variations}
        selectedVariationId={get(fieldValues, prefix('spec.variation'))}
        instructionType={CFPipelineInstructionType.ADD_SEGMENT_TO_VARIATION_TARGET_MAP}
        instructionIdentifier="SetVariationForGroup"
      />
    </SubSection>
  )
}
 
export default ServeVariationToTargetGroup