From 382c0b6d9368ab6f12d5e4cf4d560b814aff1d90 Mon Sep 17 00:00:00 2001 From: Juanma Hidalgo Date: Mon, 27 Nov 2023 19:38:16 +0100 Subject: [PATCH] feat: ask for login with clicking buy with crypto and there is no wallet connected --- .../BuyNFTButtons/BuyNFTButtons.container.tsx | 7 ++- .../BuyNFTButtons/BuyNFTButtons.tsx | 31 ++++++++++-- .../BuyNFTButtons/BuyNFTButtons.types.ts | 16 ++++++- .../BuyWithCryptoModal/BuyWithCryptoModal.tsx | 47 +++++++++++++++++-- 4 files changed, 90 insertions(+), 11 deletions(-) diff --git a/webapp/src/components/AssetPage/SaleActionBox/BuyNFTButtons/BuyNFTButtons.container.tsx b/webapp/src/components/AssetPage/SaleActionBox/BuyNFTButtons/BuyNFTButtons.container.tsx index d89552a88d..dda78e182e 100644 --- a/webapp/src/components/AssetPage/SaleActionBox/BuyNFTButtons/BuyNFTButtons.container.tsx +++ b/webapp/src/components/AssetPage/SaleActionBox/BuyNFTButtons/BuyNFTButtons.container.tsx @@ -1,4 +1,5 @@ import { connect } from 'react-redux' +import { replace } from 'connected-react-router' import { Order } from '@dcl/schemas' import { openModal } from 'decentraland-dapps/dist/modules/modal/actions' import { @@ -11,9 +12,12 @@ import { buyItemWithCardRequest } from '../../../../modules/item/actions' import { Asset } from '../../../../modules/asset/types' import { RootState } from '../../../../modules/reducer' import { getIsBuyCrossChainEnabled } from '../../../../modules/features/selectors' +import { getWallet, isConnecting } from '../../../../modules/wallet/selectors' import BuyNFTButtons from './BuyNFTButtons' const mapState = (state: RootState): MapStateProps => ({ + wallet: getWallet(state), + isConnecting: isConnecting(state), isBuyCrossChainEnabled: getIsBuyCrossChainEnabled(state) }) @@ -26,7 +30,8 @@ const mapDispatch = (dispatch: MapDispatch): MapDispatchProps => ({ order }) ), - onBuyItemWithCard: item => dispatch(buyItemWithCardRequest(item)) + onBuyItemWithCard: item => dispatch(buyItemWithCardRequest(item)), + onRedirect: path => dispatch(replace(path)) }) export default connect(mapState, mapDispatch)(BuyNFTButtons) diff --git a/webapp/src/components/AssetPage/SaleActionBox/BuyNFTButtons/BuyNFTButtons.tsx b/webapp/src/components/AssetPage/SaleActionBox/BuyNFTButtons/BuyNFTButtons.tsx index 520b640b93..5e1cec3bc9 100644 --- a/webapp/src/components/AssetPage/SaleActionBox/BuyNFTButtons/BuyNFTButtons.tsx +++ b/webapp/src/components/AssetPage/SaleActionBox/BuyNFTButtons/BuyNFTButtons.tsx @@ -1,5 +1,5 @@ -import { memo, useCallback } from 'react' -import { Link } from 'react-router-dom' +import { memo, useCallback, useMemo } from 'react' +import { Link, useLocation } from 'react-router-dom' import { Button, Icon, Mana } from 'decentraland-ui' import { t } from 'decentraland-dapps/dist/modules/translation/utils' import { getAnalytics } from 'decentraland-dapps/dist/modules/analytics/utils' @@ -12,6 +12,8 @@ import styles from './BuyNFTButtons.module.css' import { Props } from './BuyNFTButtons.types' const BuyNFTButtons = ({ + wallet, + isConnecting, asset, assetType, tokenId, @@ -19,9 +21,16 @@ const BuyNFTButtons = ({ isBuyCrossChainEnabled, onBuyWithCrypto, onExecuteOrderWithCard, - onBuyItemWithCard + onBuyItemWithCard, + onRedirect }: Props) => { const analytics = getAnalytics() + const location = useLocation() + const shouldOpenBuyWithCryptoModal = useMemo(() => { + const search = new URLSearchParams(location.search) + const shouldOpenModal = search.get('buyWithCrypto') + return shouldOpenModal + }, [location.search]) const assetId = tokenId || asset.itemId const handleBuyWithCard = useCallback( @@ -32,6 +41,17 @@ const BuyNFTButtons = ({ [analytics, onBuyItemWithCard, onExecuteOrderWithCard] ) + const handleBuyWithCrypto = useCallback( + order => { + if (!isConnecting && !wallet) { + onRedirect(locations.signIn(`${location.pathname}?buyWithCrypto=true`)) + } else { + onBuyWithCrypto(asset, order) + } + }, + [asset, isConnecting, onBuyWithCrypto, onRedirect, location, wallet] + ) + return ( <> {(asset, order) => { if (!asset) return null + if (shouldOpenBuyWithCryptoModal) { + onBuyWithCrypto(asset, order) + } return ( <> {isBuyCrossChainEnabled ? (