Skip to content

Commit 94e25f7

Browse files
authored
fix: render all top-level segment rules in the UI instead (#7342)
1 parent 1bb5667 commit 94e25f7

1 file changed

Lines changed: 41 additions & 38 deletions

File tree

frontend/web/components/modals/CreateSegment.tsx

Lines changed: 41 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,11 @@ type CreateSegmentType = {
7878
type CreateSegmentError = {
7979
status: number
8080
data: {
81-
rules: [
82-
{
83-
rules: Array<{
84-
conditions: SegmentConditionsError[]
85-
}>
86-
},
87-
]
81+
rules: Array<{
82+
rules: Array<{
83+
conditions: SegmentConditionsError[]
84+
}>
85+
}>
8886
}
8987
}
9088

@@ -227,7 +225,7 @@ const CreateSegment: FC<CreateSegmentType> = ({
227225
newValue: SegmentRule,
228226
) => {
229227
const newRules = cloneDeep(rules)
230-
newRules[0].rules[elementNumber] = newValue
228+
newRules[rulesIndex].rules[elementNumber] = newValue
231229
setRules(newRules)
232230
}
233231

@@ -336,10 +334,11 @@ const CreateSegment: FC<CreateSegmentType> = ({
336334
return () => setInterceptClose(null)
337335
}, [onClosing])
338336
const isValid = useMemo(() => {
339-
if (!rules[0]?.rules?.find((v) => !v.delete)) {
337+
const allSubRules = rules.flatMap((r) => r.rules)
338+
if (!allSubRules.find((v) => !v.delete)) {
340339
return false
341340
}
342-
const res = rules[0].rules.find((v) =>
341+
const res = allSubRules.find((v) =>
343342
v.conditions.find((c) => !Utils.validateRule(c)),
344343
)
345344
return !res
@@ -400,38 +399,42 @@ const CreateSegment: FC<CreateSegmentType> = ({
400399
}
401400
return warnings
402401
}, [operators, rules])
403-
//Find any non-deleted rules
404-
const hasNoRules = !rules[0]?.rules?.find((v) => !v.delete)
405-
const rulesToShow = rules[0].rules.filter((v) => !v.delete)
402+
const allSubRules = rules.flatMap((r) => r.rules)
403+
const hasNoRules = !allSubRules.find((v) => !v.delete)
404+
const rulesToShow = allSubRules.filter((v) => !v.delete)
406405
const rulesEl = (
407406
<div className='overflow-visible'>
408407
<div>
409408
<div className='mb-4'>
410-
{rules[0].rules.map((rule, i) => {
411-
if (rule.delete || !operators) {
412-
return null
413-
}
414-
const displayIndex = rulesToShow.indexOf(rule)
415-
return (
416-
<div key={i}>
417-
<SegmentRuleDivider rule={rule} index={displayIndex} />
418-
<Rule
419-
showDescription={showDescriptions}
420-
readOnly={readOnly}
421-
data-test={`rule-${displayIndex}`}
422-
rule={rule}
423-
index={i}
424-
operators={operators}
425-
onChange={(v: SegmentRule) => {
426-
setValueChanged(true)
427-
updateRule(0, i, v)
428-
}}
429-
errors={error?.data?.rules?.[0]?.rules?.[i]?.conditions}
430-
projectId={projectId}
431-
/>
432-
</div>
433-
)
434-
})}
409+
{rules.map((topRule, rulesIndex) =>
410+
topRule.rules.map((rule, i) => {
411+
if (rule.delete || !operators) {
412+
return null
413+
}
414+
const displayIndex = rulesToShow.indexOf(rule)
415+
return (
416+
<div key={`${rulesIndex}-${i}`}>
417+
<SegmentRuleDivider rule={rule} index={displayIndex} />
418+
<Rule
419+
showDescription={showDescriptions}
420+
readOnly={readOnly}
421+
data-test={`rule-${displayIndex}`}
422+
rule={rule}
423+
index={i}
424+
operators={operators}
425+
onChange={(v: SegmentRule) => {
426+
setValueChanged(true)
427+
updateRule(rulesIndex, i, v)
428+
}}
429+
errors={
430+
error?.data?.rules?.[rulesIndex]?.rules?.[i]?.conditions
431+
}
432+
projectId={projectId}
433+
/>
434+
</div>
435+
)
436+
}),
437+
)}
435438
</div>
436439
{hasNoRules && (
437440
<InfoMessage>

0 commit comments

Comments
 (0)