From 1b34cb77ebe906a29b2761733f3f137860300aec Mon Sep 17 00:00:00 2001 From: Yusuf Seyrek Date: Mon, 31 Jul 2023 16:23:54 +0300 Subject: [PATCH] fix: wrong asset selector state on overlay close (#332) --- .../Trade/TradeModule/AssetSelector/index.tsx | 38 ++++++++++--------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/src/components/Trade/TradeModule/AssetSelector/index.tsx b/src/components/Trade/TradeModule/AssetSelector/index.tsx index bc830785..5839b07a 100644 --- a/src/components/Trade/TradeModule/AssetSelector/index.tsx +++ b/src/components/Trade/TradeModule/AssetSelector/index.tsx @@ -15,23 +15,27 @@ interface Props { export default function AssetSelector(props: Props) { const { buyAsset, sellAsset, onChangeBuyAsset, onChangeSellAsset } = props const [overlayState, setOverlayState] = useState('closed') - const [cachedBuyAsset, setCachedBuyAsset] = useState(props.buyAsset) - const [cachedSellAsset, setCachedSellAsset] = useState(props.sellAsset) const handleSwapAssets = useCallback(() => { onChangeBuyAsset(sellAsset) onChangeSellAsset(buyAsset) }, [onChangeBuyAsset, onChangeSellAsset, sellAsset, buyAsset]) - const handleChangeBuyAsset = useCallback((asset: Asset) => { - setCachedBuyAsset(asset) - setOverlayState('sell') - }, []) + const handleChangeBuyAsset = useCallback( + (asset: Asset) => { + onChangeBuyAsset(asset) + setOverlayState('sell') + }, + [onChangeBuyAsset], + ) - const handleChangeSellAsset = useCallback((asset: Asset) => { - setCachedSellAsset(asset) - setOverlayState('closed') - }, []) + const handleChangeSellAsset = useCallback( + (asset: Asset) => { + onChangeSellAsset(asset) + setOverlayState('closed') + }, + [onChangeSellAsset], + ) const handleChangeState = useCallback( (state: OverlayState) => { @@ -42,10 +46,10 @@ export default function AssetSelector(props: Props) { useEffect(() => { if (overlayState === 'closed') { - onChangeBuyAsset(cachedBuyAsset) - onChangeSellAsset(cachedSellAsset) + onChangeBuyAsset(buyAsset) + onChangeSellAsset(sellAsset) } - }, [onChangeBuyAsset, onChangeSellAsset, overlayState, cachedBuyAsset, cachedSellAsset]) + }, [onChangeBuyAsset, onChangeSellAsset, overlayState, buyAsset, sellAsset]) return (
@@ -53,16 +57,16 @@ export default function AssetSelector(props: Props) { Sell - setOverlayState('buy')} asset={props.buyAsset} /> + setOverlayState('buy')} asset={buyAsset} /> - setOverlayState('sell')} asset={props.sellAsset} /> + setOverlayState('sell')} asset={sellAsset} />