Skip to content

Commit 4270d2a

Browse files
Zaimwa9talissoncosta
authored andcommitted
fix: extracted-mv-mapping-into-utils
1 parent 7118a89 commit 4270d2a

3 files changed

Lines changed: 56 additions & 51 deletions

File tree

frontend/common/providers/FeatureListProvider.js

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React from 'react'
22
import FeatureListStore from 'common/stores/feature-list-store'
33
import ProjectStore from 'common/stores/project-store'
4+
import Utils from 'common/utils/utils'
45

56
const FeatureListProvider = class extends React.Component {
67
static displayName = 'FeatureListProvider'
@@ -147,19 +148,9 @@ const FeatureListProvider = class extends React.Component {
147148
projectFlag,
148149
{
149150
...environmentFlag,
150-
multivariate_feature_state_values: flag.multivariate_options?.map(
151-
(v) => {
152-
const existing =
153-
environmentFlag.multivariate_feature_state_values?.find(
154-
(e) => e.multivariate_feature_option === v.id,
155-
)
156-
return {
157-
multivariate_feature_option: v.id,
158-
percentage_allocation:
159-
existing?.percentage_allocation ??
160-
v.default_percentage_allocation,
161-
}
162-
},
151+
multivariate_feature_state_values: Utils.mapMvOptionsToStateValues(
152+
flag.multivariate_options,
153+
environmentFlag.multivariate_feature_state_values,
163154
),
164155
},
165156
segmentOverrides,
@@ -229,19 +220,9 @@ const FeatureListProvider = class extends React.Component {
229220
newProjectFlag,
230221
{
231222
...environmentFlag,
232-
multivariate_feature_state_values: flag.multivariate_options?.map(
233-
(v) => {
234-
const existing =
235-
environmentFlag.multivariate_feature_state_values?.find(
236-
(e) => e.multivariate_feature_option === v.id,
237-
)
238-
return {
239-
multivariate_feature_option: v.id,
240-
percentage_allocation:
241-
existing?.percentage_allocation ??
242-
v.default_percentage_allocation,
243-
}
244-
},
223+
multivariate_feature_state_values: Utils.mapMvOptionsToStateValues(
224+
flag.multivariate_options,
225+
environmentFlag.multivariate_feature_state_values,
245226
),
246227
},
247228
segmentOverrides,

frontend/common/stores/feature-list-store.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,9 @@ const convertSegmentOverrideToFeatureState = (
6060
feature_state_value: override.value,
6161
id: override.id,
6262
live_from: changeRequest?.live_from,
63-
multivariate_feature_state_values: override.multivariate_options?.map(
64-
(v) => ({
65-
multivariate_feature_option: v.id,
66-
percentage_allocation: v.default_percentage_allocation,
67-
}),
63+
multivariate_feature_state_values: Utils.mapMvOptionsToStateValues(
64+
override.multivariate_options,
65+
override.multivariate_feature_state_values,
6866
),
6967
toRemove: override.toRemove,
7068
} as Partial<FeatureState>

frontend/common/utils/utils.tsx

Lines changed: 46 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,13 @@ const Utils = Object.assign({}, require('./base/_utils'), {
7979
const variation =
8080
variations &&
8181
variations.find((env) => env.multivariate_feature_option === v.id)
82-
total += variation
83-
? variation.percentage_allocation
84-
: typeof v.default_percentage_allocation === 'number'
85-
? v.default_percentage_allocation
86-
: (v as any).percentage_allocation
82+
if (variation) {
83+
total += variation.percentage_allocation
84+
} else if (typeof v.default_percentage_allocation === 'number') {
85+
total += v.default_percentage_allocation
86+
} else {
87+
total += (v as any).percentage_allocation
88+
}
8789
return null
8890
})
8991
return 100 - total
@@ -157,15 +159,21 @@ const Utils = Object.assign({}, require('./base/_utils'), {
157159
displayLimitAlert(type: string, percentage: number | undefined) {
158160
const envOrProject =
159161
type === 'segment overrides' ? 'environment' : 'project'
160-
return percentage >= 100 ? (
161-
<ErrorMessage
162-
error={`Your ${envOrProject} reached the limit of ${type}, please contact support to discuss increasing this limit.`}
163-
/>
164-
) : percentage ? (
165-
<WarningMessage
166-
warningMessage={`Your ${envOrProject} is using ${percentage}% of the total allowance of ${type}.`}
167-
/>
168-
) : null
162+
if (percentage >= 100) {
163+
return (
164+
<ErrorMessage
165+
error={`Your ${envOrProject} reached the limit of ${type}, please contact support to discuss increasing this limit.`}
166+
/>
167+
)
168+
}
169+
if (percentage) {
170+
return (
171+
<WarningMessage
172+
warningMessage={`Your ${envOrProject} is using ${percentage}% of the total allowance of ${type}.`}
173+
/>
174+
)
175+
}
176+
return null
169177
},
170178
escapeHtml(html: string) {
171179
const text = document.createTextNode(html)
@@ -477,11 +485,13 @@ const Utils = Object.assign({}, require('./base/_utils'), {
477485
},
478486
getPlansPermission: (feature: PaidFeature) => {
479487
const isOrgPermission = feature !== '2FA'
480-
const plans = isOrgPermission
481-
? AccountStore.getActiveOrgPlan()
482-
? [AccountStore.getActiveOrgPlan()]
483-
: null
484-
: AccountStore.getPlans()
488+
let plans: string[] | null
489+
if (isOrgPermission) {
490+
const activeOrgPlan = AccountStore.getActiveOrgPlan()
491+
plans = activeOrgPlan ? [activeOrgPlan] : null
492+
} else {
493+
plans = AccountStore.getPlans()
494+
}
485495

486496
if (!plans || !plans.length) {
487497
return false
@@ -669,7 +679,23 @@ const Utils = Object.assign({}, require('./base/_utils'), {
669679
head.appendChild(script)
670680
})
671681
},
672-
682+
mapMvOptionsToStateValues(
683+
mvOptions: MultivariateOption[],
684+
existingMvFeatureStateValues: MultivariateFeatureStateValue[],
685+
): MultivariateFeatureStateValue[] {
686+
return mvOptions?.map((mvOption) => {
687+
const existing = existingMvFeatureStateValues?.find(
688+
(e) => e.multivariate_feature_option === mvOption.id,
689+
)
690+
return {
691+
id: mvOption.id,
692+
multivariate_feature_option: mvOption.id,
693+
percentage_allocation:
694+
existing?.percentage_allocation ??
695+
mvOption.default_percentage_allocation,
696+
}
697+
})
698+
},
673699
numberWithCommas(x: number) {
674700
if (typeof x !== 'number') return ''
675701
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',')

0 commit comments

Comments
 (0)