-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsupply-form-provider.tsx
More file actions
109 lines (97 loc) · 3.01 KB
/
supply-form-provider.tsx
File metadata and controls
109 lines (97 loc) · 3.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import {
createContext,
FC,
ReactNode,
useCallback,
useContext,
useEffect,
} from 'react';
import invariant from 'tiny-invariant';
import { useForm } from 'react-hook-form';
import { useDappStatus } from 'modules/web3';
import { useAwaiter } from 'shared/hooks/use-awaiter';
import { FormControllerStyled } from 'shared/components/form';
import { useDisableForm } from 'shared/hook-form';
import { SupplyFormResolver } from './validation';
import {
useSupply,
useSupplyFormValidationContext,
useSupplyFormData,
} from './hooks';
import {
SupplyFormData,
SupplyFormDataAwaitableValidationContext,
SupplyFormFieldValues,
SupplyFormValidatedValues,
} from '../types';
// Context for sharing relevant form data
const SupplyFormDataContext = createContext<SupplyFormData | null>(null);
SupplyFormDataContext.displayName = 'SupplyFormDataContext';
export const useSupplyForm = () => {
const context = useContext(SupplyFormDataContext);
invariant(
context,
'[useSupplyForm] useSupplyForm must be used within a SupplyFormProvider',
);
return context;
};
export const SupplyFormProvider: FC<{ children: ReactNode }> = ({
children,
}) => {
const { isDappActive } = useDappStatus();
const { validationContext } = useSupplyFormValidationContext();
const { supply, retryEvent } = useSupply();
const disabled = useDisableForm();
const formObject = useForm<
SupplyFormFieldValues,
SupplyFormDataAwaitableValidationContext,
SupplyFormValidatedValues
>({
defaultValues: {
token: 'ETH',
amount: null,
mintSteth: false,
mintAddress: '',
},
mode: 'onTouched',
disabled: !isDappActive || disabled,
context: useAwaiter(validationContext).awaiter,
resolver: SupplyFormResolver,
});
const { watch, resetField } = formObject;
const [mintSteth, amount, token] = watch(['mintSteth', 'amount', 'token']);
const supplyFormData = useSupplyFormData(token, mintSteth, amount);
const invalidateSupplyFormData = supplyFormData.invalidateSupplyFormData;
const isStethMintable = supplyFormData.isStethMintableQuery.data === true;
useEffect(() => {
// reset mintSteth when stETH is not mintable
if (mintSteth && !isStethMintable) {
resetField('mintSteth');
}
// reset mintAddress when mintSteth=false
if (!mintSteth) {
resetField('mintAddress');
}
}, [isStethMintable, mintSteth, resetField]);
const onSubmit = useCallback(
async (values: SupplyFormValidatedValues) => {
const result = await supply(values);
// revalidate form data because some TXs may have changed the state
await invalidateSupplyFormData();
return result;
},
[invalidateSupplyFormData, supply],
);
return (
<SupplyFormDataContext.Provider value={supplyFormData}>
<FormControllerStyled
formObject={formObject}
onSubmit={onSubmit}
retryEvent={retryEvent}
data-testid="supplyForm"
>
{children}
</FormControllerStyled>
</SupplyFormDataContext.Provider>
);
};