Skip to content

Commit cbf6bb7

Browse files
committed
feat: hide steps depending on approval branch
1 parent 021e5c4 commit cbf6bb7

File tree

4 files changed

+66
-15
lines changed

4 files changed

+66
-15
lines changed

packages/frontend/src/components/Editor/components/AddStepButton.tsx

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

3+
import { useContext, useMemo } from 'react'
34
import { BiPlus } from 'react-icons/bi'
45
import { Box, Divider, useDisclosure } from '@chakra-ui/react'
56
import { IconButton, TouchableTooltip } from '@opengovsg/design-system-react'
67

78
import EmptyFlowStepHeader from '@/components/EmptyFlowStepHeader'
89
import FlowStepConfigurationModal from '@/components/FlowStepConfigurationModal'
10+
import { MrfContext } from '@/contexts/MrfContext'
911
import { useUnsavedChanges } from '@/hooks/useUnsavedChanges'
1012

1113
import UnsavedChangesAlert from './UnsavedChangesAlert'
@@ -21,6 +23,28 @@ interface AddStepButtonProps {
2123
export function AddStepButton(props: AddStepButtonProps): JSX.Element {
2224
const { isHidden, isLastStep, step, isDisabled, showEmptyAction } = props
2325
const { isOpen, onOpen, onClose } = useDisclosure()
26+
const { approvalBranches } = useContext(MrfContext)
27+
const approvalBranch =
28+
step.config.approval?.branch || approvalBranches[step.id]
29+
30+
const { dividerColor, iconBgColor } = useMemo(() => {
31+
if (approvalBranch === undefined) {
32+
return {
33+
dividerColor: 'base.divider.strong',
34+
iconBgColor: undefined,
35+
}
36+
}
37+
if (approvalBranch === 'approve') {
38+
return {
39+
dividerColor: 'green.500',
40+
iconBgColor: 'green.50',
41+
}
42+
}
43+
return {
44+
dividerColor: 'red.500',
45+
iconBgColor: 'red.50',
46+
}
47+
}, [approvalBranch])
2448

2549
const {
2650
cancelRef,
@@ -65,7 +89,11 @@ export function AddStepButton(props: AddStepButtonProps): JSX.Element {
6589
)}
6690
{/* Top vertical line */}
6791
<Box h={6}>
68-
<Divider orientation="vertical" borderColor="base.divider.strong" />
92+
<Divider
93+
orientation="vertical"
94+
borderColor={dividerColor}
95+
boxShadow={approvalBranch}
96+
/>
6997
</Box>
7098
<TouchableTooltip
7199
label={isDisabled ? '' : 'Add step'}
@@ -81,6 +109,7 @@ export function AddStepButton(props: AddStepButtonProps): JSX.Element {
81109
variant={isLastStep ? 'outline' : 'clear'}
82110
size="xs"
83111
color="interaction.sub.default"
112+
bg={iconBgColor}
84113
borderRadius="full"
85114
pointerEvents={isDisabled ? 'none' : 'auto'}
86115
_hover={{
@@ -98,7 +127,8 @@ export function AddStepButton(props: AddStepButtonProps): JSX.Element {
98127
<Box h={6}>
99128
<Divider
100129
orientation="vertical"
101-
borderColor="base.divider.strong"
130+
borderColor={dividerColor}
131+
boxShadow={approvalBranch}
102132
/>
103133
</Box>
104134
)}

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { useCallback, useContext } from 'react'
22
import { Flex, Tab, TabList, TabProps, Tabs } from '@chakra-ui/react'
33

4+
import { EditorContext } from '@/contexts/Editor'
45
import { MrfContext } from '@/contexts/MrfContext'
56

67
const tabStyle = (primaryColor: string): TabProps => {
@@ -24,15 +25,18 @@ const tabStyle = (primaryColor: string): TabProps => {
2425
}
2526

2627
export function ApproveReject({ stepId }: { stepId: string }) {
28+
const { setCurrentStepId, onDrawerClose } = useContext(EditorContext)
2729
const { approvalBranches, setApprovalBranch } = useContext(MrfContext)
2830

2931
const isApproveBranch = approvalBranches[stepId] === 'approve'
3032

3133
const onChange = useCallback(
3234
(index: number) => {
3335
setApprovalBranch(stepId, index === 0 ? 'approve' : 'reject')
36+
setCurrentStepId(null)
37+
onDrawerClose()
3438
},
35-
[stepId, setApprovalBranch],
39+
[stepId, setApprovalBranch, setCurrentStepId, onDrawerClose],
3640
)
3741

3842
return (

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,6 @@ export default function FlowStep(
161161
// are removed once user executes a successful test
162162
const hasInfoBox = shouldShowTemplateMsg || shouldTestStepAgain
163163

164-
const approvalBranch = step.config.approval?.branch
165-
166164
if (!allApps) {
167165
return <CircularProgress isIndeterminate my={2} />
168166
}
@@ -234,9 +232,6 @@ export default function FlowStep(
234232
borderTopRadius={hasInfoBox ? 'none' : 'lg'}
235233
h={isNested ? '48px' : '64px'}
236234
w={headerWidth}
237-
// approval branch can be approve, reject or undefined
238-
// boxShadow specified in theme/foundations/shadows.ts
239-
boxShadow={isNested ? 'none' : approvalBranch}
240235
>
241236
<Flex {...flowStepStyles.topHeader} onClick={handleClick}>
242237
<StepAppIcon

packages/frontend/src/contexts/StepsToDisplay.tsx

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { createContext, useContext, useMemo } from 'react'
66
import { extractBranchesWithSteps } from '@/helpers/toolbox'
77

88
import { EditorContext } from './Editor'
9+
import { MrfContext } from './MrfContext'
910

1011
export type StepToDisplayContextValue = {
1112
triggerStep: IStep | null
@@ -31,8 +32,26 @@ export function StepsToDisplayProvider({
3132
children,
3233
}: StepExecutionsProviderProps): JSX.Element {
3334
const { allApps, flow } = useContext(EditorContext)
35+
const { approvalBranches } = useContext(MrfContext)
3436

35-
const steps = flow.steps
37+
const allSteps = flow.steps
38+
39+
const stepsToDisplay = allSteps.filter((step) => {
40+
if (!step.config?.approval) {
41+
return true
42+
}
43+
const approvalConfigStepId = step.config.approval.stepId
44+
const approvalConfigBranch = step.config.approval.branch
45+
console.log({
46+
approvalConfigStepId,
47+
approvalConfigBranch,
48+
currentBranch: approvalBranches[approvalConfigStepId],
49+
})
50+
if (approvalBranches[approvalConfigStepId] === approvalConfigBranch) {
51+
return true
52+
}
53+
return false
54+
})
3655

3756
const appsWithActions: IApp[] = allApps.filter(
3857
(app: IApp) => !!app.actions?.length,
@@ -57,7 +76,7 @@ export function StepsToDisplayProvider({
5776
return [null, [], []]
5877
}
5978

60-
const groupStepIdx = steps.findIndex((step, index) => {
79+
const groupStepIdx = stepsToDisplay.findIndex((step, index) => {
6180
if (
6281
// We ignore the 1st step because it's either a trigger, or a
6382
// step-grouping action that is using a nested Editor to edit steps in
@@ -73,15 +92,18 @@ export function StepsToDisplayProvider({
7392

7493
let branchesWithSteps: IStep[][] = []
7594
if (groupStepIdx !== -1) {
76-
branchesWithSteps = extractBranchesWithSteps(steps.slice(groupStepIdx), 0)
95+
branchesWithSteps = extractBranchesWithSteps(
96+
stepsToDisplay.slice(groupStepIdx),
97+
0,
98+
)
7799
}
78100

79-
const triggerStep = steps[0]
101+
const triggerStep = stepsToDisplay[0]
80102

81103
return groupStepIdx === -1
82-
? [triggerStep, steps.slice(1), []]
83-
: [triggerStep, steps.slice(1, groupStepIdx), branchesWithSteps]
84-
}, [groupingActions, steps])
104+
? [triggerStep, stepsToDisplay.slice(1), []]
105+
: [triggerStep, stepsToDisplay.slice(1, groupStepIdx), branchesWithSteps]
106+
}, [groupingActions, stepsToDisplay])
85107

86108
return (
87109
<StepsToDisplayContext.Provider

0 commit comments

Comments
 (0)