Skip to content

Commit fb78662

Browse files
committed
chore: refactor branch deletion
1 parent 0fcd046 commit fb78662

File tree

3 files changed

+36
-78
lines changed

3 files changed

+36
-78
lines changed

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

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

3-
import {
4-
Fragment,
5-
MouseEventHandler,
6-
useCallback,
7-
useContext,
8-
useMemo,
9-
useRef,
10-
} from 'react'
3+
import { Fragment, useCallback, useContext, useMemo } from 'react'
114
import { BiTrash } from 'react-icons/bi'
125
import { useMutation } from '@apollo/client'
13-
import {
14-
AlertDialog,
15-
AlertDialogBody,
16-
AlertDialogContent,
17-
AlertDialogFooter,
18-
AlertDialogHeader,
19-
AlertDialogOverlay,
20-
Box,
21-
Button,
22-
Flex,
23-
Text,
24-
useDisclosure,
25-
} from '@chakra-ui/react'
6+
import { Box, Flex, Text } from '@chakra-ui/react'
267
import { IconButton } from '@opengovsg/design-system-react'
278

289
import FlowStep from '@/components/FlowStep'
2910
import { EditorContext } from '@/contexts/Editor'
3011
import { CREATE_STEP } from '@/graphql/mutations/create-step'
31-
import { DELETE_STEP } from '@/graphql/mutations/delete-step'
3212
import { GET_FLOW } from '@/graphql/queries/get-flow'
13+
import { TOOLBOX_ACTIONS } from '@/helpers/toolbox'
3314

15+
import DeleteConfirmationDialog from '../../components/DeleteConfirmationDialog'
16+
import useDeleteConfirmation from '../../hooks/useDeleteConfirmation'
3417
import { allowAddStep } from '../utils'
3518

3619
import { HoverAddStepButton } from './HoverAddStepButton'
@@ -57,30 +40,19 @@ export default function Branch(props: BranchProps) {
5740

5841
// Handle branch deletion
5942
const {
60-
isOpen: deleteConfirmationIsOpen,
61-
onOpen: openDeleteConfirmationImpl,
43+
isDeletingBranch,
44+
isOpen: isDeleteConfirmationOpen,
45+
onOpen: openDeleteConfirmation,
6246
onClose: closeDeleteConfirmation,
63-
} = useDisclosure()
64-
const cancelDeleteButton = useRef<HTMLButtonElement>(null)
47+
onDelete: deleteBranch,
48+
cancelRef,
49+
} = useDeleteConfirmation(TOOLBOX_ACTIONS.IfThen, groupedSteps, branchSteps)
6550
const [createStep, { loading: isCreatingStep }] = useMutation(CREATE_STEP, {
6651
fetchPolicy: 'no-cache',
6752
refetchQueries: [GET_FLOW],
6853
})
69-
const [deleteStep, { loading: isDeletingBranch }] = useMutation(DELETE_STEP, {
70-
refetchQueries: [GET_FLOW],
71-
})
72-
const openDeleteConfirmation = useCallback<MouseEventHandler>(
73-
(e) => {
74-
e.stopPropagation()
75-
openDeleteConfirmationImpl()
76-
},
77-
[openDeleteConfirmationImpl],
78-
)
79-
const deleteBranch = useCallback(async () => {
80-
const idsToDelete = branchSteps.map((step) => step.id)
81-
await deleteStep({
82-
variables: { input: { ids: idsToDelete } },
83-
})
54+
const handleDeleteBranch = useCallback(async () => {
55+
await deleteBranch()
8456

8557
// EDGE CASE: if the branch is the last step in a for-each action,
8658
// we should create an empty step after the for-each set up step
@@ -103,8 +75,7 @@ export default function Branch(props: BranchProps) {
10375
closeDeleteConfirmation()
10476
onDrawerClose()
10577
}, [
106-
branchSteps,
107-
deleteStep,
78+
deleteBranch,
10879
hasForEach,
10980
groupedSteps.length,
11081
stepsBeforeGroup,
@@ -144,7 +115,7 @@ export default function Branch(props: BranchProps) {
144115
<IconButton
145116
boxSize={8}
146117
onClick={(event) => {
147-
openDeleteConfirmation(event)
118+
openDeleteConfirmation()
148119
event.stopPropagation()
149120
}}
150121
variant="clear"
@@ -179,36 +150,14 @@ export default function Branch(props: BranchProps) {
179150
})}
180151

181152
{/* Delete Confirmation Modal */}
182-
<AlertDialog
183-
isOpen={deleteConfirmationIsOpen}
184-
leastDestructiveRef={cancelDeleteButton}
153+
<DeleteConfirmationDialog
154+
name={branchSteps[0].parameters.branchName as string}
155+
cancelRef={cancelRef}
156+
isOpen={isDeleteConfirmationOpen}
185157
onClose={closeDeleteConfirmation}
186-
>
187-
<AlertDialogOverlay>
188-
<AlertDialogContent>
189-
<AlertDialogHeader>
190-
Delete {branchSteps[0].parameters.branchName as string}
191-
</AlertDialogHeader>
192-
<AlertDialogBody>
193-
Are you sure you want to delete this branch? This action cannot be
194-
undone.
195-
</AlertDialogBody>
196-
<AlertDialogFooter>
197-
<Button
198-
colorScheme="neutral"
199-
variant="clear"
200-
ref={cancelDeleteButton}
201-
onClick={closeDeleteConfirmation}
202-
>
203-
Cancel
204-
</Button>
205-
<Button colorScheme="critical" onClick={deleteBranch} ml={3}>
206-
Yes, delete branch
207-
</Button>
208-
</AlertDialogFooter>
209-
</AlertDialogContent>
210-
</AlertDialogOverlay>
211-
</AlertDialog>
158+
onDelete={handleDeleteBranch}
159+
onCancel={closeDeleteConfirmation}
160+
/>
212161
</Flex>
213162
)
214163
}

packages/frontend/src/components/FlowStepGroup/hooks/useDeleteConfirmation.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ import { DELETE_STEP } from '@/graphql/mutations/delete-step'
88
import { GET_FLOW } from '@/graphql/queries/get-flow'
99
import { TOOLBOX_ACTIONS } from '@/helpers/toolbox'
1010

11-
const useDeleteConfirmation = (type: string, groupedSteps: IStep[][]) => {
11+
const useDeleteConfirmation = (
12+
type: string,
13+
groupedSteps: IStep[][],
14+
branchSteps?: IStep[],
15+
) => {
1216
const { isOpen, onOpen, onClose } = useDisclosure()
1317
const cancelRef = useRef<HTMLButtonElement>(null)
1418

@@ -35,11 +39,17 @@ const useDeleteConfirmation = (type: string, groupedSteps: IStep[][]) => {
3539
await deleteStep({
3640
variables: { input: { ids: idsToDelete } },
3741
})
38-
} else {
39-
// TODO: refactor branch deletion
42+
} else if (type === TOOLBOX_ACTIONS.IfThen) {
43+
if (!branchSteps) {
44+
return
45+
}
46+
const idsToDelete = branchSteps.map((step) => step.id)
47+
await deleteStep({
48+
variables: { input: { ids: idsToDelete } },
49+
})
4050
}
4151
onClose()
42-
}, [deleteStep, groupedSteps, onClose, type])
52+
}, [branchSteps, deleteStep, groupedSteps, onClose, type])
4353

4454
return {
4555
cancelRef,

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ export default function FlowStepGroup(props: FlowStepGroupProps) {
6868
alignItems="center"
6969
justifyContent={isDrawerOpen ? 'flex-start' : 'center'}
7070
>
71-
{/* FIXME (kevinkim-ogp): above is a temporary wrapper to ensure the flow step group is centered when drawer is closed */}
7271
<Flex
7372
{...flowStepGroupStyles.container}
7473
display={isMobile ? 'block' : 'flex'}

0 commit comments

Comments
 (0)