Skip to content

Commit 4239c86

Browse files
committed
feat: modify approval branch state in context
1 parent 973a635 commit 4239c86

File tree

3 files changed

+27
-9
lines changed

3 files changed

+27
-9
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export default function FlowStepWithAddButton({
4444
// only allow reordering if there are more than 1 action steps
4545
allowReorder={true}
4646
/>
47-
{isApprovalStep && <ApproveReject />}
47+
{isApprovalStep && <ApproveReject stepId={step.id} />}
4848
<AddStepButton
4949
isLastStep={isLastStep}
5050
step={step}

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

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

4+
import { MrfContext } from '@/contexts/MrfContext'
5+
46
const tabStyle = (primaryColor: string): TabProps => {
57
return {
68
_selected: {
@@ -21,19 +23,28 @@ const tabStyle = (primaryColor: string): TabProps => {
2123
}
2224
}
2325

24-
export function ApproveReject() {
25-
const [isApprovedSelected, setIsApprovedSelected] = useState(true)
26+
export function ApproveReject({ stepId }: { stepId: string }) {
27+
const { approvalBranches, setApprovalBranch } = useContext(MrfContext)
28+
29+
const isApproveBranch = approvalBranches[stepId] === 'approve'
30+
31+
const onChange = useCallback(
32+
(index: number) => {
33+
setApprovalBranch(stepId, index === 0 ? 'approve' : 'reject')
34+
},
35+
[stepId, setApprovalBranch],
36+
)
2637

2738
return (
2839
<Flex mt={4} mx="auto">
2940
<Tabs
3041
variant="soft-rounded"
31-
index={isApprovedSelected ? 0 : 1}
42+
index={isApproveBranch ? 0 : 1}
3243
backgroundColor="base.divider.medium"
3344
borderRadius="full"
3445
py={1}
3546
px={0.5}
36-
onChange={(index) => setIsApprovedSelected(index === 0)}
47+
onChange={onChange}
3748
>
3849
<TabList gap={2}>
3950
<Tab {...tabStyle('green.500')}>If approved</Tab>

packages/frontend/src/helpers/toolbox.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,12 @@ export function useIfThenInitializer(): [
151151
async (currStep: IStep) => {
152152
setIsInitializing(true)
153153

154+
const commonConfig = {
155+
...(currStep.config?.approval && {
156+
approval: currStep.config?.approval,
157+
}),
158+
}
159+
154160
const updateFirstBranch = await updateStep({
155161
variables: {
156162
input: {
@@ -167,6 +173,7 @@ export function useIfThenInitializer(): [
167173
connection: {
168174
id: null,
169175
},
176+
// no need to set config here since it's already set
170177
},
171178
},
172179
})
@@ -185,9 +192,7 @@ export function useIfThenInitializer(): [
185192
depth,
186193
branchName: 'Branch 2',
187194
},
188-
config: {
189-
approval: currStep.config?.approval,
190-
},
195+
config: commonConfig,
191196
},
192197
},
193198
})
@@ -211,6 +216,7 @@ export function useIfThenInitializer(): [
211216
flow: {
212217
id: currStep.flowId,
213218
},
219+
config: commonConfig,
214220
},
215221
},
216222
})
@@ -223,6 +229,7 @@ export function useIfThenInitializer(): [
223229
flow: {
224230
id: currStep.flowId,
225231
},
232+
config: commonConfig,
226233
},
227234
},
228235
})

0 commit comments

Comments
 (0)