All files / modules/75-cf/pages/feature-flags-detail/targeting-rules-tab/components/flag-enabled-rules-card FlagEnabledRulesCard.tsx

100% Statements 23/23
100% Branches 14/14
100% Functions 3/3
100% Lines 23/23

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 138              3x 3x   3x   3x 3x 3x             3x 3x 3x                                 3x                           70x   70x   70x             70x                           150x       85x 85x                           65x       59x 59x     234x     59x                                                     3x  
/*
 * 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 { Card, Container, FontVariation, Heading, Layout, SelectOption } from '@harness/uicore'
import React, { ReactElement } from 'react'
 
import { useStrings } from 'framework/strings'
import type { Segment, Target, Variation } from 'services/cf'
import DefaultRules from '../default-rules/DefaultRules'
import SpecificTargetingItem from '../specific-targeting-item/SpecificTargetingItem'
import {
  FormVariationMap,
  VariationPercentageRollout,
  TargetingRuleItemType,
  VariationColorMap,
  TargetingRuleItemStatus
} from '../../types'
import PercentageRolloutItem from '../percentage-rollout-item/PercentageRolloutItem'
import AddTargetingButton from '../add-targeting-button/AddTargetingButton'
import useAvailableTargeting from './hooks/useAvailableTargeting'
 
export interface FlagEnabledRulesCardProps {
  targets: Target[]
  segments: Segment[]
  targetingRuleItems: (VariationPercentageRollout | FormVariationMap)[]
  featureFlagVariations: Variation[]
  variationColorMap: VariationColorMap
  disabled: boolean
  refetchSegments: (searchTerm: string) => void
  refetchTargets: (searchTerm: string) => void
  addVariation: (newVariation: Variation) => void
  removeVariation: (removedVariationIndex: number) => void
  addPercentageRollout: () => void
  removePercentageRollout: (index: number) => void
}
 
const FlagEnabledRulesCard = (props: FlagEnabledRulesCardProps): ReactElement => {
  const {
    targets,
    segments,
    targetingRuleItems,
    featureFlagVariations,
    variationColorMap,
    refetchSegments,
    refetchTargets,
    addVariation,
    removeVariation,
    addPercentageRollout,
    removePercentageRollout,
    disabled
  } = props
 
  const { getString } = useStrings()
 
  const { availableSegments, availableTargets, targetingDropdownVariations } = useAvailableTargeting({
    featureFlagVariations,
    segments,
    targetingRuleItems,
    targets
  })
 
  return (
    <Card data-testid="flag-enabled-rules-card">
      <Container border={{ bottom: true }} padding={{ bottom: 'medium' }} margin={{ bottom: 'medium' }}>
        <DefaultRules
          featureFlagVariations={featureFlagVariations}
          inputName="onVariation"
          titleStringId="cf.featureFlags.rules.whenFlagEnabled"
        />
      </Container>
      <Layout.Vertical spacing="medium" id="rules-container">
        <Heading level={4} font={{ variation: FontVariation.BODY2 }}>
          {getString('cf.featureFlags.rules.specificTargeting')}
        </Heading>
        {targetingRuleItems.map((targetingRuleItem, index) => {
          if (
            targetingRuleItem.type === TargetingRuleItemType.VARIATION &&
            targetingRuleItem.status !== TargetingRuleItemStatus.DELETED
          ) {
            const item = targetingRuleItem as FormVariationMap
            return (
              <SpecificTargetingItem
                key={`${item.variationIdentifier}_${index}`}
                index={index}
                variationColorMap={variationColorMap}
                disabled={disabled}
                targets={availableTargets}
                segments={availableSegments}
                removeVariation={removeVariation}
                refetchSegments={refetchSegments}
                refetchTargets={refetchTargets}
                formVariationMapItem={item}
              />
            )
          } else if (
            targetingRuleItem.type === TargetingRuleItemType.PERCENTAGE_ROLLOUT &&
            targetingRuleItem.status !== TargetingRuleItemStatus.DELETED
          ) {
            const item = targetingRuleItem as VariationPercentageRollout
            const initialOption: SelectOption = {
              value: item.clauses[0].values[0],
              label:
                segments.find(segment => segment.identifier === item.clauses[0].values[0])?.name ||
                item.clauses[0].values[0]
            }
            return (
              <PercentageRolloutItem
                key={item.ruleId}
                disabled={disabled}
                index={index}
                initialOption={initialOption}
                featureFlagVariations={featureFlagVariations}
                removePercentageRollout={removePercentageRollout}
                segments={availableSegments}
                variationPercentageRollout={item}
              />
            )
          }
        })}
 
        <AddTargetingButton
          addPercentageRollout={addPercentageRollout}
          targetingDropdownVariations={targetingDropdownVariations}
          addVariation={addVariation}
          variationColorMap={variationColorMap}
          disabled={disabled}
        />
      </Layout.Vertical>
    </Card>
  )
}
 
export default FlagEnabledRulesCard