Skip to content

Commit

Permalink
feat: adding error handler
Browse files Browse the repository at this point in the history
  • Loading branch information
wellitongervickas committed Dec 9, 2023
1 parent 2948a18 commit 2373891
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 14 deletions.
2 changes: 1 addition & 1 deletion app/[locale]/apps/[id]/components/Container.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const PageContainer = ({ params }: AppLayoutProps) => {
[CrosschainNFTBridgeAppIdFuji]: CrosschainNFTBridge
}[id]

if (Component) return <Component />
if (Component) return <Component id={id} />

const t = useI18n()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,40 @@

import { Card } from '@/app/components'
import { allowedChainsConfig } from '@/app/config/config'
import { useMemo, useState } from 'react'
import { HTMLProps, useEffect, useMemo, useState } from 'react'
import { useEffectOnce } from 'usehooks-ts'
import { useNetwork } from 'wagmi'
import { Requirements } from './Requirements'
import { Form } from './Form'
import { FormProvider, useForm } from 'react-hook-form'
import { zodResolver } from '@hookform/resolvers/zod'
import * as z from 'zod'
import { ethers } from 'ethers'
import { useNetwork } from '@/app/lib/wallet/hooks'
import { usePathname, useRouter, useSearchParams } from 'next/navigation'
import classNames from 'classnames'

export const CrosschainNFTBridgeAppIdMumbai =
'0x3581ded89c04be046f61dbf380aff24bbb4d1866010c64302ad872e61a44c06023000000'

export const CrosschainNFTBridgeAppIdFuji =
'0xd53a723850e67d08016c7d6f115d60d59bc9c6bd6a8c75875bb6f15719ee8d6a00000000'

const bridgeChains = [allowedChainsConfig[80001], allowedChainsConfig[43113]]
const [mumbai, fuji] = [allowedChainsConfig[80001], allowedChainsConfig[43113]]

export const CrosschainNFTBridge = () => {
const [chains, setChains] = useState(bridgeChains)
const { chain } = useNetwork()
export type CrosschainNFTBridgeProps = HTMLProps<HTMLDivElement> & {
id: string
}

export const CrosschainNFTBridge = ({
id,
className,
...props
}: CrosschainNFTBridgeProps) => {
const pathname = usePathname()
const router = useRouter()

const [chains, setChains] = useState([mumbai, fuji])
const { chain, config } = useNetwork()

const sourceChain = useMemo(() => chains[0], [chains])
const destinationChain = useMemo(() => chains[1], [chains])
Expand All @@ -43,19 +56,38 @@ export const CrosschainNFTBridge = () => {
})

useEffectOnce(() => {
const [mumbai, fuji] = [bridgeChains[0].id, bridgeChains[1].id]

const sourceChainId = chain?.id === mumbai ? mumbai : fuji
const destinationChainId = sourceChainId === mumbai ? fuji : mumbai
const sourceChainId = chain?.id === mumbai.id ? mumbai.id : fuji.id
const destinationChainId = sourceChainId === mumbai.id ? fuji.id : mumbai.id

setChains(() => [
allowedChainsConfig[sourceChainId],
allowedChainsConfig[destinationChainId]
])
})

const updatePathname = (currentAppId: string, targetAppId: string) => {
const updatedPathname = pathname.replace(currentAppId, targetAppId)
router.replace(updatedPathname)
}

useEffect(() => {
const currentAppId =
config.id === mumbai.id
? CrosschainNFTBridgeAppIdFuji
: CrosschainNFTBridgeAppIdMumbai

const targetAppId =
config.id === mumbai.id
? CrosschainNFTBridgeAppIdMumbai
: CrosschainNFTBridgeAppIdFuji

if (pathname.includes(currentAppId)) {
updatePathname(currentAppId, targetAppId)
}
}, [config.id, mumbai.id, pathname, router])

return (
<div className='flex justify-center'>
<div className={classNames(className, 'flex justify-center')} {...props}>
<Card className='flex w-full flex-col space-y-4 rounded-md p-4 lg:w-auto lg:min-w-[26rem]'>
<FormProvider {...methods}>
<Form
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export const Form = ({
aria-label='Switch'
type='button'
onClick={onSwitch}
className='text-lime-400'
className='text-third-300 dark:text-lime-400'
>
<ArrowPathRoundedSquareIcon
width={24}
Expand All @@ -85,6 +85,10 @@ export const Form = ({
Your ERC721 has been bridged successfully to {destinationChain.name}!
</p>
)}

{!isLoading && status === 'error' && (
<p className='text-red-500'>Error! Please try again later.</p>
)}
</form>
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const Selector = ({
<Heading as='h5'>{label}</Heading>
<div className='flex justify-between'>
<Text className='text-gray-400'>{chain.name}</Text>
{chain && displayBlockExplorer && (
{address && chain && displayBlockExplorer && (
<Text>
<Link
target='_blank'
Expand Down

0 comments on commit 2373891

Please sign in to comment.