Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Refactor buy with crypto modal #2130

Merged
merged 19 commits into from
Feb 20, 2024
Merged

Conversation

LautaroPetaccio
Copy link
Contributor

@LautaroPetaccio LautaroPetaccio commented Feb 13, 2024

This PR refactors the BuyWithCryptoModal by:

  • Extracting most of the logic of computing the gas cost and the route from the component into props, which are then loaded through their specific components (MintNftWithCryptoModal and BuyNftWithCryptoModal). This logic has been moved and refactored into hooks.
  • Extracting some of the code that generates the modal into two components to reduce the complexity of the modal. These two new componentes are the PaymentSelector and the PurchaseTotal

Copy link

vercel bot commented Feb 13, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
marketplace ✅ Ready (Inspect) Visit Preview 💬 Add feedback Feb 20, 2024 2:34pm

@coveralls
Copy link

coveralls commented Feb 16, 2024

Coverage Status

coverage: 65.747% (-1.3%) from 67.05%
when pulling 8cbd855 on fix/refactor-buy-with-crypto-modal
into 48b88a8 on master.

@fzavalia
Copy link
Contributor

fzavalia commented Feb 20, 2024

  • Mint NFT with Polygon MANA
  • Mint NFT with Polygon DAI

Tested both of these and they worked correctly :D

@fzavalia
Copy link
Contributor

There is an issue when selecting 0 balance tokens, the estimations are not being updated. This will not affect the user given that with 0 balance they cannot proceed, just as a reminder.

Copy link
Contributor

@fzavalia fzavalia left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great idea of separating the PaymentSelector, PurchaseTotal and hooks out of the main component.
I tried some operations and it worked correctly.
Also committed some formatting and import fixes.

Copy link
Contributor

@juanmahidalgo juanmahidalgo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks awesome, so much organized than before. Left a few comments of how I think we could improve it further

})
}, [nft, order, getContract, onAuthorizedAction, onExecuteOrder])

const onBuyWithCard = useCallback(() => onExecuteOrderWithCard(nft), [nft])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this looks a little bit unnecessary IMO, it might be simpler to just use

Suggested change
const onBuyWithCard = useCallback(() => onExecuteOrderWithCard(nft), [nft])
const onBuyWithCard = (nft) => onExecuteOrderWithCard(nft)

or if you really want to avoid those re-definitions of the fn in each render, maybe like this?

Suggested change
const onBuyWithCard = useCallback(() => onExecuteOrderWithCard(nft), [nft])
const onBuyWithCard = useCallback((nft) => onExecuteOrderWithCard(nft), [onExecuteOrderWithCard])

import { Props } from './MintNftWithCryptoModal.types'
import { OnGetCrossChainRoute, OnGetGasCost } from '../BuyWithCryptoModal.types'

const MintNftWithCryptoModalHOC = (props: Props) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe we could turn this to a generic HOC that has a prop like action that could be MINT/BUY that abstracts most of the logic to avoid having repetead code that has really small differences.
E.g:

  • onBuyNatively is almost identical, it just changes the contract and nft <=> item
  • onGetCrossChainRoute as well is too similar, just changes the nft/tem
  • similar case for other props

)
}

export const useCrossChainMintNftRoute = (
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar case here, a lot of props and params repetead between the 2, could we think of making this more generic maybe?

export const useCrossChainBuyRoute = (
       item?: Item,
       order?: Order,
       ....
       
       ) => {
           const fn = item ? getMintNFTRoute : getBuyNFTRoute
           const payload = item ? { item : { collectionAddress: .... }  } : { nft: { ... } }
           ...
           return crossChainProvider.fn({ ...baseParams, ...payload })

This might be a big refactor, so maybe we could leave it for later. WDYT?

fzavalia and others added 2 commits February 20, 2024 15:29
…yptoModal.tsx

Co-authored-by: Juanma Hidalgo <juanma06@gmail.com>
Signed-off-by: Fernando Zavalia <24811313+fzavalia@users.noreply.github.com>
@fzavalia fzavalia merged commit dfe508c into master Feb 20, 2024
7 checks passed
@fzavalia fzavalia deleted the fix/refactor-buy-with-crypto-modal branch February 20, 2024 14:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants