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

100% Statements 26/26
70.97% Branches 22/31
100% Functions 5/5
100% Lines 26/26

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              4x 4x   4x   4x 4x                       4x                   23x   23x 8x 8x 8x 8x 8x     23x 11x 3x     11x 29x   29x 7x       11x 1x         11x     23x           105x             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, useEffect, useState } from 'react'
import { get } from 'lodash-es'
import type { Segment, TargetAttributesResponse, Variation } from 'services/cf'
import PercentageRollout from '@cf/components/PercentageRollout/PercentageRollout'
import type { FlagConfigurationStepFormDataValues } from '@cf/components/PipelineSteps/FlagConfigurationStep/types'
import SubSection, { SubSectionProps } from '../SubSection'
import { CFPipelineInstructionType } from '../../types'
 
export interface ServePercentageRolloutProps extends SubSectionProps {
  targetGroups?: Segment[]
  variations?: Variation[]
  fieldValues?: FlagConfigurationStepFormDataValues
  targetAttributes?: TargetAttributesResponse
  clearField: (fieldName: string) => void
  setField: (fieldName: string, value: unknown) => void
  prefix: (fieldName: string) => string
}
 
const ServePercentageRollout: FC<ServePercentageRolloutProps> = ({
  variations = [],
  targetGroups = [],
  fieldValues,
  targetAttributes = [],
  clearField,
  setField,
  prefix,
  ...props
}) => {
  const [initialLoad, setInitialLoad] = useState<boolean>(true)
 
  useEffect(() => {
    setField('identifier', 'AddRuleIdentifier')
    setField('type', CFPipelineInstructionType.ADD_RULE)
    setField('spec.priority', 100)
    setField('spec.distribution.clauses[0].op', 'segmentMatch')
    setField('spec.distribution.clauses[0].attribute', '')
  }, [])
 
  useEffect(() => {
    if (!initialLoad) {
      clearField('spec.distribution.variations')
    }
 
    variations.forEach(({ identifier }, index) => {
      setField(`spec.distribution.variations[${index}].variation`, identifier)
 
      if (!initialLoad) {
        setField(`spec.distribution.variations[${index}].weight`, Math.floor(100 / variations?.length || 1))
      }
    })
 
    if (!initialLoad && variations?.length % 2) {
      setField(
        `spec.distribution.variations[${variations?.length - 1}].weight`,
        Math.ceil(100 / variations?.length || 1)
      )
    }
    setInitialLoad(false)
  }, [variations, setInitialLoad])
 
  return (
    <SubSection data-testid="flagChanges-servePercentageRollout" {...props}>
      <PercentageRollout
        targetGroups={targetGroups}
        variations={variations}
        fieldValues={get(fieldValues, prefix('spec.distribution'))}
        prefix={(fieldName: string) => prefix(`spec.distribution.${fieldName}`)}
        bucketByAttributes={targetAttributes}
      />
    </SubSection>
  )
}
 
export default ServePercentageRollout