Skip to content

Commit e3f90f0

Browse files
committed
chore: allow ifthen and foreach in different branches
1 parent b9ad250 commit e3f90f0

File tree

4 files changed

+54
-63
lines changed

4 files changed

+54
-63
lines changed

packages/frontend/src/components/FlowStep/components/DuplicateStepButton.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export default function DuplicateStepButton(props: DuplicateStepButtonProps) {
2323
const { flow, isMobile, onDrawerOpen, setCurrentStepId } =
2424
useContext(EditorContext)
2525

26+
// TODO: use onCreateStep from EditorContext instead
2627
const [createStep] = useMutation(CREATE_STEP, { refetchQueries: [GET_FLOW] })
2728
const onDuplicateStep = useCallback(async () => {
2829
const duplicateConfig = {

packages/frontend/src/components/FlowStepConfigurationModal/ChooseAppAndEvent/ChooseApp.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ export default function ChooseApp(props: ChooseAppProps) {
5454
prevStepId,
5555
})
5656

57+
console.log(appSelectableMap)
58+
5759
const [_, isInitializingIfThen] = useIfThenInitializer()
5860
const isLoading = isInitializingIfThen
5961

Lines changed: 36 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import { IFlow, IStep } from '@plumber/types'
1+
import { IStep } from '@plumber/types'
22

33
import { useContext } from 'react'
44

55
import { BranchContext } from '@/components/FlowStepGroup/Content/IfThen/BranchContext'
66
import { NESTED_IFTHEN_FEATURE_FLAG } from '@/config/flags'
7-
import { EditorContext } from '@/contexts/Editor'
87
import { LaunchDarklyContext } from '@/contexts/LaunchDarkly'
8+
import { StepsToDisplayContext } from '@/contexts/StepsToDisplay'
99
import { TOOLBOX_ACTIONS } from '@/helpers/toolbox'
1010

1111
/**
@@ -16,68 +16,26 @@ import { TOOLBOX_ACTIONS } from '@/helpers/toolbox'
1616
*
1717
*/
1818
function isDelaySelectable({
19-
flow,
19+
hasForEach,
20+
groupedSteps,
2021
step,
2122
prevStepId,
2223
}: {
23-
flow: IFlow
24+
hasForEach: boolean
25+
groupedSteps: IStep[][]
2426
step?: IStep
2527
prevStepId?: string
2628
}) {
27-
const forEachStepPosition = flow?.steps.find(
28-
(s) => s.key === TOOLBOX_ACTIONS.ForEach,
29-
)?.position
30-
31-
let prevStepPosition = null
32-
if (step) {
33-
prevStepPosition = step.position
34-
} else if (prevStepId) {
35-
prevStepPosition = flow?.steps?.find((s) => s.id === prevStepId)?.position
36-
}
37-
38-
let isDelaySelectable: boolean
39-
if (!forEachStepPosition) {
40-
isDelaySelectable = true
41-
} else if (forEachStepPosition && prevStepPosition) {
42-
isDelaySelectable = prevStepPosition < forEachStepPosition
43-
} else {
44-
isDelaySelectable = false
29+
if (!hasForEach) {
30+
return true
4531
}
46-
47-
return isDelaySelectable
48-
}
49-
50-
/**
51-
* Helper function to check if For-each action should be selectable; supports edge
52-
* case in ChooseEvent component.
53-
*
54-
* For-each should only be selectable if:
55-
* - We're the last step.
56-
* - We are not inside a for-each action.
57-
* - We are not inside an if-then action.
58-
* - There is no if-then action in the flow,
59-
*
60-
* Using many consts as purpose of the conditions may not be immediately
61-
* apparent.
62-
*/
63-
function isForEachSelectable({
64-
flow,
65-
hasIfThen,
66-
isLastStep,
67-
}: {
68-
flow: IFlow
69-
hasIfThen: boolean
70-
isLastStep: boolean
71-
}) {
72-
const hasForEach = flow?.steps?.some(
73-
(step: IStep) => step.key === TOOLBOX_ACTIONS.ForEach,
74-
)
75-
76-
if (hasForEach || hasIfThen || !isLastStep) {
32+
const isStepWithinForEach = groupedSteps.flat().some((groupedStep) => {
33+
if (step?.id === groupedStep.id || prevStepId === groupedStep.id) {
34+
return true
35+
}
7736
return false
78-
}
79-
80-
return true
37+
})
38+
return !isStepWithinForEach
8139
}
8240

8341
/**
@@ -116,6 +74,7 @@ function isIfThenSelectable({
11674
}
11775

11876
const isNestedBranch = depth > 0
77+
11978
return !isNestedBranch
12079
}
12180

@@ -129,21 +88,35 @@ export const useIsAppSelectable = ({
12988
prevStepId?: string
13089
}): Record<string, boolean> => {
13190
const { depth } = useContext(BranchContext)
132-
const { flow, hasIfThen } = useContext(EditorContext)
91+
const { groupedSteps } = useContext(StepsToDisplayContext)
13392
const { getFlagValue } = useContext(LaunchDarklyContext)
13493

94+
const hasIfThen = groupedSteps.some((group) =>
95+
group.some((step) => step.key === TOOLBOX_ACTIONS.IfThen),
96+
)
97+
98+
const hasForEach = groupedSteps.some((group) =>
99+
group.some((step) => step.key === TOOLBOX_ACTIONS.ForEach),
100+
)
101+
console.log({
102+
step,
103+
prevStepId,
104+
})
105+
135106
return {
136107
[TOOLBOX_ACTIONS.IfThen]: isIfThenSelectable({
137108
depth,
138109
hasIfThen,
139110
isLastStep,
140111
getFlagValue,
141112
}),
142-
[TOOLBOX_ACTIONS.ForEach]: isForEachSelectable({
143-
flow,
144-
hasIfThen,
145-
isLastStep,
146-
}),
147-
delay: isDelaySelectable({ flow, step, prevStepId }),
113+
/**
114+
* For-each should only be selectable if:
115+
* - We're the last step.
116+
* - There's no other for-each action
117+
* - There's no other if-then action
118+
*/
119+
[TOOLBOX_ACTIONS.ForEach]: isLastStep && !hasIfThen && !hasForEach,
120+
delay: isDelaySelectable({ hasForEach, groupedSteps, step, prevStepId }),
148121
}
149122
}

packages/frontend/src/components/FlowStepGroup/Content/IfThen/index.tsx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ import { Flex } from '@chakra-ui/react'
77
import { Button } from '@opengovsg/design-system-react'
88

99
import { EditorContext } from '@/contexts/Editor'
10+
import { MrfContext } from '@/contexts/MrfContext'
1011
import { CREATE_STEP } from '@/graphql/mutations/create-step'
1112
import { GET_FLOW } from '@/graphql/queries/get-flow'
13+
import { getMrfApprovalConfig } from '@/helpers/formsg'
1214
import { TOOLBOX_ACTIONS, TOOLBOX_APP_KEY } from '@/helpers/toolbox'
1315

1416
import Branch from './Branch'
@@ -24,6 +26,7 @@ export default function IfThen(props: IfThenProps): JSX.Element {
2426
const { groupedSteps, stepsBeforeGroup } = props
2527

2628
const { depth } = useContext(BranchContext)
29+
const { approvalBranches } = useContext(MrfContext)
2730
const {
2831
flowId,
2932
readOnly: isEditorReadOnly,
@@ -50,6 +53,11 @@ export default function IfThen(props: IfThenProps): JSX.Element {
5053
const lastGroup = groupedSteps[groupedSteps.length - 1]
5154
const lastStep = lastGroup[lastGroup.length - 1]
5255

56+
const approvalConfig = getMrfApprovalConfig({
57+
previousStep: lastStep,
58+
approvalBranches,
59+
})
60+
5361
const branchStep = await createStep({
5462
variables: {
5563
input: {
@@ -65,6 +73,9 @@ export default function IfThen(props: IfThenProps): JSX.Element {
6573
depth,
6674
branchName: `Branch ${numBranches + 1}`,
6775
},
76+
config: {
77+
approval: approvalConfig,
78+
},
6879
},
6980
},
7081
})
@@ -80,6 +91,9 @@ export default function IfThen(props: IfThenProps): JSX.Element {
8091
flow: {
8192
id: flowId,
8293
},
94+
config: {
95+
approval: approvalConfig,
96+
},
8397
},
8498
},
8599
})
@@ -92,6 +106,7 @@ export default function IfThen(props: IfThenProps): JSX.Element {
92106
createStep,
93107
flowId,
94108
depth,
109+
approvalBranches,
95110
numBranches,
96111
onDrawerOpen,
97112
setCurrentStepId,

0 commit comments

Comments
 (0)