fix: fixed updating states

This commit is contained in:
Linkie Link 2023-11-14 11:51:50 +01:00
parent ddc592855e
commit 0be6b2ff97
No known key found for this signature in database
GPG Key ID: 5318B0F2564D38EA

View File

@ -48,7 +48,7 @@ export default function SwapForm(props: Props) {
const { data: marketAssets } = useMarketAssets() const { data: marketAssets } = useMarketAssets()
const { data: route, isLoading: isRouteLoading } = useSwapRoute(sellAsset.denom, buyAsset.denom) const { data: route, isLoading: isRouteLoading } = useSwapRoute(sellAsset.denom, buyAsset.denom)
const isBorrowEnabled = !!marketAssets.find(byDenom(sellAsset.denom))?.borrowEnabled const isBorrowEnabled = !!marketAssets.find(byDenom(sellAsset.denom))?.borrowEnabled
const isRepayable = account?.debts.find(byDenom(buyAsset.denom)) const isRepayable = !!account?.debts.find(byDenom(buyAsset.denom))
const [isMarginChecked, setMarginChecked] = useToggle(isBorrowEnabled ? useMargin : false) const [isMarginChecked, setMarginChecked] = useToggle(isBorrowEnabled ? useMargin : false)
const [isAutoRepayChecked, setAutoRepayChecked] = useToggle(isRepayable ? useAutoRepay : false) const [isAutoRepayChecked, setAutoRepayChecked] = useToggle(isRepayable ? useAutoRepay : false)
const [buyAssetAmount, setBuyAssetAmount] = useState(BN_ZERO) const [buyAssetAmount, setBuyAssetAmount] = useState(BN_ZERO)
@ -198,16 +198,17 @@ export default function SwapForm(props: Props) {
) )
const handleAutoRepayToggleChange = useCallback( const handleAutoRepayToggleChange = useCallback(
(isAutoRepay: boolean) => { (isAutoRepay: boolean) => {
useStore.setState({ useAutoRepay: isAutoRepay }) if (isRepayable) useStore.setState({ useAutoRepay: isAutoRepay })
setAutoRepayChecked(isAutoRepay) setAutoRepayChecked(isAutoRepay)
}, },
[setAutoRepayChecked], [isRepayable, setAutoRepayChecked],
) )
useEffect(() => { useEffect(() => {
setBuyAssetAmount(BN_ZERO) setBuyAssetAmount(BN_ZERO)
setSellAssetAmount(BN_ZERO) setSellAssetAmount(BN_ZERO)
setMarginChecked(isBorrowEnabled ? useMargin : false) setMarginChecked(isBorrowEnabled ? useMargin : false)
setAutoRepayChecked(isRepayable ? useAutoRepay : false)
simulateTrade( simulateTrade(
BNCoin.fromDenomAndBigNumber(buyAsset.denom, BN_ZERO), BNCoin.fromDenomAndBigNumber(buyAsset.denom, BN_ZERO),
BNCoin.fromDenomAndBigNumber(sellAsset.denom, BN_ZERO), BNCoin.fromDenomAndBigNumber(sellAsset.denom, BN_ZERO),
@ -217,6 +218,7 @@ export default function SwapForm(props: Props) {
) )
}, [ }, [
isBorrowEnabled, isBorrowEnabled,
isRepayable,
useMargin, useMargin,
buyAsset.denom, buyAsset.denom,
sellAsset.denom, sellAsset.denom,
@ -224,6 +226,7 @@ export default function SwapForm(props: Props) {
isAutoRepayChecked, isAutoRepayChecked,
simulateTrade, simulateTrade,
setMarginChecked, setMarginChecked,
setAutoRepayChecked,
]) ])
useEffect(() => { useEffect(() => {