@@ -17,10 +17,11 @@ import {
1717 Stepper ,
1818 useDisclosure ,
1919} from "@chakra-ui/react" ;
20+ import { useMutation } from "@tanstack/react-query" ;
2021import { format } from "date-fns" ;
21- import { useTxNotifications } from "hooks/useTxNotifications" ;
2222import { CheckIcon } from "lucide-react" ;
2323import { useRef } from "react" ;
24+ import { toast } from "sonner" ;
2425import { Button , FormLabel , Text } from "tw-components" ;
2526
2627interface TimelineStep {
@@ -188,14 +189,9 @@ const CancelTransactionButton = ({
188189 authToken : string ;
189190} ) => {
190191 const { isOpen, onOpen, onClose } = useDisclosure ( ) ;
191- const { onSuccess, onError } = useTxNotifications (
192- "Successfully sent a request to cancel the transaction" ,
193- "Failed to cancel transaction" ,
194- ) ;
195192 const closeButtonRef = useRef < HTMLButtonElement > ( null ) ;
196-
197- const onClickContinue = async ( ) => {
198- try {
193+ const cancelTransactionMutation = useMutation ( {
194+ mutationFn : async ( ) => {
199195 const resp = await fetch ( `${ instanceUrl } transaction/cancel` , {
200196 method : "POST" ,
201197 headers : {
@@ -205,17 +201,24 @@ const CancelTransactionButton = ({
205201 } ,
206202 body : JSON . stringify ( { queueId : transaction . queueId } ) ,
207203 } ) ;
204+
208205 if ( ! resp . ok ) {
209206 const json = await resp . json ( ) ;
210207 throw json . error ?. message ;
211208 }
212- onSuccess ( ) ;
213- } catch ( e ) {
214- console . error ( "Cancelling transaction:" , e ) ;
215- onError ( e ) ;
216- }
209+ } ,
210+ } ) ;
217211
218- onClose ( ) ;
212+ const cancelTransactions = async ( ) => {
213+ const promise = cancelTransactionMutation . mutateAsync ( ) ;
214+ toast . promise ( promise , {
215+ success : "Transaction cancelled successfully" ,
216+ error : "Failed to cancel transaction" ,
217+ } ) ;
218+
219+ promise . then ( ( ) => {
220+ onClose ( ) ;
221+ } ) ;
219222 } ;
220223
221224 return (
@@ -273,7 +276,11 @@ const CancelTransactionButton = ({
273276 >
274277 Close
275278 </ Button >
276- < Button type = "submit" colorScheme = "red" onClick = { onClickContinue } >
279+ < Button
280+ colorScheme = "red"
281+ onClick = { cancelTransactions }
282+ isLoading = { cancelTransactionMutation . isPending }
283+ >
277284 Cancel transaction
278285 </ Button >
279286 </ ModalFooter >
0 commit comments