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

100% Statements 33/33
100% Branches 26/26
100% Functions 7/7
100% Lines 32/32

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 139 140 141              5x 5x 5x                 5x 5x   5x   5x   5x 5x   5x                         5x                   16x   16x 16x       16x   16x 16x 1x       16x   16x 16x 16x 16x     16x 16x 2x     14x 11x     3x     16x                     2x                                                                                   5x  
/*
 * 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, useCallback, useEffect, useMemo } from 'react'
import { get } from 'lodash-es'
import {
  Container,
  getMultiTypeFromValue,
  Heading,
  Layout,
  MultiTypeInputType,
  RUNTIME_INPUT_VALUE,
  Text
} from '@wings-software/uicore'
import { FontVariation } from '@harness/design-system'
import { useStrings } from 'framework/strings'
import type { Feature } from 'services/cf'
import MultiTypeSelectorButton from '@common/components/MultiTypeSelectorButton/MultiTypeSelectorButton'
import type { FeatureFlagConfigurationInstruction, FlagConfigurationStepFormDataValues } from '../types'
import FlagChangesForm, { FlagChangesFormProps } from './FlagChangesForm'
 
import subSectionCSS from './SubSection.module.scss'
import css from './FlagChanges.module.scss'
 
const allowedTypes = [MultiTypeInputType.FIXED, MultiTypeInputType.RUNTIME]
 
export interface FlagChangesProps {
  selectedFeature?: Feature | typeof RUNTIME_INPUT_VALUE
  selectedEnvironmentId?: string
  initialInstructions?: FeatureFlagConfigurationInstruction[] | typeof RUNTIME_INPUT_VALUE
  clearField: (fieldName: string) => void
  setField: (fieldName: string, value: unknown) => void
  fieldValues: FlagConfigurationStepFormDataValues
  showRuntimeFixedSelector?: boolean
  pathPrefix?: string
}
 
const FlagChanges: FC<FlagChangesProps> = ({
  selectedFeature,
  selectedEnvironmentId,
  fieldValues,
  initialInstructions,
  clearField,
  setField,
  showRuntimeFixedSelector = false,
  pathPrefix = ''
}) => {
  const { getString } = useStrings()
 
  const prefix = useCallback<(path: string) => string>(
    path => (pathPrefix ? `${pathPrefix}.${path}` : path),
    [pathPrefix]
  )
 
  const instructionsPath = useMemo<string>(() => prefix('spec.instructions'), [prefix])
 
  useEffect(() => {
    if (selectedFeature === RUNTIME_INPUT_VALUE) {
      setField(instructionsPath, RUNTIME_INPUT_VALUE)
    }
  }, [selectedFeature, instructionsPath])
 
  const subsectionsDisabled = showRuntimeFixedSelector && get(fieldValues, instructionsPath) === RUNTIME_INPUT_VALUE
 
  enum UI_STATE {
    RUNTIME,
    FLAG_OR_ENV_NOT_SELECTED,
    DISPLAY_FORM
  }
 
  const status = useMemo<UI_STATE>(() => {
    if (subsectionsDisabled) {
      return UI_STATE.RUNTIME
    }
 
    if (!selectedFeature || !selectedEnvironmentId) {
      return UI_STATE.FLAG_OR_ENV_NOT_SELECTED
    }
 
    return UI_STATE.DISPLAY_FORM
  }, [subsectionsDisabled, selectedFeature, selectedEnvironmentId])
 
  return (
    <Layout.Vertical spacing="medium">
      <Layout.Horizontal spacing="small" flex={{ alignItems: 'center' }}>
        <Heading level={5} font={{ variation: FontVariation.H5 }}>
          {getString('cf.pipeline.flagConfiguration.flagChanges')}
        </Heading>
        {showRuntimeFixedSelector && (
          <MultiTypeSelectorButton
            type={getMultiTypeFromValue(get(fieldValues, instructionsPath), allowedTypes)}
            allowedTypes={allowedTypes}
            onChange={type =>
              setField(instructionsPath, type === MultiTypeInputType.RUNTIME ? RUNTIME_INPUT_VALUE : undefined)
            }
            data-testid="runtime-fixed-selector-button"
            disabled={selectedFeature === RUNTIME_INPUT_VALUE}
          />
        )}
      </Layout.Horizontal>
 
      {status === UI_STATE.RUNTIME && (
        <Container className={subSectionCSS.subSection} padding="large" data-testid="flag-changes-runtime">
          <Text>{getString('cf.pipeline.flagConfiguration.flagChangesRuntime')}</Text>
          <ul className={css.runtimeList}>
            <li>{getString('cf.pipeline.flagConfiguration.flagChangesRuntimeSetFlagSwitch')}</li>
            <li>{getString('cf.pipeline.flagConfiguration.flagChangesRuntimeSetDefaultRules')}</li>
            <li>{getString('cf.pipeline.flagConfiguration.flagChangesRuntimeServeVariationToTargets')}</li>
            <li>{getString('cf.pipeline.flagConfiguration.flagChangesRuntimeServeVariationToTargetGroups')}</li>
            <li>{getString('cf.pipeline.flagConfiguration.flagChangesRuntimeServePercentageRollout')}</li>
          </ul>
        </Container>
      )}
 
      {status === UI_STATE.FLAG_OR_ENV_NOT_SELECTED && (
        <Container className={subSectionCSS.subSection} padding="large" data-testid="flag-changes-no-flag-selected">
          <Text>{getString('cf.pipeline.flagConfiguration.pleaseSelectAFeatureFlag')}</Text>
        </Container>
      )}
 
      {status === UI_STATE.DISPLAY_FORM && (
        <FlagChangesForm
          prefix={prefix}
          initialInstructions={initialInstructions as FlagChangesFormProps['initialInstructions']}
          clearField={clearField}
          setField={setField}
          fieldValues={fieldValues}
          selectedFeature={selectedFeature as FlagChangesFormProps['selectedFeature']}
          environmentIdentifier={selectedEnvironmentId as string}
        />
      )}
    </Layout.Vertical>
  )
}
 
export default FlagChanges