Skip to content
This repository has been archived by the owner on Jan 15, 2021. It is now read-only.

Commit

Permalink
Fix TokenAdder (#1641)
Browse files Browse the repository at this point in the history
* move TokensAdder higher into TradeWidgetContainer

* cleanup

* remember position of added token

* remove extra logic
  • Loading branch information
Velenir authored Nov 24, 2020
1 parent b8aad5f commit 1be5563
Showing 1 changed file with 41 additions and 26 deletions.
67 changes: 41 additions & 26 deletions src/components/TradeWidget/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -181,15 +181,49 @@ const TradeWidgetContainer: React.FC = () => {
const [sellToken, setSellToken] = useState(sellTokenWithFallback)
const [receiveToken, setReceiveToken] = useState(buyTokenWithFallback)

useEffect(() => {
setSellToken(sellTokenWithFallback)
setReceiveToken(buyTokenWithFallback)
}, [sellTokenWithFallback, buyTokenWithFallback])
const onTokensAdded = (newTokens: TokenDetails[]): void => {
const { sellTokenAddress, receiveTokenAddress } = positionedAddedTokens
const sellToken =
sellTokenAddress && newTokens.find(({ address }) => sellTokenAddress.toLowerCase() === address.toLowerCase())
const receiveToken =
receiveTokenAddress &&
newTokens.find(({ address }) => receiveTokenAddress.toLowerCase() === address.toLowerCase())

// don't need to depend on more than network as everything else updates together
// also avoids excessive setStates
batchUpdateState(() => {
if (sellToken) setSellToken(sellToken)
if (receiveToken) setReceiveToken(receiveToken)
})
}

if (!sellToken || !receiveToken) return <NoTokens>NO TOKENS FOUND</NoTokens>
const { tokenAddressesToAdd, positionedAddedTokens } = useMemo(
() => {
const tokenAddressesToAdd = preprocessTokenAddressesToAdd(
[sellTokenSymbol, receiveTokenSymbol],
networkIdOrDefault,
)
return {
tokenAddressesToAdd,
positionedAddedTokens: {
sellTokenAddress: sellTokenSymbol,
receiveTokenAddress: receiveTokenSymbol,
},
}
},
// eslint-disable-next-line react-hooks/exhaustive-deps
[],
) // no deps, so that we only calc once on mount
if (!sellToken || !receiveToken) {
return (
<>
<TokensAdder
tokenAddresses={tokenAddressesToAdd}
networkId={networkIdOrDefault}
onTokensAdded={onTokensAdded}
/>
<NoTokens>NO TOKENS FOUND</NoTokens>
</>
)
}

return (
<TradeWidget
Expand Down Expand Up @@ -605,24 +639,6 @@ const TradeWidget: React.FC<TradeWidgetProps> = ({
const onSelectChangeSellToken = onSelectChangeFactory(setSellToken, receiveTokenBalance)
const onSelectChangeReceiveToken = onSelectChangeFactory(setReceiveToken, sellTokenBalance)

const tokenAddressesToAdd: string[] = useMemo(
() => preprocessTokenAddressesToAdd([sellToken.symbol, receiveToken.symbol], networkIdOrDefault),
// eslint-disable-next-line react-hooks/exhaustive-deps
[],
) // no deps, so that we only calc once on mount

const onTokensAdded = (newTokens: TokenDetails[]): void => {
const [firstToken, secondToken] = tokenAddressesToAdd
const sellToken = firstToken && newTokens.find(({ address }) => firstToken.toLowerCase() === address.toLowerCase())
const receiveToken =
secondToken && newTokens.find(({ address }) => secondToken.toLowerCase() === address.toLowerCase())

batchUpdateState(() => {
if (sellToken) onSelectChangeSellToken(sellToken)
if (receiveToken) onSelectChangeReceiveToken(receiveToken)
})
}

const onConfirm = handleSubmit(onSubmit)
const { toggleModal, modalProps } = useSubmitTxModal({
onConfirm,
Expand All @@ -637,7 +653,6 @@ const TradeWidget: React.FC<TradeWidgetProps> = ({

return (
<WrappedWidget className={ordersVisible ? '' : 'expanded'}>
<TokensAdder tokenAddresses={tokenAddressesToAdd} networkId={networkIdOrDefault} onTokensAdded={onTokensAdded} />
{/* Toggle Class 'expanded' on WrappedWidget on click of the <OrdersPanel> <button> */}
<FormProvider {...methods}>
<WrappedForm onSubmit={(e): void => e.preventDefault()} autoComplete="off" noValidate>
Expand Down

0 comments on commit 1be5563

Please sign in to comment.