Skip to content

Commit 642929c

Browse files
committed
feat: edit logic to determine approval branch in step metadata context
1 parent 97d11ff commit 642929c

File tree

4 files changed

+72
-16
lines changed

4 files changed

+72
-16
lines changed

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ export default function FlowStep(
7777
substeps,
7878
shouldShowDragHandle,
7979
isDeletable,
80+
approvalBranch,
8081
} = useStepMetadata(step, true)
8182

8283
const {
@@ -161,8 +162,6 @@ export default function FlowStep(
161162
// are removed once user executes a successful test
162163
const hasInfoBox = shouldShowTemplateMsg || shouldTestStepAgain
163164

164-
const approvalBranch = step.config.approval?.branch
165-
166165
if (!allApps) {
167166
return <CircularProgress isIndeterminate my={2} />
168167
}
@@ -236,7 +235,7 @@ export default function FlowStep(
236235
w={headerWidth}
237236
// approval branch can be approve, reject or undefined
238237
// boxShadow specified in theme/foundations/shadows.ts
239-
boxShadow={isNested ? 'none' : approvalBranch}
238+
boxShadow={isNested ? 'none' : approvalBranch ?? undefined}
240239
>
241240
<Flex {...flowStepStyles.topHeader} onClick={handleClick}>
242241
<StepAppIcon

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { NESTED_DRAG_HANDLE_WIDTH } from '@/components/SortableList/components/S
99
import { EditorContext } from '@/contexts/Editor'
1010
import { getFlowStepHeaderWidth, getToolboxIcon } from '@/helpers/editor'
1111
import { TOOLBOX_ACTIONS } from '@/helpers/toolbox'
12+
import { useStepMetadata } from '@/hooks/useStepMetadata'
1213

1314
import { MIN_FLOW_STEP_WIDTH } from '../Editor/constants'
1415

@@ -27,6 +28,7 @@ interface FlowStepGroupProps {
2728
export default function FlowStepGroup(props: FlowStepGroupProps) {
2829
const { groupedSteps, stepsBeforeGroup } = props
2930
const { isDrawerOpen, isMobile, onDrawerClose } = useContext(EditorContext)
31+
const { approvalBranch } = useStepMetadata(groupedSteps[0]?.[0])
3032

3133
const { stepGroupType, stepGroupCaption } = useMemo(() => {
3234
let stepGroupType: string | null = null
@@ -70,8 +72,6 @@ export default function FlowStepGroup(props: FlowStepGroupProps) {
7072
(step) => step.key === TOOLBOX_ACTIONS.ForEach,
7173
)
7274

73-
const approvalBranch = groupedSteps[0]?.[0]?.config?.approval?.branch
74-
7575
return (
7676
<Flex
7777
w={
@@ -90,7 +90,7 @@ export default function FlowStepGroup(props: FlowStepGroupProps) {
9090
// approval branch can be approve, reject or undefined
9191
// boxShadow specified in theme/foundations/shadows.ts
9292
// we display for entire group instead of individual nested steps
93-
boxShadow={approvalBranch}
93+
boxShadow={approvalBranch ?? undefined}
9494
>
9595
<Box {...flowStepGroupStyles.header} w="100%">
9696
<Flex

packages/frontend/src/helpers/formsg.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@ export function getMrfApprovalConfig({
3636
}
3737

3838
/**
39-
* If previous step is an approval step, check which branch it's currently set to
39+
* If previous step is an approval step and is set to reject, return the reject branch
4040
*/
41-
if (previousStep.id in approvalBranches) {
41+
if (approvalBranches[previousStep.id] === 'reject') {
4242
return {
43-
branch: approvalBranches[previousStep.id],
43+
branch: 'reject',
4444
stepId: previousStep.id,
4545
}
4646
}

packages/frontend/src/hooks/useStepMetadata.ts

Lines changed: 64 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
1-
import { IAction, IApp, IStep, ISubstep, ITrigger } from '@plumber/types'
1+
import {
2+
IAction,
3+
IApp,
4+
IStep,
5+
IStepApprovalBranch,
6+
ISubstep,
7+
ITrigger,
8+
} from '@plumber/types'
29

310
import { useContext, useMemo } from 'react'
4-
import get from 'lodash/get'
511

612
import { EditorContext } from '@/contexts/Editor'
7-
import { FORMSG_APP_KEY, MRF_ACTION_KEY } from '@/helpers/formsg'
13+
import { MrfContext } from '@/contexts/MrfContext'
814
import getStepName from '@/helpers/getStepName'
915
import {
1016
isIfThenStep as checkIfThenStep,
@@ -27,6 +33,7 @@ interface UseStepMetadataResult {
2733
isDeletable: boolean
2834
isMrfStep: boolean
2935
isApprovalStep: boolean
36+
approvalBranch: IStepApprovalBranch | null
3037
}
3138

3239
export function useStepMetadata(
@@ -35,6 +42,7 @@ export function useStepMetadata(
3542
): UseStepMetadataResult {
3643
const { readOnly, isMobile, isDrawerOpen, allApps } =
3744
useContext(EditorContext)
45+
const { mrfSteps, approvalBranches } = useContext(MrfContext)
3846

3947
const isCompleted = step?.status === 'completed'
4048
const isTrigger = step?.type === 'trigger'
@@ -75,11 +83,59 @@ export function useStepMetadata(
7583
[readOnly, selectedActionOrTrigger, step?.key],
7684
)
7785

78-
const isMrfStep =
79-
step?.appKey === FORMSG_APP_KEY && step?.key === MRF_ACTION_KEY
86+
const isMrfStep = useMemo(() => {
87+
return mrfSteps.some((mrfStep) => mrfStep.id === step?.id)
88+
}, [mrfSteps, step?.id])
89+
90+
const isApprovalStep = useMemo(() => {
91+
if (!step?.id) {
92+
return false
93+
}
94+
return approvalBranches[step.id] !== undefined
95+
}, [approvalBranches, step?.id])
8096

81-
const isApprovalStep =
82-
isMrfStep && !!get(step?.parameters, 'mrf.approvalField', false)
97+
const approvalBranch = useMemo(() => {
98+
// If step not instantiated, return null
99+
if (!step) {
100+
return null
101+
}
102+
// If not MRF workflow, return null
103+
if (!mrfSteps.length || !step) {
104+
return null
105+
}
106+
// If step is an MRF step, return null
107+
if (isMrfStep) {
108+
return null
109+
}
110+
if (step.config.approval?.branch === 'reject') {
111+
return 'reject'
112+
}
113+
// Find the most immediate prior mrfStep by iterating from the last to the first.
114+
let immediatePriorMrfStep = null
115+
for (let i = mrfSteps.length - 1; i >= 0; i--) {
116+
const mrfStep = mrfSteps[i]
117+
if (mrfStep.position < step.position) {
118+
immediatePriorMrfStep = mrfStep
119+
break
120+
}
121+
}
122+
// if is approval step, return whether
123+
if (!immediatePriorMrfStep) {
124+
console.warn(
125+
'No immediate prior mrf step found... this should not happen tho',
126+
)
127+
return null
128+
}
129+
/**
130+
* If no approval step config exists, but is part of an mrf branch,
131+
* it's part of the approve branch by default
132+
*/
133+
if (approvalBranches[immediatePriorMrfStep.id]) {
134+
return 'approve'
135+
}
136+
// If follows a non-approval mrf step, return null
137+
return null
138+
}, [step, mrfSteps, isMrfStep, approvalBranches])
83139

84140
/**
85141
* NOTE: there are various conditions that determine whether the drag handle
@@ -130,5 +186,6 @@ export function useStepMetadata(
130186
isDeletable,
131187
isMrfStep,
132188
isApprovalStep,
189+
approvalBranch,
133190
}
134191
}

0 commit comments

Comments
 (0)