Skip to content

Commit

Permalink
refactor: add toChain for automatic selection
Browse files Browse the repository at this point in the history
  • Loading branch information
chybisov committed Aug 11, 2022
1 parent 0ebc628 commit 29e8691
Showing 1 changed file with 16 additions and 13 deletions.
29 changes: 16 additions & 13 deletions packages/widget/src/providers/SwapFormProvider/SwapFormProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,25 +32,28 @@ export const SwapFormProvider: React.FC<React.PropsWithChildren<{}>> = ({
},
});

// Set wallet chain as default if no fromChain is provided by config and if it wasn't changed during widget usage
// Set wallet chain as default if no chains are provided by config and if they were not changed during widget usage
useEffect(() => {
const { isDirty, isTouched } = methods.getFieldState(
SwapFormKey.FromChain,
methods.formState,
);
if (
account.isActive &&
account.chainId &&
!fromChain &&
!isDirty &&
!isTouched
) {
if (!account.isActive || !account.chainId) {
return;
}
const { isDirty: isFromChainDirty, isTouched: isFromChainTouched } =
methods.getFieldState(SwapFormKey.FromChain, methods.formState);
if (!fromChain && !isFromChainDirty && !isFromChainTouched) {
methods.setValue(SwapFormKey.FromChain, account.chainId, {
shouldDirty: false,
shouldTouch: false,
});
}
}, [account.chainId, account.isActive, fromChain, methods]);
const { isDirty: isToChainDirty, isTouched: isToChainTouched } =
methods.getFieldState(SwapFormKey.ToChain, methods.formState);
if (!toChain && !isToChainDirty && !isToChainTouched) {
methods.setValue(SwapFormKey.ToChain, account.chainId, {
shouldDirty: false,
shouldTouch: false,
});
}
}, [account.chainId, account.isActive, fromChain, methods, toChain]);

return <FormProvider {...methods}>{children}</FormProvider>;
};

0 comments on commit 29e8691

Please sign in to comment.