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 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 | 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 30x 30x 30x 30x 30x 30x 30x 12x 12x 2x 12x 2x 12x 2x 12x 15x 2x 1x 1x 1x 1x 1x 1x 12x 15x 13x 5x 5x 1x 1x 1x 4x 4x 1x 4x 4x 1x 4x 4x 1x 4x 4x 1x 8x 8x 1x 7x 7x 12x 11x 11x 10x 10x 10x 1x 1x 30x 4x | /* * 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 { useParams } from 'react-router-dom' import { useToaster } from '@harness/uicore' import patch from '@cf/utils/instructions' import { PatchFeatureQueryParams, usePatchFeature, Variation } from 'services/cf' import useActiveEnvironment from '@cf/hooks/useActiveEnvironment' import { showToaster } from '@cf/utils/CFUtils' import { useStrings } from 'framework/strings' import useRBACError from '@rbac/utils/useRBACError/useRBACError' import type { ErrorHandlerProps } from '@rbac/utils/utils' import { FormVariationMap, TargetingRuleItemStatus, TargetingRuleItemType, TargetingRulesFormValues, VariationPercentageRollout } from '../types' import { PatchFeatureFlagUtils } from '../utils/PatchFeatureFlagUtils' export interface UsePatchFeatureFlagProps { featureFlagIdentifier: string initialValues: TargetingRulesFormValues variations: Variation[] refetchFlag: () => Promise<unknown> } interface UsePatchFeatureFlagReturn { saveChanges: (newValues: TargetingRulesFormValues) => void loading: boolean } const usePatchFeatureFlag = ({ featureFlagIdentifier, initialValues, refetchFlag }: UsePatchFeatureFlagProps): UsePatchFeatureFlagReturn => { const { projectIdentifier, orgIdentifier, accountId: accountIdentifier } = useParams<Record<string, string>>() const { activeEnvironment: environmentIdentifier } = useActiveEnvironment() const { getRBACErrorMessage } = useRBACError() const { showError } = useToaster() const { getString } = useStrings() const { mutate: patchFeature, loading } = usePatchFeature({ identifier: featureFlagIdentifier as string, queryParams: { projectIdentifier, environmentIdentifier, accountIdentifier, orgIdentifier } as PatchFeatureQueryParams }) const saveChanges = (submittedValues: TargetingRulesFormValues): void => { const patchFeatureUtils = PatchFeatureFlagUtils(submittedValues, initialValues) // flag state if (patchFeatureUtils.hasFlagStateChanged()) { patchFeatureUtils.createUpdateFlagStateInstruction() } // default ON serves if (patchFeatureUtils.hasDefaultOnVariationChanged()) { patchFeatureUtils.createDefaultServeOnInstruction() } // default OFF serves if (patchFeatureUtils.hasDefaultOffVariationChanged()) { patchFeatureUtils.createDefaultServeOffInstruction() } // API requires delete rule instructions first so handle those initially submittedValues.targetingRuleItems .filter(rule => rule.status === TargetingRuleItemStatus.DELETED) .forEach(rule => { if (rule.type === TargetingRuleItemType.VARIATION) { const item = rule as FormVariationMap patchFeatureUtils.createRemoveTargetGroupsInstructions(item.targetGroups) patchFeatureUtils.createRemoveTargetsInstructions( item.variationIdentifier, item.targets.map(target => target.value) ) } else { const item = rule as VariationPercentageRollout patchFeatureUtils.createRemovePercentageRolloutInstruction(item) } }) // Loop through the non-deleted rules and handle the added/updated ones submittedValues.targetingRuleItems .filter(targetingRule => targetingRule.status !== TargetingRuleItemStatus.DELETED) .forEach((targetingRule: FormVariationMap | VariationPercentageRollout) => { if (targetingRule.type === TargetingRuleItemType.VARIATION) { const item = targetingRule as FormVariationMap if (targetingRule.status === TargetingRuleItemStatus.ADDED) { patchFeatureUtils.createAddTargetGroupInstructions( item.variationIdentifier, item.targetGroups, item.priority ) patchFeatureUtils.createAddTargetsInstructions( item.variationIdentifier, item.targets.map(x => x.value) ) } else { const removedTargetGroups = patchFeatureUtils.removedTargetGroups(item.variationIdentifier) if (removedTargetGroups.length) { patchFeatureUtils.createRemoveTargetGroupsInstructions(removedTargetGroups) } const addedTargetGroups = patchFeatureUtils.addedTargetGroups(item.variationIdentifier) if (addedTargetGroups.length) { patchFeatureUtils.createAddTargetGroupInstructions( item.variationIdentifier, addedTargetGroups, item.priority ) } const removedTargetIds = patchFeatureUtils.removedTargets(item.variationIdentifier) if (removedTargetIds.length) { patchFeatureUtils.createRemoveTargetsInstructions(item.variationIdentifier, removedTargetIds) } const addedTargetIds = patchFeatureUtils.addedTargets(item.variationIdentifier) if (addedTargetIds.length) { patchFeatureUtils.createAddTargetsInstructions(item.variationIdentifier, addedTargetIds) } } } else { const item = targetingRule as VariationPercentageRollout if (item.status === TargetingRuleItemStatus.ADDED) { patchFeatureUtils.createAddPercentageRolloutInstructions(item, item.priority) } else { const updatedPercentageRollouts = patchFeatureUtils.updatedPercentageRollouts() patchFeatureUtils.createUpdatePercentageRolloutInstructions(updatedPercentageRollouts) } } }) // submit request patch.feature.onPatchAvailable(async data => { try { await patchFeature(data) patch.feature.reset() await refetchFlag() showToaster(getString('cf.messages.flagUpdated')) } catch (error: unknown) { patch.feature.reset() showError(getRBACErrorMessage(error as ErrorHandlerProps)) } }) } return { saveChanges, loading } } export default usePatchFeatureFlag |