Commit f9eb46a0 authored by Jordan Frankfurt's avatar Jordan Frankfurt Committed by GitHub

fix: close bag when the user needs to connect a wallet (#5209)

parent 2970f9f1
...@@ -9,13 +9,13 @@ import { SupportedChainId } from 'constants/chains' ...@@ -9,13 +9,13 @@ import { SupportedChainId } from 'constants/chains'
import { Box } from 'nft/components/Box' import { Box } from 'nft/components/Box'
import { Column, Row } from 'nft/components/Flex' import { Column, Row } from 'nft/components/Flex'
import { bodySmall } from 'nft/css/common.css' import { bodySmall } from 'nft/css/common.css'
import { useBag } from 'nft/hooks/useBag'
import { useWalletBalance } from 'nft/hooks/useWalletBalance' import { useWalletBalance } from 'nft/hooks/useWalletBalance'
import { BagStatus } from 'nft/types' import { BagStatus } from 'nft/types'
import { ethNumberStandardFormatter, formatWeiToDecimal } from 'nft/utils' import { ethNumberStandardFormatter, formatWeiToDecimal } from 'nft/utils'
import { PropsWithChildren, useCallback, useMemo } from 'react' import { PropsWithChildren, useMemo } from 'react'
import { AlertTriangle } from 'react-feather' import { AlertTriangle } from 'react-feather'
import { useModalIsOpen, useToggleWalletModal } from 'state/application/hooks' import { useToggleWalletModal } from 'state/application/hooks'
import { ApplicationModal } from 'state/application/reducer'
import styled from 'styled-components/macro' import styled from 'styled-components/macro'
import { ThemedText } from 'theme' import { ThemedText } from 'theme'
import { switchChain } from 'utils/switchChain' import { switchChain } from 'utils/switchChain'
...@@ -95,19 +95,10 @@ export const BagFooter = ({ ...@@ -95,19 +95,10 @@ export const BagFooter = ({
eventProperties, eventProperties,
}: BagFooterProps) => { }: BagFooterProps) => {
const toggleWalletModal = useToggleWalletModal() const toggleWalletModal = useToggleWalletModal()
const walletModalIsOpen = useModalIsOpen(ApplicationModal.WALLET)
const { account, chainId, connector } = useWeb3React() const { account, chainId, connector } = useWeb3React()
const connected = Boolean(account && chainId) const connected = Boolean(account && chainId)
const handleClick = useCallback(() => { const setBagExpanded = useBag((state) => state.setBagExpanded)
if (!connected) {
toggleWalletModal()
} else if (connected && chainId !== SupportedChainId.MAINNET) {
switchChain(connector, SupportedChainId.MAINNET)
} else {
fetchAssets()
}
}, [connected, chainId, toggleWalletModal, connector, fetchAssets])
const { balance: balanceInEth } = useWalletBalance() const { balance: balanceInEth } = useWalletBalance()
const sufficientBalance = useMemo(() => { const sufficientBalance = useMemo(() => {
...@@ -117,12 +108,14 @@ export const BagFooter = ({ ...@@ -117,12 +108,14 @@ export const BagFooter = ({
return parseEther(balanceInEth).gte(totalEthPrice) return parseEther(balanceInEth).gte(totalEthPrice)
}, [connected, chainId, balanceInEth, totalEthPrice]) }, [connected, chainId, balanceInEth, totalEthPrice])
const { buttonText, disabled, warningText } = useMemo(() => { const { buttonText, disabled, warningText, handleClick } = useMemo(() => {
let handleClick = fetchAssets
let buttonText = <Trans>Something went wrong</Trans> let buttonText = <Trans>Something went wrong</Trans>
let disabled = true let disabled = true
let warningText = null let warningText = null
if (connected && chainId !== SupportedChainId.MAINNET) { if (connected && chainId !== SupportedChainId.MAINNET) {
handleClick = () => switchChain(connector, SupportedChainId.MAINNET)
buttonText = <Trans>Switch networks</Trans> buttonText = <Trans>Switch networks</Trans>
disabled = false disabled = false
warningText = <Trans>Wrong network</Trans> warningText = <Trans>Wrong network</Trans>
...@@ -132,7 +125,11 @@ export const BagFooter = ({ ...@@ -132,7 +125,11 @@ export const BagFooter = ({
warningText = <Trans>Insufficient funds</Trans> warningText = <Trans>Insufficient funds</Trans>
} else if (bagStatus === BagStatus.WARNING) { } else if (bagStatus === BagStatus.WARNING) {
warningText = <Trans>Something went wrong. Please try again.</Trans> warningText = <Trans>Something went wrong. Please try again.</Trans>
} else if (!connected || walletModalIsOpen) { } else if (!connected) {
handleClick = () => {
toggleWalletModal()
setBagExpanded({ bagExpanded: false })
}
disabled = false disabled = false
buttonText = <Trans>Connect wallet</Trans> buttonText = <Trans>Connect wallet</Trans>
} else if (bagStatus === BagStatus.FETCHING_FINAL_ROUTE || bagStatus === BagStatus.CONFIRMING_IN_WALLET) { } else if (bagStatus === BagStatus.FETCHING_FINAL_ROUTE || bagStatus === BagStatus.CONFIRMING_IN_WALLET) {
...@@ -146,10 +143,10 @@ export const BagFooter = ({ ...@@ -146,10 +143,10 @@ export const BagFooter = ({
buttonText = <Trans>Pay</Trans> buttonText = <Trans>Pay</Trans>
} }
return { buttonText, disabled, warningText } return { buttonText, disabled, warningText, handleClick }
}, [bagStatus, chainId, connected, sufficientBalance, walletModalIsOpen]) }, [bagStatus, chainId, connected, connector, fetchAssets, setBagExpanded, sufficientBalance, toggleWalletModal])
const isPending = PENDING_BAG_STATUSES.includes(bagStatus) || walletModalIsOpen const isPending = PENDING_BAG_STATUSES.includes(bagStatus)
return ( return (
<Column className={styles.footerContainer}> <Column className={styles.footerContainer}>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment