From c3bf3bf2b25639ac46ed9008f9dfad359940cf9d Mon Sep 17 00:00:00 2001 From: Kyle Johnson Date: Tue, 11 Jun 2024 08:39:19 +0100 Subject: [PATCH] feat: Add default rule for segments (#4095) --- frontend/e2e/helpers.cafe.ts | 1 - .../web/components/modals/CreateSegment.tsx | 92 ++++++++++--------- 2 files changed, 49 insertions(+), 44 deletions(-) diff --git a/frontend/e2e/helpers.cafe.ts b/frontend/e2e/helpers.cafe.ts index 300f9aede73b..9e15b9c4c2e4 100644 --- a/frontend/e2e/helpers.cafe.ts +++ b/frontend/e2e/helpers.cafe.ts @@ -433,7 +433,6 @@ export const createSegment = async ( rules?: Rule[], ) => { await click(byId('show-create-segment-btn')) - await click(byId('add-rule')) await setText(byId('segmentID'), id) for (let x = 0; x < rules.length; x++) { const rule = rules[x] diff --git a/frontend/web/components/modals/CreateSegment.tsx b/frontend/web/components/modals/CreateSegment.tsx index f98da72548ac..eb66c1fc3a7b 100644 --- a/frontend/web/components/modals/CreateSegment.tsx +++ b/frontend/web/components/modals/CreateSegment.tsx @@ -114,7 +114,13 @@ const CreateSegment: FC = ({ rules: [ { conditions: [], - rules: [], + rules: [ + { + conditions: [{ ...Constants.defaultRule }], + rules: [], + type: 'ANY', + }, + ], type: 'ALL', }, ], @@ -313,49 +319,51 @@ const CreateSegment: FC = ({ }, [operators, rules]) //Find any non-deleted rules const hasNoRules = !rules[0]?.rules?.find((v) => !v.delete) - + const rulesToShow = rules[0].rules.filter((v) => !v.delete) const rulesEl = (
- {rules[0].rules - ?.filter((v) => !v?.delete) - .map((rule, i) => { - return ( -
- - - {Format.camelCase( - `${i > 0 ? 'And ' : ''}${ - rule.type === 'ANY' - ? 'Any of the following' - : 'None of the following' - }`, - )} - - - { - setValueChanged(true) - removeRule(0, i) - }} - onChange={(v: SegmentRule) => { - setValueChanged(true) - updateRule(0, i, v) - }} - /> -
- ) - })} + {rules[0].rules.map((rule, i) => { + if (rule.delete) { + return null + } + const displayIndex = rulesToShow.indexOf(rule) + return ( +
+ + + {Format.camelCase( + `${displayIndex > 0 ? 'And ' : ''}${ + rule.type === 'ANY' + ? 'Any of the following' + : 'None of the following' + }`, + )} + + + { + setValueChanged(true) + removeRule(0, i) + }} + onChange={(v: SegmentRule) => { + setValueChanged(true) + updateRule(0, i, v) + }} + /> +
+ ) + })}
{hasNoRules && ( @@ -457,9 +465,7 @@ const CreateSegment: FC = ({ style={{ fontWeight: 'normal', marginLeft: '12px' }} className='mb-0 fs-small text-dark' > - {showDescriptions - ? 'Hide condition descriptions' - : 'Show condition descriptions'} + Show condition descriptions {metadataEnable && segmentContentType?.id && (