All files / modules/75-cf/components/EditFlagTabs TabTargeting.tsx

100% Statements 29/29
85.11% Branches 40/47
100% Functions 4/4
100% Lines 28/28

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              3x 3x   3x 3x 3x 3x 3x   3x 3x 3x 3x 3x   3x                         3x                   16x 15x 15x   15x   15x                   15x 11x     15x 2x 2x       15x       15x                         2x                                                                                         3x  
/*
 * 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, { useEffect, useState } from 'react'
import { FlexExpander, Layout } from '@wings-software/uicore'
import type { FormikProps } from 'formik'
import { ResourceType } from '@rbac/interfaces/ResourceType'
import { PermissionIdentifier } from '@rbac/interfaces/PermissionIdentifier'
import RbacButton from '@rbac/components/Button/Button'
import { FeatureFlagActivationStatus } from '@cf/utils/CFUtils'
import { useStrings } from 'framework/strings'
import type { Feature } from 'services/cf'
import { TargetAttributesProvider } from '@cf/hooks/useTargetAttributes'
import usePlanEnforcement from '@cf/hooks/usePlanEnforcement'
import { FeatureIdentifier } from 'framework/featureStore/FeatureIdentifier'
import CustomRulesView from './CustomRulesView'
import { DefaultRulesView } from './DefaultRulesView'
import type { FlagActivationFormValues } from '../FlagActivation/FlagActivation'
import FlagToggleSwitch from './FlagToggleSwitch'
 
export interface TabTargetingProps {
  feature: Feature
  formikProps: FormikProps<FlagActivationFormValues>
  editing: boolean
  setEditing: (flag: boolean) => void
  environmentIdentifier: string
  projectIdentifier: string
  orgIdentifier: string
  accountIdentifier: string
}
 
const TabTargeting: React.FC<TabTargetingProps> = props => {
  const {
    feature,
    formikProps,
    editing,
    setEditing,
    environmentIdentifier,
    projectIdentifier,
    orgIdentifier,
    accountIdentifier
  } = props
  const [isEditRulesOn, setEditRulesOn] = useState(false)
  const { getString } = useStrings()
 
  const { isPlanEnforcementEnabled } = usePlanEnforcement()
 
  const planEnforcementProps = isPlanEnforcementEnabled
    ? {
        featuresProps: {
          featuresRequest: {
            featureNames: [FeatureIdentifier.MAUS]
          }
        }
      }
    : undefined
 
  useEffect(() => {
    if (!editing && isEditRulesOn) setEditRulesOn(false)
  }, [editing, isEditRulesOn])
 
  const onEditBtnHandler = (): void => {
    setEditRulesOn(!isEditRulesOn)
    setEditing(true)
  }
 
  const showCustomRules =
    editing ||
    (feature?.envProperties?.rules?.length || 0) > 0 ||
    (feature?.envProperties?.variationMap?.length || 0) > 0
 
  return (
    <TargetAttributesProvider
      projectIdentifier={projectIdentifier}
      orgIdentifier={orgIdentifier}
      accountIdentifier={accountIdentifier}
      environmentIdentifier={environmentIdentifier}
    >
      <Layout.Vertical padding={{ left: 'xxlarge', right: 'large', bottom: 'large' }} spacing="medium">
        <Layout.Horizontal flex={{ alignItems: 'center' }}>
          <FlagToggleSwitch
            currentState={formikProps.values.state}
            currentEnvironmentState={feature.envProperties?.state}
            handleToggle={() =>
              formikProps.setFieldValue(
                'state',
                formikProps.values.state === FeatureFlagActivationStatus.OFF
                  ? FeatureFlagActivationStatus.ON
                  : FeatureFlagActivationStatus.OFF
              )
            }
          />
          <FlexExpander />
          <RbacButton
            text={getString('cf.featureFlags.rules.editRules')}
            icon="edit"
            onClick={onEditBtnHandler}
            style={{
              visibility: isEditRulesOn ? 'hidden' : undefined
            }}
            disabled={feature.archived}
            permission={{
              resource: {
                resourceType: ResourceType.ENVIRONMENT,
                resourceIdentifier: environmentIdentifier
              },
              permission: PermissionIdentifier.EDIT_FF_FEATUREFLAG
            }}
            {...planEnforcementProps}
          />
        </Layout.Horizontal>
 
        <Layout.Vertical spacing="medium">
          <DefaultRulesView formikProps={formikProps} editing={isEditRulesOn} variations={feature.variations} />
          {showCustomRules && (
            <CustomRulesView
              feature={feature}
              editing={isEditRulesOn}
              formikProps={formikProps}
              environmentIdentifier={environmentIdentifier}
              projectIdentifier={projectIdentifier}
            />
          )}
        </Layout.Vertical>
      </Layout.Vertical>
    </TargetAttributesProvider>
  )
}
 
export default TabTargeting