Skip to content

Commit 5188478

Browse files
committed
refactor: use prevStep instead of prevStepId
1 parent 3374fde commit 5188478

File tree

8 files changed

+41
-33
lines changed

8 files changed

+41
-33
lines changed

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { IStep } from '@plumber/types'
2+
13
import { BiPlus } from 'react-icons/bi'
24
import { Box, Divider, useDisclosure } from '@chakra-ui/react'
35
import { IconButton, TouchableTooltip } from '@opengovsg/design-system-react'
@@ -14,11 +16,11 @@ interface AddStepButtonProps {
1416
isDisabled: boolean
1517
isLastStep: boolean
1618
showEmptyAction: boolean
17-
stepId: string
19+
step: IStep
1820
}
1921

2022
export function AddStepButton(props: AddStepButtonProps): JSX.Element {
21-
const { isHidden, isLastStep, stepId, isDisabled, showEmptyAction } = props
23+
const { isHidden, isLastStep, step, isDisabled, showEmptyAction } = props
2224
const { isOpen, onOpen, onClose } = useDisclosure()
2325

2426
const {
@@ -107,7 +109,7 @@ export function AddStepButton(props: AddStepButtonProps): JSX.Element {
107109
onClose={onClose}
108110
isTrigger={false} // Can only add an action all the time
109111
isLastStep={isLastStep}
110-
prevStepId={stepId}
112+
prevStep={step}
111113
/>
112114
)}
113115
</>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export default function FlowStepWithAddButton({
4242
{isApprovalStep && <ApproveReject />}
4343
<AddStepButton
4444
isLastStep={isLastStep}
45-
stepId={step.id}
45+
step={step}
4646
isHidden={isHidden}
4747
isDisabled={isDisabled}
4848
showEmptyAction={showEmptyAction}

packages/frontend/src/components/FlowStepConfigurationModal/FlowStepConfigurationContext.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ interface FlowStepConfigurationContextValue {
99
patchModalState: (modalState: Partial<ModalState>) => void
1010
isTrigger: boolean
1111
isLastStep: boolean
12+
prevStep?: IStep
1213
prevStepId?: string
1314
step?: IStep
1415
}
@@ -49,7 +50,7 @@ interface FlowStepConfigurationContextProps {
4950
event?: ITrigger | IAction
5051
isTrigger: boolean
5152
isLastStep: boolean
52-
prevStepId?: string
53+
prevStep?: IStep
5354
children: React.ReactNode
5455
}
5556

@@ -59,7 +60,7 @@ export const FlowStepConfigurationContextProvider = ({
5960
event,
6061
isTrigger,
6162
isLastStep,
62-
prevStepId,
63+
prevStep,
6364
children,
6465
}: FlowStepConfigurationContextProps) => {
6566
const [modalState, setModalState] = useState<ModalState>({
@@ -81,7 +82,8 @@ export const FlowStepConfigurationContextProvider = ({
8182
patchModalState,
8283
isTrigger,
8384
isLastStep,
84-
prevStepId,
85+
prevStep,
86+
prevStepId: prevStep?.id,
8587
step,
8688
}}
8789
>

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ interface FlowStepConfigurationModalProps {
2020
step?: IStep
2121
app?: IApp
2222
event?: ITrigger | IAction
23-
prevStepId?: string
23+
prevStep?: IStep
2424
}
2525

2626
function FlowStepConfigurationModalContent({
@@ -52,15 +52,15 @@ function FlowStepConfigurationModalContent({
5252
export default function FlowStepConfigurationModal(
5353
props: FlowStepConfigurationModalProps,
5454
): JSX.Element {
55-
const { onClose, isTrigger, isLastStep, step, app, event, prevStepId } = props
55+
const { onClose, isTrigger, isLastStep, step, app, event, prevStep } = props
5656

5757
return (
5858
<FlowStepConfigurationContextProvider
5959
isTrigger={isTrigger}
6060
isLastStep={isLastStep}
6161
app={app}
6262
event={event}
63-
prevStepId={prevStepId}
63+
prevStep={prevStep}
6464
step={step}
6565
>
6666
<Modal

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ interface HoverAddStepButtonProps {
1818
isDisabled: boolean
1919
isDrawerOpen: boolean
2020
isLastStep: boolean
21-
prevStepId: string
21+
prevStep: IStep
2222
showEmptyAction?: boolean
2323
step: IStep
2424
allowReorder?: boolean
@@ -31,7 +31,7 @@ export function HoverAddStepButton(
3131
const {
3232
isDisabled,
3333
isLastStep,
34-
prevStepId,
34+
prevStep,
3535
showEmptyAction,
3636
step,
3737
allowReorder,
@@ -108,7 +108,7 @@ export function HoverAddStepButton(
108108
onClose={onClose}
109109
isTrigger={false} // Can only add an action all the time
110110
isLastStep={isLastStep}
111-
prevStepId={prevStepId}
111+
prevStep={prevStep}
112112
/>
113113
)}
114114

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export default function GroupStepWithAddButton(
4646
isDisabled={readOnly || !canAddStep}
4747
isDrawerOpen={isDrawerOpen}
4848
isLastStep={isLastStep}
49-
prevStepId={step.id}
49+
prevStep={step}
5050
showEmptyAction={showEmptyAction}
5151
step={step}
5252
allowReorder={allowReorder}

packages/frontend/src/contexts/MrfContext.tsx

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,25 @@
1-
import { IStep } from '@plumber/types'
1+
import { IStep, IStepApprovalBranch } from '@plumber/types'
22

3-
import { createContext, useContext, useState } from 'react'
3+
import { createContext, useState } from 'react'
4+
import get from 'lodash/get'
45

56
import { FORMSG_APP_KEY, MRF_ACTION_KEY } from '@/helpers/formsg'
67

7-
type ApprovalBranch = 'approve' | 'reject'
8-
98
interface MrfContextReturnValue {
109
mrfSteps: IStep[]
10+
mrfApprovalSteps: IStep[]
1111
approvalBranches: {
12-
[stepId: string]: ApprovalBranch
12+
[stepId: string]: IStepApprovalBranch
1313
}
14-
setApprovalBranch: (stepId: string, branch: ApprovalBranch) => void
14+
setApprovalBranch: (stepId: string, branch: IStepApprovalBranch) => void
1515
}
1616

17-
const MrfContext = createContext<MrfContextReturnValue | undefined>(undefined)
18-
19-
export const useMrfContext = () => {
20-
const context = useContext(MrfContext)
21-
if (!context) {
22-
throw new Error('useMrfContext must be used within a MrfContextProvider')
23-
}
24-
return context
25-
}
17+
export const MrfContext = createContext<MrfContextReturnValue>({
18+
mrfSteps: [],
19+
mrfApprovalSteps: [],
20+
approvalBranches: {},
21+
setApprovalBranch: () => null,
22+
})
2623

2724
interface MrfContextProviderProps {
2825
children: React.ReactNode
@@ -37,16 +34,20 @@ export const MrfContextProvider = ({
3734
(step) => step.appKey === FORMSG_APP_KEY && step.key === MRF_ACTION_KEY,
3835
)
3936

37+
const mrfApprovalSteps = mrfSteps.filter(
38+
(step) => !!get(step.parameters, 'mrf.approvalField', false),
39+
)
40+
4041
const [approvalBranches, setApprovalBranches] = useState<
41-
Record<string, ApprovalBranch>
42+
Record<string, IStepApprovalBranch>
4243
>({
43-
...mrfSteps.reduce((acc, step) => {
44+
...mrfApprovalSteps.reduce((acc, step) => {
4445
acc[step.id] = 'approve'
4546
return acc
46-
}, {} as Record<string, ApprovalBranch>),
47+
}, {} as Record<string, IStepApprovalBranch>),
4748
})
4849

49-
const setApprovalBranch = (stepId: string, branch: ApprovalBranch) => {
50+
const setApprovalBranch = (stepId: string, branch: IStepApprovalBranch) => {
5051
setApprovalBranches((prev) => ({
5152
...prev,
5253
[stepId]: branch,
@@ -57,6 +58,7 @@ export const MrfContextProvider = ({
5758
<MrfContext.Provider
5859
value={{
5960
mrfSteps,
61+
mrfApprovalSteps,
6062
approvalBranches,
6163
setApprovalBranch,
6264
}}

packages/types/index.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,8 @@ export interface IExecution {
166166
createdAt: string
167167
}
168168

169+
export type IStepApprovalBranch = 'approve' | 'reject'
170+
169171
export interface IStepConfig {
170172
stepName?: string
171173
templateConfig?: IStepTemplateConfig

0 commit comments

Comments
 (0)