@@ -5,28 +5,20 @@ import {
55 useCallback ,
66 createContext ,
77 useContext ,
8- useState ,
98} from 'react' ;
109import { FormProvider , useForm } from 'react-hook-form' ;
11- import { Address , isAddress } from 'viem' ;
10+ import type { Address } from 'viem' ;
1211import invariant from 'tiny-invariant' ;
1312
14- import { useFormControllerRetry } from 'shared/hook-form/form-controller/use-form-controller-retry-delegate' ;
1513import { useWithdrawable , useWithdraw } from 'features/supply/withdraw/hooks' ;
1614
1715import {
1816 FormController ,
1917 FormControllerContext ,
2018 FormControllerContextValueType ,
2119} from 'shared/hook-form/form-controller' ;
22- import { SubmitModal } from 'shared/components' ;
2320
2421import { WithdrawFormSchema } from 'features/supply/withdraw/types' ;
25- import {
26- SubmitPayload ,
27- SubmitStep ,
28- SubmitStepEnum ,
29- } from 'shared/components/submit-modal/types' ;
3022
3123type WithdrawDataContextValue = {
3224 withdrawableAmount : bigint | undefined ;
@@ -53,10 +45,6 @@ export const useWithdrawFormData = () => {
5345export const WithdrawFormProvider : FC < { children : ReactNode } > = ( {
5446 children,
5547} ) => {
56- const [ submitStep , setSubmitStep ] = useState < {
57- step : SubmitStep ;
58- tx ?: Address ;
59- } > ( ( ) => ( { step : SubmitStepEnum . edit } ) ) ;
6048 const formObject = useForm < WithdrawFormSchema > ( {
6149 defaultValues : {
6250 amount : undefined ,
@@ -65,11 +53,7 @@ export const WithdrawFormProvider: FC<{ children: ReactNode }> = ({
6553 mode : 'all' ,
6654 reValidateMode : 'onChange' ,
6755 } ) ;
68- const { callWithdraw } = useWithdraw ( ) ;
69- const { retryEvent, retryFire } = useFormControllerRetry ( ) ;
70- const setModalState = useCallback ( ( submitStep : SubmitPayload ) => {
71- setSubmitStep ( submitStep ) ;
72- } , [ ] ) ;
56+ const { withdraw, retryEvent } = useWithdraw ( ) ;
7357
7458 const {
7559 data : withdrawableAmount ,
@@ -95,49 +79,34 @@ export const WithdrawFormProvider: FC<{ children: ReactNode }> = ({
9579
9680 const onSubmit = useCallback (
9781 async ( { amount, recipient } : WithdrawFormSchema ) => {
98- try {
99- if ( amount && recipient && isAddress ( recipient ) ) {
100- setModalState ( { step : SubmitStepEnum . initiate } ) ;
101- const tx = await callWithdraw ( { amount, recipient, setModalState } ) ;
102- setModalState ( { step : SubmitStepEnum . overview , tx } ) ;
103- return true ;
104- }
105- } catch ( err ) {
106- if (
107- err instanceof Error &&
108- err . message . includes ( 'User rejected the request' )
109- ) {
110- setModalState ( { step : SubmitStepEnum . reject } ) ;
111- } else {
112- setModalState ( { step : SubmitStepEnum . error } ) ;
113- }
82+ invariant (
83+ amount ,
84+ '[WithdrawFormProvider] withdrawableAmount is undefined' ,
85+ ) ;
86+ invariant ( recipient , '[WithdrawFormProvider] recipient is undefined' ) ;
11487
115- return false ;
116- }
88+ const { success } = await withdraw ( { amount, recipient } ) ;
11789
118- return false ;
90+ return success ;
11991 } ,
120- // eslint-disable-next-line react-hooks/exhaustive-deps
121- [ callWithdraw ] ,
92+ [ withdraw ] ,
12293 ) ;
12394
12495 const formControllerValue : FormControllerContextValueType < WithdrawFormSchema > =
12596 useMemo (
12697 ( ) => ( {
12798 onSubmit,
12899 retryEvent,
129- retryFire,
130100 onReset : formObject . reset ,
131101 } ) ,
132- [ retryFire , retryEvent , onSubmit , formObject . reset ] ,
102+ [ retryEvent , onSubmit , formObject . reset ] ,
133103 ) ;
134104
135105 return (
136106 < WithdrawDataContext . Provider value = { withdrawData } >
137107 < FormProvider { ...formObject } >
138108 < FormControllerContext . Provider value = { formControllerValue } >
139109 < FormController > { children } </ FormController >
140- < SubmitModal submitStep = { submitStep } setModalState = { setModalState } />
141110 </ FormControllerContext . Provider >
142111 </ FormProvider >
143112 </ WithdrawDataContext . Provider >
0 commit comments