11import { 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'
114import { BiTrash } from 'react-icons/bi'
125import { 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'
267import { IconButton } from '@opengovsg/design-system-react'
278
289import FlowStep from '@/components/FlowStep'
2910import { EditorContext } from '@/contexts/Editor'
3011import { CREATE_STEP } from '@/graphql/mutations/create-step'
31- import { DELETE_STEP } from '@/graphql/mutations/delete-step'
3212import { 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'
3417import { allowAddStep } from '../utils'
3518
3619import { 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}
0 commit comments